Android draw multiple bitmaps next to each other - java

I need to customize an ImageView so that several images are displayed next to each other like This:
[IMG][IMG]
My original code looks like this:
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(img1, 10, 10, null);
canvas.drawBitmap(img2, fourtyfive.getScaledWidth(canvas)+10, 10, null);
}
What I'm trying to do is place the first image, get it's scaled device-specific width then place the second image at an offset based on the width the first.
Any suggestions?

Related

Programmatically draw vector drawable / SVG on to screen

I'm trying to programmatically draw an SVG on to the screen. The code below is what I've tried, when I run the program nothing shows up.
public MainView(Context context) {
hexagon = AppCompatResources.getDrawable(context,R.drawable.hex_svg);
}
#Override
protected void onDraw(Canvas canvas) {
hexagon.setBounds(30,30,30,30);
hexagon.draw(canvas);
}
The setBounds(int left, int top, int right, int bottom) method takes the positions of the bounding box of the drawable - you've set them all to 30 so you're setting the drawable's width and height to zero.
If you want the image to be at 30x,30y with a width and height of 30 then call setBounds(30, 30, 60, 60).
Also I'd call this in the onLayout method rather than in onDraw.

ImageView spread out animation

I basically have an ImageView which got modified with Canvas and looks like a cirlce. (original image had the dimensions of a square (500x500))
Image what the starting position looks like:
http://imgur.com/bvXdLoP
(red is transparent and got removed with help of the Canvas method)
The animation should take aroud 1000 miliseconds and during this time step for step restore to the original picture. So in the end there should be a sqaure again.
In other words the cut off corners, which are the differnce between a square and a circle (and red marked in the image), get step for step restored in around 1000 milliseconds, with a sort of spreading looking animation.
Don't really have any clue on how to achieve this, so I can just share the Canvas method I used to cut off the corners (if it helps :X):
private Bitmap createCircleImage(Bitmap bitmap) {
Bitmap bmp;
bmp = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Bitmap.Config.ARGB_8888);
BitmapShader shader = new BitmapShader(bitmap,
BitmapShader.TileMode.CLAMP,
BitmapShader.TileMode.CLAMP);
float radius = bitmap.getWidth() / 2f;
Canvas canvas = new Canvas(bmp);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setShader(shader);
canvas.drawCircle(bitmap.getWidth()/2,bitmap.getHeight()/2,bitmap.getHeight()/2, paint);
return bmp;
}
Appreciate any help,
thank you!
There are many ways to achieve such behavior. For example you can create custom view and override its onDraw method such as
#Override
public void onDraw(Canvas canvas) {
canvas.drawCircle(viewWidth/2, viewHeight/2, currentRadius, paint);
}
and then you can just increase the radius little by little and invalidate the view.Since view is by default will clip on its clipBounds (you can only draw inside viewidth x viewheight rectangle unless you set it otherwise) you will get the effect that you want. And then you can tweak the interpolation to achieve smoother and more natural animation.
note: a bitmap shader is attached to the paint (you already know chow to create it). I don't include it in the code, since you shouldn't initialize it inside onDraw method for performance reason.

canvas bitmap animations without using a sprite sheet

Bitmap[] planeFrames = new Bitmap[4];
protected void onDraw(Canvas canvas) {
for(int i = 0 ; i < planeFrames.length;i++)
canvas.drawBitmap(planeFrames[i], plane.getCenterX(), 0, null); // planeFrames is an array of Bitmaps
}
Im trying to animate a plane by just swapping images but its not working I don't know if my method is to simple to work with
You can not animate this way. onDraw is called per draw iteration meaning that whatever you draw on the Canvas at the return is what's going to be drawn on the screen. Basically what you're doing here is drawing all the Bitmaps on the Canvas at the same spot and they will all be plastered on top of each other.
What you need to do is call the invalidate() method of the View. To make things more efficient, you can use a Rect or define the areas around the Bitmaps.
#Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(planeFrames[i], plane.getCenterX(), 0, null);
if(++i >= planeFrames.length) {
i = 0;
}
invalidate(bmpRect);
}
This will consistently draw through the bitmaps while repeatedly drawing the new bitmaps. This will last forever as long as the View is on screen. bmpRect is a Rect that has the coordinates of the Bitmap.

ColorSplash effect tutorial for java or android

I'm trying to make a colorsplash effect in android. A grayscale image that can be colored with a brush and also paint it again to a grayscale with other brush.
I managed to paint the colors using setXfermode on the canvas but I'm not able to paint the grayscale again.
I'm looking for a tutorial or a simple example of it. I found lots of android apps that can do it but nothing related on how they did it.
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (isBlackAndWhite) {
//draw background
canvas.drawBitmap(bgr, 0, 0, null);
//copy the default overlay into temporary overlay and punch a hole in it
//c2.drawBitmap(overlayDefault, 0, 0, null); //exclude this line to show all as you draw
if (isErase) {
c2.drawCircle(X, Y, 80, mPaintTransparent);
} else {
c2.drawCircle(X, Y, 80, mPaintNormal);
}
//draw the overlay over the background
canvas.drawBitmap(overlay, 0, 0, null);
}
}
So my background is the colored image and the overlay is the grayscale filtered image
mPainttransparent is as Paint object with
setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR))
My problem now is that I can't paint it back to normal

Change the size of a customized ProgressBar

I created a CustomBar which extends ProgressBar, it has various methods to customize the appearance. One of this Functions adds a Icon to the progressing part. Since it's bigger that the ProgressBar it gets cropped, which I do not want.
So I tried changing the size of the ProgressBar when the Icon is added.
like this:
ViewGroup.LayoutParams params = getLayoutParams();
params.height = bitmap.getHeight();
setLayoutParams(params);
requestLayout();
This doesn't solve the problem, so I tried changing the size of the Drawable.
It's a LayerDrawable, I tried, setBounds, setLayerInsets, changing the first Layer to a InsetDrawable with padding to get the size I wanted. All to no avail.
So what am I missing? How to properly change the size of a progressBar at runtime?
Found the answer myself, you have to overwrite the bounds, but also for the Canvas.
#Override
protected synchronized void onDraw(Canvas canvas)
{
if(bitmap != null && bitmap.getHeight() > getHeight())
{
canvas.clipRect(0, 0, getWidth(), bitmap.getHeight(), Region.Op.REPLACE);
bounds = getProgressDrawable().getBounds();
bounds.bottom = bitmap.getHeight();
}
doSomething();
}
Important is the Region.Op.REPLACE flag, normally clipping just gets intersected with the Bounds of the Canvas.

Categories

Resources