【问题标题】:Problem in rotating a dialog which has an animation inside旋转内部有动画的对话框时出现问题
【发布时间】:2011-09-06 09:02:49
【问题描述】:

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

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 动画的新位置?

【问题讨论】:

    标签: android animation rotation android-alertdialog


    【解决方案1】:

    我刚刚添加了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();
        }
    

    【讨论】:

      猜你喜欢
      • 2013-07-25
      • 2021-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-01
      • 2011-07-29
      • 2014-12-23
      • 1970-01-01
      相关资源
      最近更新 更多