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);
}
Related
Following is my onDraw method.
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(inte < listOfPoints.size()){
canvas.drawLine(listOfPoints.get(0).x, listOfPoints.get(0).y, listOfPoints.get(inte).x,listOfPoints.get(inte).y, paint);
inte++;
if(inte < listOfPoints.size()){
invalidate();
}
}
}
Is it possible to get a callback once if the canvas has finished drawing the above point?
OnDraw is automatically called for situations that need to be drawn inside the view, so a Canvas is provided, so it will be called as long as the list size.
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);
}
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);
}
Is there another way of rotating an EditText apart from using the RotateAnimation class?
Yes. You have to write your own EditText and override the onDraw:
#Override
protected void onDraw(Canvas canvas) {
canvas.save();
canvas.rotate();
canvas.restore();
}
of course the TextPaint it is on your own too.
i need some help on my android project.
i have a canvas and i fill it with some bitmap.
and i have a pointer that drawn a line on the canvas.
my problem is how to clean the line that i've drawn before?
what method should i call on canvas?
i've tried Canvas.drawColor(), invalidate() and that's not working.
and what is the function of Canvas.drawColor() and
Please help me solve my problem.
thanks in advance
UPDATE!
if i made the code like this:
#Override
protected void onDraw(Canvas canvas) {
// fills the canvas with black
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, p);
obaby.draw(canvas);
}
where i place invalidate() in my code?
and what code should i use if i want to clear the canvas using a button?
UPDATE!
i wrote my onDraw like this:
#Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
if(letsdraw){
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, p);
obaby.draw(canvas);
}
}
and the method in reset button is like this:
public void rst(){
letsdraw = false;
invalidate();
Log.v("tag", "this method called");
}
but no change in the canvas when i called the method.
did i wrote something wrong on the code above?
Make your logic like this. Draw the line on the Canvas with some condition. Check if you want to draw the line, then draw the line.
Skeleton code -
#Override
protected void onDraw(Canvas canvas)
{
if(needToDrawLine)
{
//draw the line
}
// Other drawing stuff
}
Now just update your needToDrawLine variable and call invalidate(). You'll get your result. Let me know if it works.
Update:
onDraw() method will call everytime you call the invalidate(). So everyting inside the onDraw() will execute. The way is, you have to prevent it from drawing some of the part. You'll call invalidate() when you want to redraw the whole view, for example - button for clear the canvas.
canvas.drawColor(Color.BLACK); this line clear your whole view to BLACK color.
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint); this line draw the bitmap at (0,0).
canvas.drawPath(mPath, p); this line draw the path mPath.
obaby.draw(canvas); some other object draw itself.
Now - for example you want to clear the screen, when button pressed. Just initialize a variable if it draw everything. And update the variable in button click.
public boolean drawEverything = true;
#Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
if(drawEverything)
{
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, p);
obaby.draw(canvas);
}
}
public void buttonClicked( ... )
{
drawEverything = false;
}
Iam useing this errage the paint in my activity
mBitmap.eraseColor(Color.TRANSPARENT);
mPath.reset();
mView.invalidate();