ImageView rounded corners

后端 未结 17 2046
长发绾君心
长发绾君心 2020-11-27 15:33

I wanted image to have rounded corners. I implement this xml code and use this in my image view. but image overlap the shape. I am downloading the image through async task.<

17条回答
  •  生来不讨喜
    2020-11-27 16:29

    Try like this if you need to get rounded image ..

    Your MainActivity.java class

    public class MainActivity extends Activity {
        private static final int REQUEST_CODE = 1;
        private Bitmap bitmap;
        private ImageView imageView;
        Bitmap roundedBitmapImage;
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            imageView = (ImageView) findViewById(R.id.result);
    
        }
    
        public void pickImage(View View) {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            startActivityForResult(intent, REQUEST_CODE);
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
    
    
                try {
                    // We need to recycle unused bitmaps
                    if (bitmap != null) {
                        bitmap.recycle();
                    }
                    InputStream stream = getContentResolver().openInputStream(
                            data.getData());
                    bitmap = BitmapFactory.decodeStream(stream);
                    stream.close();
    
                   /* Bitmap bitmap1=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
                    Bitmap bitmap2=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
    
                    Bitmap resultingImage=Bitmap.createBitmap(320, 480, bitmap1.getConfig());
    
                    Canvas canvas = new Canvas(resultingImage);
    
                    Paint paint = new Paint();
                    paint.setAntiAlias(true);
                    Path path=new Path();
                    path.lineTo(150, 0);
                    path.lineTo(230, 120);
                    path.lineTo(70, 120);
                    path.lineTo(150, 0);
    
                    canvas.drawPath(path, paint);
    
                    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
                    canvas.drawBitmap(bitmap2, 0, 0, paint);*/
    
                   //-------> compositeImageView.setImageBitmap(resultingImage);
    
                    // Use this when to provide any shape to image i.e Fit image to any shape.
                    // under mentioned taking reference from Rounder class. Finally changing image in round shape.
                    // Here we are passing reference  bitmap.
                    roundedBitmapImage = new  Rounder().getRoundedShape(bitmap);
    
                    imageView.setImageBitmap(roundedBitmapImage);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
    

    And your rounder class ..

    /** This class crops image to round shape */

    public class Rounder {
        public Bitmap getRoundedShape(Bitmap scaleBitmapImage) {
            int targetWidth = 125;
            int targetHeight = 125;
            Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight,
                    Bitmap.Config.ARGB_8888);
    
            Canvas canvas = new Canvas(targetBitmap);
    
            Path path = new Path();
    
            path.addCircle(((float) targetWidth - 1) / 2,
                    ((float) targetHeight - 1) / 2,
                    (Math.min(((float) targetWidth), ((float) targetHeight)) / 2),
                    Path.Direction.CCW);
    
            canvas.clipPath(path);
    
            Bitmap sourceBitmap = scaleBitmapImage;
    
            canvas.drawBitmap(sourceBitmap, new Rect(0, 0, sourceBitmap.getWidth(),
                    sourceBitmap.getHeight()), new Rect(0, 0, targetWidth,
                    targetHeight), null);
    
            return targetBitmap;
        }
    }
    

提交回复
热议问题