旋转内部有动画的对话框时出现问题

时间:2011-09-06 09:02:49

标签: android animation rotation alertdialog

我使用以下类创建一个可旋转的对话框,一切正常。

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;
    }
}
}

我在这个对话框的左侧有一个配有动画的ImageView。当对话框未旋转时,ImageView会正确设置动画。当我旋转对话框时,ImageView必须在对话框的右侧设置动画,但它会将之前放置的ImageView的屏幕像素更改为丑陋状态。我的意思是在旋转后ImageView的位置正确改变但其动画的位置保持不变。

如何设置ImageView动画的新位置?

1 个答案:

答案 0 :(得分:0)

我刚刚添加了行invalidate();,所有内容都已更正:

    @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();
    }