如何在我的imageview android中为位图添加Round Corner方法?

时间:2014-07-21 04:42:33

标签: android performance android-imageview android-bitmap

Gud早上的朋友们。我在listview的行中创建了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;

现在我想在My This imageview中设置位图方法。用我的代码。我想补充的方法是吼叫。

 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;
 }

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

2 个答案:

答案 0 :(得分:0)

尝试以下代码:

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

答案 1 :(得分:0)

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

    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;
    }

你可以在一些常量类中编写方法,并在适配器中调用此方法 //照片是要在圆形

中转换的位图图像
Bitmap bitmapImage = Bitmap.createScaledBitmap(photo, 200, 200,true);
    bitmap = imageprocessing.getRoundedRectBitmap(bitmapImage, 100);