【问题标题】:How can i add Round Corner method for bitmap in my imageview android? [duplicate]如何在我的 imageview android 中为位图添加圆角方法? [复制]
【发布时间】:2014-07-21 14:15:37
【问题描述】:

Gud 早安朋友们。我在列表视图的行中创建了 Imageview。我还在我的 Imageview 中设置了我的位图,例如,

public View getView(final int position, View convertView, ViewGroup parent) {
    View row = convertView;
    UserHolder holder = null;
    Log.d("main", "pos:" + "" + position);

            if (row == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);
        holder = new UserHolder();
        holder.Name = (TextView) row.findViewById(R.id.name);

        holder.Number = (TextView) row.findViewById(R.id.number);

        holder.img=(ImageView) row.findViewById(R.id.image);


        Typeface face=Typeface.createFromAsset(context.getAssets(),"helve.ttf");

        holder.Name.setTypeface(face);
        //holder.Number.setTypeface(face);

        //bit = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);     
       //

        //holder.img.setImageBitmap(getRoundedCornerBitmap(bit, 40));

        row.setTag(holder);
    } else {
        holder = (UserHolder) row.getTag();
    }
    User user = data.get(position); 

    holder.Name.setText(user.getName());
    //holder.img.setImageBitmap(user.getbi());
    holder.img.setImageBitmap(roundCornerImage(user.getbi(),50));
    //holder.img.setImageBitmap(roundCornerImage(BitmapFactory.decodeResource(user.getbi(), R.drawable.ic_launcher),60));
    holder.Number.setText(user.getNumber());




    // Give Different Back Ground To List View---------------------------------------------

    if ((position % 2) == 0) {
        row.setBackgroundResource(R.drawable.list_dark);
    } else {
        row.setBackgroundResource(R.drawable.list_light);
    }




    Log.d("main", "pos:" + "" + position);

    return row;

现在我想在我的这个图像视图中设置位图方法。用我的代码。我想添加的方法如下。

 public Bitmap roundCornerImage(Bitmap src, float round) {
  // Source image size
  int width = src.getWidth();
  int height = src.getHeight();
  // create result bitmap output
  Bitmap result = Bitmap.createBitmap(width, height, Config.ARGB_8888);
  // set canvas for painting
  Canvas canvas = new Canvas(result);
  canvas.drawARGB(0, 0, 0, 0);

  // configure paint
  final Paint paint = new Paint();
  paint.setAntiAlias(true);
  //paint.setColor);


  // configure rectangle for embedding
  final Rect rect = new Rect(0, 0, width, height);
  final RectF rectF = new RectF(rect);

  // draw Round rectangle to canvas
  canvas.drawRoundRect(rectF, round, round, paint);

  // create Xfer mode
  paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
  // draw source image to canvas
  canvas.drawBitmap(src, rect, rect, paint);

  // return final image
  return result;
 }

如何在我的代码中添加此方法。提前谢谢你。

【问题讨论】:

  • 你有源位图吗?
  • 是的,我有位图中的联系人图像。
  • 将此问题作为重复问题关闭,因为它正是您针对同一问题提出的duplicate question。与其提出新问题,不如编辑现有问题并提供足够的详细信息,让您得到好的答案。

标签: android performance android-imageview android-bitmap


【解决方案1】:

试试下面的代码:

holder.img.setImageBitmap(roundCornerImage(user.getbi(),ANY CONSTANT VALUE TO MAKE YOUR IMAGEVIEW ROUND LIKE-1,2));

【讨论】:

  • 完成了,但什么是常量值。??我是 android @pratik 的新手
  • 试一试,看你需要多少回合,试一试,看看效果如何
  • 好吧,试试看告诉我。
  • 兄弟它不起作用。我的项目是强制关闭... NULL POINTER EXEPTION
  • 它的真实代码是... imViewAndroid.setImageBitmap(roundCornerImage(BitmapFactory.decodeResource(getResources(), R.drawable.custom_bg),60));
【解决方案2】:

您可以在适配器中使用此方法--

    public Bitmap getRoundedRectBitmap(Bitmap scalebitmap, int pixels) {
        Bitmap targetBitmap = null;
        try {

            int targetWidth = 150;
            int targetHeight = 150;
            targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight,
                    Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(targetBitmap);
            Path path = new Path();
            path.addCircle(
                    ((float) targetWidth) / 2,
                    ((float) targetHeight) / 2,
                    (Math.min(((float) targetWidth), ((float) targetHeight)) / 2),
                    Path.Direction.CW);
            Paint paint = new Paint();
            paint.setColor(Color.GRAY);
            // paint.setStyle(Paint.Style.STROKE);
            paint.setStyle(Paint.Style.FILL);
            paint.setAntiAlias(true);
            paint.setDither(true);
            paint.setFilterBitmap(true);
            canvas.drawOval(new RectF(0, 0, targetWidth, targetHeight), paint);
            // paint.setColor(Color.TRANSPARENT);
            canvas.clipPath(path);
            Bitmap sourceBitmap = scalebitmap;
            canvas.drawBitmap(
                    sourceBitmap,
                    new Rect(0, 0, sourceBitmap.getWidth(), sourceBitmap
                            .getHeight()), new RectF(0, 0, targetWidth,
                            targetHeight), paint);

        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (OutOfMemoryError o) {
            o.printStackTrace();
        }
        return targetBitmap;
    }

你可以在一些常量类中编写方法并在适配器中调用这个方法,比如 //photo 是你要圆形转换的Bitmap图片

Bitmap bitmapImage = Bitmap.createScaledBitmap(photo, 200, 200,true);
    bitmap = imageprocessing.getRoundedRectBitmap(bitmapImage, 100);

【讨论】:

  • 什么是图像处理。?? @user1170640
  • 我在 Imageprocessing.java 文件中编写了 getRoundedRxtBitmap() ,而图像处理就是其中的对象。
  • 比什么是位图..???
猜你喜欢
  • 2013-08-16
  • 2021-06-09
  • 2012-09-15
  • 1970-01-01
  • 2021-10-31
  • 2013-04-18
  • 1970-01-01
  • 1970-01-01
  • 2011-03-10
相关资源
最近更新 更多