1

i am trying to convert imageview into 60px circular image but its not happening ...

the way i am trying is...

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

it is loading rectangle shape image so what should i do ?

Please help...

Loktar
  • 34,764
  • 7
  • 90
  • 104
dev_android
  • 493
  • 1
  • 11
  • 29

3 Answers3

2

here is my code and it works for me:

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

}

just create a class and put this code in it, and then you can use it like this:

// 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));   
}
Hamid Reza
  • 624
  • 7
  • 23
1

For that the better option you have universal image loader. you can easily make image rounded with create an option of image loader

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

you can take more reference here

GovindRathod
  • 867
  • 1
  • 8
  • 22
1

see below link :-

Add border to getRoundedCornerBitmap android

or use 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);
Community
  • 1
  • 1
duggu
  • 37,851
  • 12
  • 116
  • 113