I have a Picture object, loaded from an SVG file, and I have set hardwareAccelerated=false to make it works on all devices.
Since there is a bug on android 4.0.4, I have to convert the Picture to Bitmap and I do that, in this way:
...
...
public void draw(Canvas canvas) {
...
...
//myPicture size is 9000x5000 but I want to display only this portion
clipRect.set(50, 50, 370, 530);
Bitmap bmp = getBitmapFromPicture(myPicture, clipRect);
canvas.drawBitmap(bmp, 0, 0, null);
bmp.recycle();
...
...
}
public static Bitmap getBitmapFromPicture(Picture picture, RectF clipRect) {
Bitmap bitmap = Bitmap.createBitmap(Math.round(clipRect.width()), Math.round(clipRect.height()), Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
canvas.drawPicture(picture);
}
Now I want to clip the Picture because I want to display only the visible screen part of it.
But the canvas.drawPicture does not accept srcRect parameter.
How is it possible to achieve this?
EDIT:
By translate the canvas: canvas.translate(-50, -50) it seems that translate the bitmap, too.
You need to set a transform on the Canvas.
To move the portion of the picture at 50,50 down so it is on the bitmap (ie. at 0,0), just do:
canvas.translate(-50, -50);
So your method becomes:
public static Bitmap getBitmapFromPicture(Picture picture, RectF clipRect)
{
Bitmap bitmap = Bitmap.createBitmap(Math.round(clipRect.width()),
Math.round(clipRect.height()),
Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
canvas.translate(-clipRect.left, -clipRect.top);
canvas.drawPicture(picture);
}
Related
How to mask image such as we can scale/zoom/rotate the background image but not the mask?
I should be able to zoom the image in this mask but it is scaling whole image.
Suggest me a way to achieve this, I'm creating a photo collage app.
Blue color is background of Layout.
The white color is for mask
I'm able to achieve this type of layout with masking, but when I apply MultiTouchListener to scale and zoom, it will scale the whole image with mask and not the image inside it.
private void getMaskedBitmap() {
Bitmap bgBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.background_drawable);
ImageView bg = (ImageView) findViewById(R.id.bg);
bg.setImageBitmap(bgBitmap);
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
Bitmap emptyBitmap = Bitmap.createBitmap(bgBitmap.getWidth(), bgBitmap.getHeight(), conf);
Canvas canvasBmp = new Canvas(bgBitmap);
ImageView mImageView = (ImageView) findViewById(R.id.troll_face);
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);
bg.setOnTouchListener(new MultiTouchListener());
mImageView.invalidate();
}
Exception on line
Canvas canvasBmp = new Canvas(bgBitmap);
java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor
I cannot provide you code but here is what you should do:
Get background image as bitmap and draw it on view's canvas.
Create an new (empty) bitmap of same size (use Bitmap.Config.ARGB_8888) and get its canvas, here you will draw mask.
Canvas canvasBmp = new Canvas(bmp);
Now draw bmp on view's canvas. So, that you can get mask effect on image.
Zoom (re-size etc whatever you want) background bitmap image.
Invalidate your view, and follow all steps again
In my android app, I have this function that creates a new bitmap from an old one via canvas.
private static Bitmap convert(Bitmap bitmap, Bitmap.Config config, int width, int height) {
Bitmap convertedBitmap = Bitmap.createBitmap(width, height, config);
Canvas canvas = new Canvas(convertedBitmap);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
canvas.drawBitmap(bitmap, 0, 0, paint);
bitmap.recycle();
return convertedBitmap;
}
The problem is when I draw the old bitmap onto the canvas, since the old bitmap is bigger in dimensions than the canvas, only the top left part of the bitmap gets drawn on the canvas. Is there a way I can make it so when it draws the bitmap on the canvas, it scales the bitmap so it fits perfectly in the canvas?
I don't want to resize the bitmap using createScaledBitmap. Is there a faster way?
OR Can I do createScaledBitmap but make it mutable at the same time? That is what I am trying to do overall, resize and make mutable at same time.
Thanks
You can call public void drawBitmap (Bitmap bitmap, Rect src, RectF dst, Paint paint) for this function (Android Doc). The code below should accomplish what you are trying to do:
canvas.drawBitmap(bitmap, null, new RectF(0, 0, canvasWidth, canvasHeight), null);
I'm trying to write some text across a bitmap in Android. I've followed several guides and attempted several different code variations but I either get something obscure, a blank screen or the unaltered bitmap. This is the basic idea of what I've been playing around with.
public Bitmap writeOnDrawable(Context gContext, String path, String text) {
Bitmap bitmap = BitmapFactory.decodeFile(path);
Bitmap bmOverlay = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_4444);
Canvas canvas = new Canvas(bmOverlay);
Paint paint = new Paint();
paint.setColor(Color.CYAN);
paint.setTextSize(20);
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
canvas.drawBitmap(bitmap, 0, 0, null);
canvas.drawPoint(30, 50, paint);
canvas.drawText("Text", 33, 53, paint);
return bmOverlay;
}
I pass this function the variables and then use the returned bitmap by turning it into a BitmapDrawable and making it a background of a layout. This particular version of the function returns only my initial, unaltered bitmap with no "Text" text across it.
I've attempted variations such as copying the original image:
Bitmap drawableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
Drawing the original image onto the canvas:
canvas.drawBitmap(drawableBitmap, screenHeight, screenWidth, paint);
I solved the problem using android-maps-util library.
add compile 'com.google.maps.android:android-maps-utils:0.4' dependency to your gradle file.
Use IconGenerator to create bitmap with text and use it in your BitmapDescriptor to create marker on your map.
I want to display Canvas contents on ImageView in android,
but ImageView displaying blank.
Bitmap imgBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
Canvas canvas = new Canvas();
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
canvas.drawBitmap(imgBitmap, 0, 0, paint);
paint.setColor(Color.BLACK);
paint.setAlpha(100);
canvas.drawRect(0, 0, 100, 100, paint); // transparent black on image
imgView.draw(canvas);
what is the problem? and what should I do?
When ImageView.draw() is called its actually putting the contents of the ImageView into the provided canvas, you have it logically backwards here. Instead use Canvas(Bitmap) constructor instead (so your canvas will draw on to the bitmap) then ImageView.setImageBitmap() with the same bitmap given to the canvas. You can use Bitmap.createBitmap(int, int, Bitmap.Config) to create the size of Bitmap you want. And remember if you have the canvas draw outside the Bitmap's bounds it is clipped.
How can a Drawable loaded from a resource be rotated when it is drawn? For example, I would like to draw an arrow and be able to rotate it to face in different directions when it is drawn?
You need to use Bitmap and Canvas Class functions to prepare drawable:
Bitmap bmpOriginal = BitmapFactory.decodeResource(this.getResources(), R.drawable.image2);
Bitmap bmResult = Bitmap.createBitmap(bmpOriginal.getWidth(), bmpOriginal.getHeight(), Bitmap.Config.ARGB_8888);
Canvas tempCanvas = new Canvas(bmResult);
tempCanvas.rotate(90, bmpOriginal.getWidth()/2, bmpOriginal.getHeight()/2);
tempCanvas.drawBitmap(bmpOriginal, 0, 0, null);
mImageView.setImageBitmap(bmResult);
In this code sample rotation for 90 degrees over image center occurs.
essentially it can be boiled down to: do a(n inverse) canvas transformation instead of transforming drawable
private BitmapDrawable drawable; // or Drawable
protected void onDraw(Canvas canvas) { // inherited from View
//...
canvas.save();
canvas.rotate(degrees, pivotX, pivotY);
drawable.draw(canvas);
canvas.restore();
//...
}
if you have BitmapDrawable it may be desirable to increase quality of the output by setting antialiasing
drawable.setAntialias(true);
Accepted answer doesn't work for me. I have non square image, so I changed his code a bit.
private Bitmap rotateDrawable(#DrawableRes int resId) {
Bitmap bmpOriginal = BitmapFactory.decodeResource(getResources(), resId);
Bitmap bmpResult = Bitmap.createBitmap(bmpOriginal.getHeight(), bmpOriginal.getWidth(), Bitmap.Config.ARGB_8888);
Canvas tempCanvas = new Canvas(bmpResult);
int pivot = bmpOriginal.getHeight() / 2;
tempCanvas.rotate(90, pivot, pivot);
tempCanvas.drawBitmap(bmpOriginal, 0, 0, null);
return bmpResult;
}
mImageView.setImageBitmap(rotateDrawable(R.drawable.some_image));
essentially this:
ImageView spaceshipImage = (ImageView) findViewById(R.id.spaceshipImage);
Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump);
spaceshipImage.startAnimation(hyperspaceJumpAnimation);
source link:
http://developer.android.com/guide/topics/graphics/2d-graphics.html#tween-animation