【问题标题】:Applying a bitmap transformation on an ImageView [closed]在 ImageView 上应用位图转换 [关闭]
【发布时间】:2013-01-25 22:58:50
【问题描述】:

如何让这段代码工作?我想把一张图片变成一个crycle,我在这个网站上找到了这个代码,但它没有说明如何使用它......有人有什么想法吗?

Cropping circular area from bitmap in Android

public Bitmap getCroppedBitmap(Bitmap bitmap) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
            bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    // canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
    canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2,
            bitmap.getWidth() / 2, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    //Bitmap _bmp = Bitmap.createScaledBitmap(output, 60, 60, false);
    //return _bmp;
    return output;
}

【问题讨论】:

    标签: java android


    【解决方案1】:

    假设您有一个要使用的可绘制对象?

    image 对象 - 可以是 ImageView:

    Bitmap bitImg = BitmapFactory.decodeResource(getResources(),
        R.drawable.YOUR_DRAWABLE_NAME);
    image.setImageBitmap(getCroppedBitmap(bitImg));
    

    【讨论】:

      【解决方案2】:

      您将您的可绘制对象转换为位图,将其传入并将其设置为 ImageView(我假设这是您想要的):

      Bitmap myDrawable = BitmapFactory.decodeResource(getResources(), R.drawable.myDrawable);
      
      myImageView.setImageBitmap(getCroppedBitmap(myDrawable));
      

      【讨论】:

        猜你喜欢
        • 2013-12-15
        • 1970-01-01
        • 1970-01-01
        • 2012-06-13
        • 2023-03-23
        • 2012-06-24
        • 1970-01-01
        • 1970-01-01
        • 2013-05-11
        相关资源
        最近更新 更多