How to animate shapes and Images that draw at onDraw method? - android

I'm trying to create myImageView Custom Control that extends ImageView
I need to animate everything that draw in myImageView.
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
canvas.drawBitmap(bitmap, getImageMatrix(), paint);
}

Related

Rotate ImageButton onCreate without Animation

Is it possible to rotate an ImageButton using the onCreate() function? Or do you have to use an Animation which starts onCreate()? Because with an Animation i can see a little "flick" on Activity start...
you can use ViewCompat.setRotation(buttonInstance, rotationAngle);. From the documentation
Sets the degrees that the view is rotated around the pivot point.
You can override the onDraw() method using a custom class that extends ImageButton (which I'm sure you have).
#Override
protected void onDraw(#NonNull Canvas canvas) {
super.onDraw(canvas);
// Rotate a Bitmap
final Matrix matrix = new Matrix();
matrix.setRotate(angle, imageCenterX, imageCenterY);
canvas.drawBitmap(bitmap, matrix, null);
/*
* OR
**/
// Rotate the canvas
canvas.save(Canvas.MATRIX_SAVE_FLAG);
canvas.rotate(-angle);
canvas.drawBitmap(bitmap, left, top, null);
canvas.restore();
}
Choose one or the other solution, not both together ;)
EDIT
After some quick reflection, that could also work (not tested):
#Override
protected void onDraw(#NonNull Canvas canvas) {
canvas.rotate(-angle);
super.onDraw(canvas);
}

Changing bitmaps on a canvas

I have a view class and and activity and the view class in linked in with the XML. The view class starts by drawing a bitmap of the letter a. In the activity there are next and previous buttons to allow the user to maneuver through the letter images. However I have looked everywhere for clearing a canvas and there doesn't seem to be anything on the matter. I am unsure how to access the view class to change the images as they are linked through the XML layout file.
View class
protected void onDraw(Canvas canvas) {
drawLetterA(canvas);
}
public void drawLetterA (Canvas canvas) {
Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.lettera);
canvas.drawBitmap(myBitmap, 0, 0, null);
}
public void drawLetterB (Canvas canvas) {
//canvas.drawColor(Color.WHITE);
Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.letterb);
canvas.drawBitmap(myBitmap, 0, 0, null);
}

Android: transform image using a Matrix

I'm looking to transform an image using a Matrix on the onDraw method of a custom class I created which extends ImageView e.g.,
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
canvas.setMatrix(imageMatrix);
canvas.drawBitmap(((BitmapDrawable)mIcon).getBitmap(), imageMatrix, null);
canvas.restore();
}
However, what I coded above does not really work. How exactly do I apply the imageMatrix on the canvas? Thanks!
Try calling Drawable.draw(Canvas) method:
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
canvas.setMatrix(imageMatrix);
((BitmapDrawable)mIcon).draw(canvas);
canvas.restore();
}
All you did is good, just put the super call to be the last, coz there is where all the painting is done...
#Override
public void onDraw(Canvas canvas) {
canvas.setMatrix(imageMatrix);
super.onDraw(canvas);
}

Drawing with a canvas over an ImageView

I want to create an ImageView and then draw text on top of the ImageView. I also need to be able to modify the text periodically. Currently I've created a custom view that extends ImageView. Then I overwrite onDraw() and use it to draw the text. Only problem then is that when I use my custom ImageView it doesn't draw the image, just the text.
public class BoardView extends ImageView
{
public BoardView(Context context)
{
super(context);
}
protected void onDraw(Canvas canvas)
{
Paint paint = new Paint();
setImageResource(R.drawable.board);
paint.setColor(Color.BLUE);
canvas.drawText(x.getName(), x.getX(), x.getY(), paint);
}
}
You should call the super.onDraw in your onDraw method before your draw code.

How to take canvas on imageview in android

I want to display canvas contents on imageview in android
i am not understanding the
imageview.draw(canvas);
Heres my code:
public class Matrix extends Activity {
public Bitmap mybitmap,newbmp,bitmap,bmp;
ImageView imageview;
Paint paint;
#Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageview=(ImageView)findViewById(R.id.ImageView01);
imageview.setDrawingCacheEnabled(true);
}
protected void onDraw(Canvas canvas)
{
imageview.draw(canvas);
mybitmap=BitmapFactory.decodeResource(getResources(), R.drawable.image);
canvas.drawBitmap(mybitmap, 0, 0, paint);
}
}
"I want to display canvas contents on imageview in android"
So you want to draw what is on the Canvas in to your ImageView? If that's what you want then you need to read the links given by johike because you seem to have become confused a little.
The following in your code:
imageview.draw(canvas);
Does NOT mean draw the contents of canvas in to the imageview. It means the opposite, draw the imageview to the canvas.
Even if your question is not detailed enough to give you a precise answer I can give you the following hints:
Derive your on class from ImageView and then override the onDraw method
#Override
protected void onDraw(Canvas canvas) {
// draw a blue background
canvas.drawColor(Color.BLUE);
// additional drawings here
}
Further study the Android references:
http://developer.android.com/guide/topics/graphics/2d-graphics.html
http://developer.android.com/reference/android/graphics/Canvas.html

Categories

Resources