VectorDrawable: How to resize the vectordrawable dynamically - android

I am using a custom view in which I have a rectangle and need to draw a VectorDrawable between the bounds.
I need to resize the size of the vectorDrawable on the go.
I am already using canvas to set the outer rectangle, so I cannot create another canvas using a bitmap.
This is what I am doing inside onDraw():
canvas!!.drawRect(mRect, mRadius!!,mRadius!!, mPaint)
vectorDrawable.mutate()
vectorDrawable.setBounds(mRect)
vectorDrawable.draw(canvas)
If I try to create a bitmap and create a new canvas object:
var bmp = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(),
vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888)
var can : Canvas = Canvas(bmp)
Then my vectorDrawable is not seen.
Please let me know the best way to resize the vectorDrawable.

Related

How to add rainbow gradient to an Image in android

I want to add Transparent Rainbow or Spectrum Gradient to Bitmap.
In photoshop it is very easy. Now I want to do it programmatically. I have gone through a lot of research. But gradient has only one starting color and one ending. How could I add the rainbow color(multiple color) Gradient in Android.
Or is there a way to add that effect to bitmap in Android? Like given
for this you can use a translucent image with the desired gradient and put it on top of your image view in a FrameLayout. then capture bitmap of the view.
once you get your layout ready use this answer to capture bitmap from it
try this to convert a view (framelayout) into a bitmap:
public Bitmap viewToBitmap(View view) {
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}

How to Crop Image in Hexagon,Octagon and in circle shape in Android?

I want to crop shape in Hexagon,Octagon and in circle shape.I have used Custom Image view class and use in xml for showing image. It works properly for different shapes.
Now i need to crop the image in user selected shape in next activity.i get image in next activity.
I tried this example:
Masking(crop) image in frame But the image didn't fit in shape.only part of the image get masked.
How i can achieve this?
Read this blog post by Romain Guy on how to make images with rounded corners. Using this technique you can create a variety of shapes.
How would this work?
If you have a predefined number of shapes, I'd suggest you create a class with a few Shapes defined by Paths. Now whenever your user asks for a different shape to be used on an ImageView, you create a Bitmap and return a bitmap from that class and put it on your ImageView.
For example a octagon shape would be something like this:
public static Bitmap drawOctagonShapedBitmap(Bitmap src) {
Bitmap dst = Bitmap.createBitmap(src.getWidth(), src.getHeight(), Bitmap.Config.ARGB_8888);
//Create an output as big as the actual bitmap.
Path octagon = null; //Create an octagon shape, it should be big enough to crop enough of the bitmap.
Canvas canvas = new Canvas(dst);
Paint mPaint = new Paint();
BitmapShader mBitmapShader = new BitmapShader(src, Shader.TileMode.CLAMP,
Shader.TileMode.CLAMP);
mPaint.setAntiAlias(true);
mPaint.setShader(mBitmapShader);
canvas.drawPath(octagon, mPaint);
return dst;
}

How to copy one canvas into another canvas?

I have drawn some rects into a canvas and I want to copy those rects into another canvas.The commonly discussed solution of using bitmaps will not work because I didn't use a bitmap in this canvas, no images only those rects.
How do I do that?
Have you tried saving the attributes given to the rectangles upon drawing to the first canvas, and then redrawing them on the second canvas based on these attributes? Perhaps you could try creating a Rect-object for the first canvas and passing it to Canvas.drawRect() on the second canvas?
You can create a bitmap using a canvas and you draw on that bitmap.
bitmap = Bitmap.createBitmap(50,50,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
// drawn some rects into canvas
Now you can use the bitmap to draw on any canvas you want;
Canvas canvas1 = new Canvas(bitmap);
Canvas canvas2 = new Canvas(bitmap);
Or if you have a canvas already:
canvas.drawBitmap(bitmap, 0, 0, paint);
This way you draw rects only once and not on each canvas.

Android creating a circle bitmap without drawing it on canvas

My question is: is there any way to create a circle and then make it bitmap and not to display it on canvas??For example make a black circle then convert it to a bitmap but not using the canvas.setBitmap(Bitmap bitmap).Thanks in advance.
You can easily get a bitmap object using Bitmap static method (Bitmap.create) and then use that object to draw anything on it by instantiating a canvas object with bitmapObject.
your code should look something like this:
Bitmap bitmap = Bitmap.create(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
//draw on your canvas.

How to add Border around Bitmap

I would like to merge a "drawRectF-like thing" with a Bitmap OR simple apply a border to a Bitmap in some way.
I don't want something like this:
Canvas.drawRectF(bitmap.x, bitmap.y, bitmap.x1, bitmap.y1)
Canvas.draw(bitmap)
I want to be able to apply the border to the Bitmap only once, then when I simply call Canvas.draw(bitmap) the border will be there around the bitmap.
It is preferable that the border has rounded corners.
First create a new canvas with the specified bitmap to draw into:
Canvas canvas1 = new Canvas(bitmap);
Then draw the border using this canvas:
canvas1.drawRectF(x, y, x1, y1);
Finally draw the bitmap to the first canvas.
canvas.draw(bitmap);
Note: For this to work, the bitmap must be mutable.

Categories

Resources