I am trying to get a bitmap from a canvas by the following functionality..But I am getting the bitmap width and height -1.....Can't make out why is it happening...I have attached the code snippet below...Please help!!
Thanks in advance :)
`private Bitmap createBitmapFromCanvas() {
Bitmap bitmap = Bitmap.createBitmap(panelWidth, panelHeight,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas();
canvas.setBitmap(bitmap);
canvas.drawRect(new Rect(0,0, panelWidth, panelHeight), new Paint());
for (PathListForDraw points : finalPathList) {
canvas.drawPath(points.getmPath(), points.getmPaint());
// canvas.save();
}
return bitmap;
}`
Related
Bitmap bitmap = Bitmap.createBitmap(canvasLL.getawidth(), canvasLL.getHeight(), Bitmap.Config.RGB_5);
Canvas canvas = new Canvas(bitmap)
View.draw(canvas);
This how i had create a bitmap using the things draw on canvas. But i want to check that if something draw or not on the canvas. Any help will be really appreciated.
Bitmap.getGenerationId() can be used to check if a bitmap has been modified.
Bitmap bitmap = Bitmap.createBitmap(canvasLL.getWidth(), canvasLL.getHeight(), Bitmap.Config.RGB_565);
final int oldBitmapId = bitmap.getGenerationId();
Canvas canvas = new Canvas(bitmap);
View.draw(canvas);
final int newBitmapId = bitmap.getGenerationId();
if (oldBitmapId != newBitmapId) {
// a Bitmap has been changed
}
I'm having this code:
public void saveimage() {
Bitmap bitmap = Bitmap.createBitmap(colorGFX.getWidth(),
colorGFX.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.WHITE);
canvas = colorGFX.canvas;
canvas.setBitmap(bitmap);
saveBitmap(bitmap);
}
colorGFX object extends a SurfaceView.
saveBitmap actually writes the image on the file.
The problem is that most of the times a WHITE image is saved, other times the correct image is saved.
Did I missed something, or why does the image saves only let's say 1/5 of the times?
This worked for me:
Bitmap bitmap = Bitmap.createBitmap(colorGFX.getWidth(),
colorGFX.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.WHITE);
canvas.drawBitmap(colorGFX.bitmap, 0f, 0f, null);
canvas.drawBitmap(colorGFX.pictureBitmap, 0f, 0f, null);
saveBitmap(bitmap);
Try this:
public void saveimage() {
Bitmap bitmap = Bitmap.createBitmap(colorGFX.getWidth(),
colorGFX.getHeight(), Bitmap.Config.ARGB_8888);
colorGFX.draw(new Canvas(bitmap))
saveBitmap(bitmap);
}
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);
}
I have a bitmap on which I'm trying to write a text using canvas.After setting the bitmap in a canvas and doing the necessary operations(writting a text on the canvas), I draw the resulting canvas on a ImageView.The big problem is that there is no image displayed...the screen turns black.Now, I know that the bitmap is returned ok because I displayed right before doing the canvas operations.
So,here is how I did it:
image= (ImageView) findViewById(R.id.imageview);
bitmap = android.provider.MediaStore.Images.Media
.getBitmap(cr, selectedImage);
int heightOfOld=bitmap.getHeight();
int widthOfOld=bitmap.getWidth();
android.graphics.Bitmap.Config hasAlpha=bitmap.getConfig();
Bitmap bitmapResult=bitmap.createBitmap(widthOfOld, heightOfOld, hasAlpha);
Canvas c=new Canvas(bitmapResult);
Canvas c1=drawTextImage(c);
image.draw(c1);
And here is the method used for drawing text on the canvas:
private Canvas drawTextImage(Canvas c){
Paint paint=new Paint();
paint.setColor(Color.BLUE);
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
paint.setTextSize(20);
c.drawText("Golden Stag", 30, 200, paint);
return c;
}
Could someone tell me where is the problem,please!?
Since the documentation for draw(Canvas) says:
"Manually render this view (and all of its children) to the given Canvas."
Maybe try with:
image.setImageBitmap(bitmapResult);
"Sets a Bitmap as the content of this ImageView."
Update: Example
I think this should work (can't test it though):
image = (ImageView) findViewById(R.id.imageview);
bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, selectedImage);
Canvas c = new Canvas(bitmap);
drawTextImage(bitmap);
image.setImageBitmap(bitmap);
private Canvas drawTextImage(Bitmap b){
Canvas c = new Canvas(b);
Paint paint = new Paint();
paint.setColor(Color.BLUE);
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
paint.setTextSize(20);
c.drawText("Golden Stag", 30, 200, paint);
}
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