I want to create the crop bitmap functionality and have referred Android: Free Croping of Image but this sets the bitmap in another imageView.Now I know I can set the cropped bitmap in the canvas but I want to retain the original bitmap and want to do the cropping in onDraw() and not make a different bitmap and then set it in canvas.I have tried to implement the below code in onDraw but there is no cropping and bitmap remains as it is.Pleas help so that I can code this in onDraw() method of the CustomView.
compositeImageView = (ImageView) findViewById(R.id.our_imageview);
Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(),
R.drawable.gallery_12);
Bitmap resultingImage = Bitmap.createBitmap(widthOfscreen,
heightOfScreen, bitmap2.getConfig());
Canvas canvas = new Canvas(resultingImage);
Paint paint = new Paint();
paint.setAntiAlias(true);
Path path = new Path();
for (int i = 0; i < SomeView.points.size(); i++) {
path.lineTo(SomeView.points.get(i).x, SomeView.points.get(i).y);
}
canvas.drawPath(path, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap2, 0, 0, paint);
compositeImageView.setImageBitmap(resultingImage);
my onDraw() method
@Override
protected void onDraw(Canvas canvas)
{
Bitmap bitmap1 = bitmap.copy(bitmap.getConfig(), true);
canvas.drawBitmap(bitmap1, 0,0, null);
Paint paint = new Paint();
paint.setAntiAlias(true);
Path path = new Path();
for (int i = 0; i < SomeView.points.size(); i++) {
path.lineTo(SomeView.points.get(i).x, SomeView.points.get(i).y);
}
canvas.drawPath(path, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap,0, 0, paint);
}
Canvas#drawPath
with thePaint
object which has set theBitmapShader
, no need forBitmap#copy
, xfer modes andCanvas#drawBitmap
– EarthlyonDraw
will contain only one line of code:canvas.drawPath(path, paint);
everything else (setting theShader
,Path
etc) can be done in your customView
constructor – Earthly