1

I use the following class to create a rotatable dialog and everything is ok.

import android.content.Context;

import android.graphics.Canvas;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.LinearLayout;

public class RotateLinearLayout extends LinearLayout {

private Matrix mForward = new Matrix();
private Matrix mReverse = new Matrix();
private float[] mTemp = new float[2];
private float degree = 0;

public RotateLinearLayout(Context context) {
    super(context);
}

public RotateLinearLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
protected void dispatchDraw(Canvas canvas) {
    if (degree == 0) {
        super.dispatchDraw(canvas);
        return;
    }
    canvas.rotate(degree, getWidth() / 2, getHeight() / 2);
    mForward = canvas.getMatrix();
    mForward.invert(mReverse);
    canvas.save();
    canvas.setMatrix(mForward); // This is the matrix we need to use for
                                // proper positioning of touch events
    super.dispatchDraw(canvas);
    canvas.restore();
}

@Override
public boolean dispatchTouchEvent(MotionEvent event) {
    if (degree == 0) {
        return super.dispatchTouchEvent(event);
    }
    final float[] temp = mTemp;
    temp[0] = event.getX();
    temp[1] = event.getY();

    mReverse.mapPoints(temp);

    event.setLocation(temp[0], temp[1]);
    return super.dispatchTouchEvent(event);
}

public void rotate() {
    if (degree == 0) {
        degree = 180;
    } else {
        degree = 0;
    }
}
}

I have an ImageView on left side of this dialog which is equipped with an animation. When the dialog is not rotated ImageView animates correctly. When I rotate my dialog, ImageView must animate on right side of dialog but it changes the screen pixels of previously placed ImageView in an ugly state. I mean after rotation the position of ImageView changes correctly but the position of its animation remains unchanged.

How can I set the new position of ImageView's animation?

Bob
  • 22,810
  • 38
  • 143
  • 225

1 Answers1

0

I just added the line invalidate(); and everything was corrected:

    @Override
protected void dispatchDraw(Canvas canvas) {
    if (degree == 0) {
        super.dispatchDraw(canvas);
        return;
    }
    canvas.rotate(degree, getWidth() / 2, getHeight() / 2);
    mForward = canvas.getMatrix();
    mForward.invert(mReverse);
    canvas.save();
    canvas.setMatrix(mForward); // This is the matrix we need to use for
                                // proper positioning of touch events
    super.dispatchDraw(canvas);
    canvas.restore();
    // is needed
    invalidate();
    }
Bob
  • 22,810
  • 38
  • 143
  • 225