Android custom view: Set canvas size to wrap bitmap - android

I have a custom view that has Canvas in it. I'm using this canvas to show a bitmap on it and then I can draw over the bitmap on touch. When I load the bitmap it is a lot bigger then the view size and I can't see the whole bitmap (it's picture taken with camera). I tried to create scaled bitmap and then draw it on the canvas, but in that case the bitmap is smaller and the canvas is taking the whole layout space available. I'm adding this view programmatically, not int the xml layout. I have set this to the view but not working:
fdvImage = new ImageEditingView(this, b);
RelativeLayout.LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.CENTER_IN_PARENT, 1);
fdvImage.setLayoutParams(lp);
and this is my view constructor:
public ImageEditingView(Context c, Bitmap b) {
super(c);
this.setMinimumHeight(0);
mBitmap = Bitmap.createScaledBitmap(b, b.getWidth()/2, b.getHeight()/2, true);
mCanvas = new Canvas(mBitmap);
}
and in the onDraw I have this:
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
}
So, my question is: Why is the canvas taking all the layout space even if the bitmap is smaller then it?

Use this code to draw bitmap
canvas.drawBitmapMesh(yourBitmap, 1, 1,vertices, 0, null, 0,
paint);
Make vertices according to your view
[EDIT]
float[] vertices = new float[8];
vertices[0]=0; vertices[1]=0; vertices[2]=100; vertices[3]=0;
vertices[4]=0;vertices[5]=200; vertices[6]=100; vertices[7]=200;
change the 100 to your width and 200 to height

Related

Android View.draw(bitmap) saving view to bitmap does not render BlurMaskFilter properly

I am trying to programmatically create a gray Rect and black Rect with BlurMaskFilter layer (drop shadow effect) by overriding onDraw in a custom View. I am able to get it to draw on screen without any issues, but when I try to draw the view to a bitmap the results are different. In drawing to bitmap, it appears BlueMaskFilter is applied to the View rather than the specific layer of the black Rect.
What am I missing to make the drawn bitmap same as the output drawn on screen?
CustomView's onDraw override:
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// draw black box (shadow) first
RectF rectShadow = new RectF();
rectShadow.set(10, 10, 120, 120);
Paint shadowPaint = new Paint();
shadowPaint.setMaskFilter(new BlurMaskFilter(5, Blur.NORMAL));
shadowPaint.setStyle(Paint.Style.FILL);
shadowPaint.setColor(Color.DKGRAY);
shadowPaint.setAntiAlias(true);
canvas.drawRect(rectShadow, shadowPaint);
setLayerType(View.LAYER_TYPE_SOFTWARE, shadowPaint);
// draw grey box
RectF rectGray = new RectF();
rectGray.set(0, 0, 100, 100);
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.parseColor("#656565"));
paint.setAntiAlias(true);
canvas.drawRoundRect(rectGray, paint);
}
Function to save view to bitmap:
public void saveViewToBitmap( View customView int outWidth, int outHeight ) {
customView.setLayoutParams(new ViewGroup.LayoutParams(outWidth, outHeight));
customView.measure(
ViewGroup.MeasureSpec.makeMeasureSpec(customView.getLayoutParams().width,
ViewGroup.MeasureSpec.EXACTLY),
ViewGroup.MeasureSpec.makeMeasureSpec(customView.getLayoutParams().height,
ViewGroup.MeasureSpec.EXACTLY));
customView.layout(0, 0,
customView.getMeasuredWidth(), customView.getMeasuredHeight());
customView.requestLayout();
Bitmap outputBitmap = Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(outputBitmap);
customView.draw(canvas);
return outputBitmap;
}
Note in the following images the screen image has crisp edges for the gray rect, whereas the drawn bitmap has the top and left edges of the gray rect blurred demonstrating the problem.
Image on screen:http://i.stack.imgur.com/0Hl0t.png
Image saved from bitmap created by saveViewToBitmap: http://i.stack.imgur.com/xNzi5.png
Thanks!

Android clip Picture to Bitmap. Is Possible?

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);
}

How to draw a bitmap on a canvas, respecting alpha values of the bitmap?

background
i have a master bitmap that i need to draw on it other bitmaps.
the master bitmap has some semi-transparent pixels (pixels with variant values for the alpha channel) , so that the other bitmaps that are drawn on it should be merged with it instead of overriding the colors completely.
the question
how can i set the canvas to draw the bitmaps on the master bitmap with respect to the semi-transparent pixels ?
note: the alpha is not for the whole bitmap/s . it's per pixel.
Canvas.setXfermode(Xfermode xfermode). There are a number of Xfermodes you can choose.
public void putOver(Bitmap master, Bitmap alphaBitmap){
Canvas canvas = new Canvas(matter);
Paint paint = new Paint();
paint.setXferMode(new PorterDuffXfermode(PorterDuff.Mode.DST_OVER));
canvas.drawBitmap(left, top, left+alphaBitmap.width, left+alphaBitmap.height, paint);
}
public Bitmap PutoverChange(Bitmap all, Bitmap scaledBorder) {
Paint paint = new Paint();
final int width = change.getWidth();
final int height = change.getHeight();
patt = Bitmap.createScaledBitmap(change, width, height, true);
Bitmap mutableBitmap = patt.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(mutableBitmap);
scaledBorder = Bitmap.createScaledBitmap(border, width, height, true);
paint.setAlpha(100);
canvas.drawBitmap(scaledBorder, 0, 0, paint);
return mutableBitmap;
}
here the transparency is 100. you can modify it to 50 so it becomes semi transparent.

canvas draw TextView rotation, placement

I have this code:
public class ZeichenView extends ImageView implements OnTouchListener{
#TargetApi(11)
public final Bitmap getScreenCopy() {
measure(getWidth(), getHeight());
layout(0, 0, getWidth(), getHeight());
Bitmap bitmap = Bitmap.createBitmap(
getWidth(),
getHeight(),
Bitmap.Config.ARGB_8888
);
Canvas temporaryCanvas = new Canvas(bitmap);
bildflaeche.draw(temporaryCanvas);
messAusgabe.draw(temporaryCanvas);
referenzAusgabe.draw(temporaryCanvas);
draw(temporaryCanvas);
return bitmap;
}}
it draw all elements on one canvas and return one bitmap - but the TextView properties like: rotation and placement is wrong (not) drawed. Why not. The property: text is right.
referenzAusgabe and messAusgabe are TextViews. bildflaeche is a ImageView. I put all on one canvas to have one bitmap after that.
The user changes the rotation and placement of the TextView's so I don't know before where they will be. It would be very nice if you can help me.
maybe try to use:
temporaryCanvas.drawtext()
and give it the coordinate u want.
also for rotating u can save() -> rotate the canvas -> load()

Canvas to ImageView problem in Android

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.

Categories

Resources