Here is sample code to crop image as par background shape. You'll need two drawable, one for background and one for masking.
ImageView mImageView = (ImageView) findViewById(R.id.myImageView); Bitmap original = BitmapFactory.decodeResource(getResources(), R.drawable.random_drawable); Bitmap mask = BitmapFactory.decodeResource(getResources(), R.drawable.mask_drawable); original = Bitmap.createScaledBitmap(original, mask.getWidth(), mask.getHeight(), true); Bitmap result = Bitmap.createBitmap(mask.getWidth(), mask.getHeight(), Config.ARGB_8888); Canvas mCanvas = new Canvas(result); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); mCanvas.drawBitmap(original, 0, 0, null); mCanvas.drawBitmap(mask, 0, 0, paint); paint.setXfermode(null); mImageView.setImageBitmap(result); mImageView.setScaleType(ScaleType.FIT_XY); mImageView.setBackgroundResource(R.drawable.background_drawable);
thanks
ReplyDelete