Android - how do i animate a shape? - java

I need a little start-up aid. I want to animate a circle, that is 'flying' vertically, when the user swipes crabwise. What do i have to do, after creating a circle on a canvas?
Bitmap bg = Bitmap.createBitmap(480, 800, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas (bg);
canvas.drawCircle(canvas.getWidth()/2, (float) (canvas.getHeight()/1.8), 13, paint);

You can use Android Animation like in this tutorial, or "Canvas" animation like in that.

Related

Drawing on camera frame continuously by using camera2 api?

I am trying to draw on the surface view using the Camera2 API sample code from Google. But for some reason, the drawing disappears after the camera preview starts. I am really confused as to why this is happening.
private void drawonSurfaceView(){
Paint paint = new Paint();
SurfaceHolder surfaceHolder= mOverlay.getHolder();
if(surfaceHolder.getSurface().isValid()) {
Canvas canvas = surfaceHolder.lockCanvas();
//... actual drawing on canvas
paint.setStyle(Paint.Style.FILL);
int x = canvas.getWidth();
int y = canvas.getHeight();
paint.setColor(Color.GREEN);
paint.setStyle(Paint.Style.STROKE);
Rect rect = new Rect(50, 50, x-300, y-300);
canvas.drawRect(rect, paint );
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
I Have placed the code inside the setUpCameraOutputs() method.
That's true, you cannot lock the canvas of a preview surface for drawing. Some while ago, the SurfaceView used for camera preview required special settings (see What does SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS means?), but since API 11, this happens automagically.
You can always draw on a transparent view that is above the camera preview, but this is not the recommended solution. Much better, use a SurfaceTexture for your camera live stream, and draw with OpenGL in the 2D virtual world that includes that texture. To have your drawings synchronized with the camera stream, you should hide the preview texture and draw the camera frames yourself using the same callback that you use to feed the image processing algorithm.

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.

Android Finger Painting stretch on rotate

I am making a finger painting app. Using Paths I have created a canvas for users to draw lines on as if finger painting. I have saved the paths and reload them when the device is rotated to landscape mode. However with the new screen dimensions part of the drawing is now off screen.
What is the best way of adjusting the points in my path to stretch the image across the new screen dimensions so the user doesn't loose any of their drawing off screen?
I solved my issue like this:
if(getWidth() > getHeight())
{
Matrix scaleMatrix = new Matrix();
RectF rectF = new RectF();
p.computeBounds(rectF, true);
scaleMatrix.setScale(1.6f, .6f,0,0);
p.transform(scaleMatrix);
}
canvas.drawPath(p, paint);
Where p is the path.

Draw circle over the bitmap with the color same as the under bitmap

I have an image, I can draw an circle over it like the code below:
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.girl).copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setStrokeWidth(200);
paint.setStyle(Style.STROKE.FILL);
paint.setAntiAlias(true);
canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, 100,
paint);
And the circle will be red color! But because of some reason, I want the color of circle same as its under bitmap. Can I do it? and how? Thanks everyone!
Edited: One more question, How I can get all the pixels inside circle?
why dont you simply remove the paint.setStyle(Style.STROKE.FILL); or try changing the paint.setColor(Color.RED); to Transparent color, that should be it
you can get the color of a specified pixel by the method in this article and then make a RGB Color then set this color instead of the color.RED you mentioned.

Convert Text To Bitmap(Pixel) on Android

I have an android application in which I need to download text from a website, convert it into bitmap format and display it on an LED-based display board.
I am struggling with the bitmap conversion.
Tried to use the following:
Bitmap mybitmap = Bitmap.createBitmap(100, 16, Bitmap.Config.ALPHA_8);
Canvas c = new Canvas(mybitmap);
c.drawText("0", 0, 0, paint);
But it doesn't seem to be working. Any suggestions?
Update:
Paint object is initialized like this:
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.RED);
paint.setTextSize(16);
paint.setAntiAlias(true);
paint.setTypeface(Typeface.MONOSPACE);
I think you draw outside the image. Try setting y to 16.
c.drawText("0", 0, 16, paint);
Note that when drawing text the coordinate origin is the lower left coordinate corner.

Categories

Resources