How to draw Bitmap from ARGB_8888 Bitmap - android

I am drawing paths on canvas and creating Transparent bitmap while saving and creating cropping bitmap from transparent bitmap.
See Images :
In this I mage I am drawing path on canvas and I am creating transparent bitmap and according to startX,lowestY and highestX,highestY
Bitmap cropBitmap =Bitmap.createBitmap(sourceBitmap,startX,lowestY,highestX,highestY);
When I am cropping Bitmap I want Only "Test" drawing crop bitmap.But it's giving empty bitmap. Like this
Inside red box I want cropped bitmap from transparent bitmap whatever I draw on canvas.

You cannot simply create a transparent bitmap, and assume that whatever is within the bounds of the bitmap has become a part of it meaning that the pixels are identical. There are two problems with your assumption, number 1, a transparent bitmap won't help because transparency serves as a see through bitmap which only will have alpha value, number 2, the data that is drawn with the path, has no correlation to the data of your bitmap, you initially created a bitmap with no color, except that it's completely transparent.
Here's the general code for the correct way how to achieve such a task:
class myDrawingView extends View() {
// all your class members you initialize here
#Override
public void onDraw(Canvas canvas) {
// get your width and height using startx, starty, highestx, highesty, lowesty
Bitmap buffer = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
canvas.setBitmap(buffer);
canvas.drawPath(//draw your path here);
invalidate();
}
The Bitmap called buffer, now is the frame buffer for all your drawing so you can draw a path and that will be rendered on the buffer you defined. And you see that no transparency was used we just simply created a bitmap with no pixels assigned to it at first, once you draw to it via the canvas, your bitmaps pixels will be whatever you drew.

Related

Android change bitmap transparency on canvas

I want to be able to slowly make a bitmap image more and more transparent on my canvas.
Currently I am drawing the .png file (stored in drawables) like this:
//I setup the Bitmap in my constructor.
heartSymb = BitmapFactory.decodeResource(getResources(),R.drawable.heartsymbol);
//This is in on draw.
canvas.drawBitmap(heartSymb,0,0,null);
How would I be able to slowly change the bitmap's transparency until it becomes fully transparent?
You can use a Paint object to modify the alpha of the bitmap to be drawn:
Paint alphaPaint = new Paint();
alphaPaint.setAlpha(alpha);
canvas.drawBitmap(heartSymb, 0, 0, alphaPaint);
Then you just have to modify alpha value and perform update periodically, maybe by using a Handler.

Android: Canvas drawBitmap?

I have a 200x200px bitmap. I want to draw the top left 50x50px corner of my bitmap, on my canvas at coordinates 100,100 with a width and height of 50px, by using:
drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint)
This is what I've tried:
drawBitmap(myBitmap, new Rect(0,0,50,50), new Rect(100,100,150,150) , null);
What am I doing wrong?
From developer.android.com:
Parameters
bitmap The bitmap to be drawn
src May be null. The subset of the bitmap to be drawn
dst The rectangle that the bitmap will be scaled/translated to fit into
paint May be null. The paint used to draw the bitmap
What is missing in my code?
Thanks!
You need to change your rectangles. This is because, as described in the documentation, the first rectangle is the subset of the bitmap you want to draw, the second is the scaling/translating so basically the size of the destination draw (50x50)
So it should look like this:
drawBitmap(myBitmap, new Rect(100,100,150,150), new Rect(0,0,50,50) , null);

Draw on a large canvas, scroll it, save it, reload it ... works, but only the viewport part is saved

My program can draw 2D shapes on a canvas. The result can be scrolled. , When I save the result, then ONLY the image in the viewport is saved.
I use a View and draw on it's canvas.
Saving the drawing/updated result is:
setDrawingCacheEnabled(true);
buildDrawingCache();
Bitmap bmp = Bitmap.createBitmap( this.getDrawingCache());
setDrawingCacheEnabled( false);
// the bitmap I can save into a file
bm.compress( Bitmap.CompressFormat.PNG, 90, new FileOutputStream( new File( filepath)));
When I use a large view/canvas, then I get errors that my drawingCache is not big enough. When I use a regualar view/canvas size, then nearly all drawings outside the viewport are clipped.
==> Can I draw on a bitmap and have that bitmap immediately drawn on the view's canvas?
So, after some drawing, the initial bitmap is updated with the newly added drawing?
Happily, there is a simple solution to this question.
Create a large bitmap with your own size;
Create your own cachedCanvas = new Canvas( largeBitmap);
Draw first on the cachedCanvas'.
In onDraw() do: canvas.drawBitmap( cachedBitmap, 0, 0, null);
Saving your own largeBitmap is easy!

Android: drawRect without antialiasing

I draw a rectangle on a bitmap and its borders are antialiased or have some effects as you can see:
original image:
How to switch off that effect to get solid lines like this:
I use this code:
Bitmap mBuffer = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
gfx = new Canvas(mBuffer);
Paint paintNorm = new Paint();
paintNorm.setAntiAlias(false);
paintNorm.setStrokeWidth(1);
paintNorm.setStyle(Paint.Style.STROKE);
paintNorm.clearShadowLayer();
paintNorm.setDither(false);
paintNorm.setFilterBitmap(false);
paintNorm.setColor(0xFFA8A8A8);
gfx.drawRect(2,3,50,50, paintNorm);
My impression is that your bitmap is correctly drawn without anti-aliasing, but the blurry effect may come from the magnification when displaying the bitmap. Your screenshot is 245 pixels wide, but your code seems to indicate that the rectangle should be about 50 pixels or so.

Android Canvas draw a multiple frame Image

I have an image with different frame to be displayed, like the following:
As you can see that image has three frames, the full heart, half heart and empty space.
Now i need to only one frame of the three. I'm just wondering if there is a method to do that using a single gif in android sdk.
For example in several language there is a method like:
canvas.drawImage(xpos,ypos,xwidth,xheight,gifx,gify,gifwidth,gifheight)
where gifx,gify,gifwidth,gifheight are the coordinates and size of the selected frame.
Ok i answer myself, i just found a solution.
In order to draw a multiframe image the following method can be used:
public void drawBitmap (Bitmap bitmap, Rect src, RectF dst, Paint paint)
Where:
Bitmap bitamp is the Bitmap resource obtained with BitmapFactory.decodeResource (or via your preferred method).
Rect src is the frame to be shown (it can be null)
Rect dst rhe rectangle that the bitmap will be scaled/translated to fit into (it can be null)
Paint paint used to draw the bitmap (it can be null)
Another way (that i didn't tested) could be using BitmapRegionDecoder first of all a new instance of the object must be created using BitmapRegionDecoder.newInstance(...)
and then the selected region of bitmap to be shown could be obtained with the method:
public Bitmap decodeRegion (Rect rect, BitmapFactory.Options options)
Where rect is the selected region to be shown. For more info on BitmapRegionDecoder:
http://developer.android.com/reference/android/graphics/BitmapRegionDecoder.html

Categories

Resources