How to make an image fit into a circular frame in android

后端 未结 7 2026
暖寄归人
暖寄归人 2020-12-01 00:27

I have a ListView in which there is an ImageView, the image in the ImageView gets loaded dynamically after its fetched from the server

相关标签:
7条回答
  • 2020-12-01 00:50

    We can manage the height and width of an image from xml code and draw circle/oval from java code like

        <ImageView
                android:id="@+id/imageView1"
                android:layout_width="@dimen/width"
                android:layout_height="@dimen/height"
                />
    

    for oval view

    ImageView img1 = (ImageView) findViewById(R.id.imageView1);
    Bitmap bm = BitmapFactory.decodeResource(getResources(),
            R.drawable.user_image);
    Bitmap conv_bm = getRoundedBitmap(bm);
    img1.setImageBitmap(conv_bm);
    
    
    public static Bitmap getRoundedBitmap(Bitmap bitmap)
    {
        final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        final Canvas canvas = new Canvas(output);
    
        final int color = Color.RED;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);
    
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawOval(rectF, paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
    
        bitmap.recycle();
    
        return output;
      }
    
     }
    
    0 讨论(0)
  • 2020-12-01 00:52
     public static Bitmap getCircleBitmap(Bitmap bitmap) {
            final Bitmap circuleBitmap = Bitmap.createBitmap(bitmap.getWidth(),
                    bitmap.getWidth(), Bitmap.Config.ARGB_8888);
            final Canvas canvas = new Canvas(circuleBitmap);
    
            final int color = Color.RED;
            final Paint paint = new Paint();
            final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getWidth());
            final RectF rectF = new RectF(rect);
    
            paint.setAntiAlias(true);
            canvas.drawARGB(0, 0, 0, 0);
            paint.setColor(color);
            canvas.drawOval(rectF, paint);
    
            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
            canvas.drawBitmap(bitmap, rect, rect, paint);
    
            bitmap.recycle();
    
            return circuleBitmap;
        }
    
    0 讨论(0)
  • 2020-12-01 00:54

    With the help of previous answer I came up with this solution.Hope it help others:

    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
     import android.graphics.Canvas;
     import android.graphics.Paint;
    import android.graphics.PorterDuff.Mode;
    import android.graphics.PorterDuffXfermode;
    import android.graphics.Rect;
    import android.graphics.RectF;
    import android.os.Bundle;
    import android.widget.ImageView;
    
    
    
    public class CircleImage extends Activity {
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.circle_layout);
        ImageView img1 = (ImageView) findViewById(R.id.imageView1);
        Bitmap bm = BitmapFactory.decodeResource(getResources(),
                R.drawable.hair_four);
        Bitmap resized = Bitmap.createScaledBitmap(bm, 100, 100, true);
        Bitmap conv_bm = getRoundedRectBitmap(resized, 100);
        img1.setImageBitmap(conv_bm);
        // TODO Auto-generated method stub
    }
    
    public static Bitmap getRoundedRectBitmap(Bitmap bitmap, int pixels) {
        Bitmap result = null;
        try {
            result = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(result);
    
            int color = 0xff424242;
            Paint paint = new Paint();
            Rect rect = new Rect(0, 0, 200, 200);
    
            paint.setAntiAlias(true);
            canvas.drawARGB(0, 0, 0, 0);
            paint.setColor(color);
            canvas.drawCircle(50, 50, 50, paint);
            paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
            canvas.drawBitmap(bitmap, rect, rect, paint);
    
        } catch (NullPointerException e) {
        } catch (OutOfMemoryError o) {
        }
        return result;
    }
    
     }
    
    0 讨论(0)
  • 2020-12-01 00:57

    Try this code:

    public static Bitmap getRoundedRectBitmap(Bitmap bitmap, int pixels) {
    try {
    result = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
    Bitmap.Config.ARGB_8888);
    canvas = new Canvas(result);
    
    color = 0xff424242;
    paint = new Paint();
    rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    rectF = new RectF(rect);
    roundPx = pixels;
    
    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
    
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    } catch (NullPointerException e) {
    // return bitmap;
    } catch (OutOfMemoryError o){}
    return result;
    }
    

    If you want an actual circle then you can pass 100px as parameter.

    0 讨论(0)
  • 2020-12-01 01:00

    Add following dependancies

     implementation 'jp.wasabeef:picasso-transformations:2.2.1'
     implementation 'de.hdodenhof:circleimageview:3.0.0'
    

    CircularImageView available for image fit in circle also if image is not looking proper .resize is for image resizing in circular image view.

        CircleImageView img;
        String Imageid; 
    
        Imageid="ImageName"; //String is not compulsary it may be drawable  
    
        Picasso.with(mContext)
                .load(Imageid.get(position)) //Load the image
                .error(R.drawable.ic_launcher_background) //Image resource for error
                .resize(20, 20)  // Post processing - Resizing the image
                .into(img); // View where image is loaded.
    
    0 讨论(0)
  • 2020-12-01 01:05

    Update

    There is a CircleImageView available on Github.

    You can get latest version from Maven repository as add as a gradle dependency.

    0 讨论(0)
提交回复
热议问题