尝试将图像从矩形转换为圆形

时间:2014-05-20 11:34:00

标签: android bitmap android-canvas

我正在尝试将imageview转换为60px的圆形图像,但它没有发生......

我正在尝试的方式是......

File imgFile = new  File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                    "MyCameraApp" + File.separator + "profile_" + userId + ".jpg");

        if(imgFile.exists()){
            Bitmap correctBmp=null;

              ExifInterface exif;
            try {
                exif = new ExifInterface(imgFile.getPath());
                  int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

                    int angle = 0;

                    if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
                           angle = 90;
                    }
                    else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
                           angle = 180;
                    }
                    else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
                           angle = 270;
                    }

                    Matrix mat = new Matrix();
                    mat.postRotate(90);



                  Bitmap bmp1 = BitmapFactory.decodeStream(new FileInputStream(imgFile), null, null);
                  /* correctBmp = Bitmap.createBitmap(bmp1, 0, 0, bmp1.getWidth(), bmp1.getHeight(), mat, true);*/
                  correctBmp = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(),  bmp1.getConfig());
                  Canvas canvas = new Canvas(correctBmp);
                   Paint paint = new Paint();
                   paint.setAntiAlias(true);
                   paint.setShader(new BitmapShader(bmp1, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
                   canvas.drawRoundRect((new RectF(0.0f, 0.0f, bmp1.getWidth(), bmp1.getHeight())), 10, 10, paint);
                   profileImage.setImageBitmap(correctBmp);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
                  }

它正在加载矩形图像,所以该怎么办?

请帮忙......

3 个答案:

答案 0 :(得分:2)

这是我的代码,它对我有用:

public class ImageHelper {

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

}

只需创建一个类并将此代码放入其中,然后就可以像这样使用它:

// create round image
public void setCircularImage() {

    ImageView imv = (ImageView) findViewById(R.id.imageView1);
    Bitmap bitImg = BitmapFactory.decodeResource(getResources(), R.drawable.icon_person);
    ImageHelper imgHlp = new ImageHelper();
    imv.setImageBitmap(imgHlp.getCroppedBitmap(bitImg));   
}

答案 1 :(得分:1)

为此,您可以选择通用图像加载器。 您可以通过创建图像加载器选项轻松地使图像变圆

options = new DisplayImageOptions.Builder()
        .displayer(new RoundedBitmapDisplayer(50))
        .showStubImage(R.drawable.ic_app)
        .showImageForEmptyUri(R.drawable.camera)
        .showImageOnFail(R.drawable.ic_error)
        .cacheOnDisc()
        .build();

您可以参考更多参考here

答案 2 :(得分:1)

见以下链接: -

Add border to getRoundedCornerBitmap android

或使用Universal Loader

DisplayImageOptions options = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisc(true)
.displayer(new RoundedBitmapDisplayer(60))
.build();//value which you want to round

ImageLoader.getInstance().displayImage(Uri.parse(imgByURL).toString(), imgThumb, options);