旋转内部有动画的对话框时出现问题
Posted
技术标签:
【中文标题】旋转内部有动画的对话框时出现问题【英文标题】: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 动画的新位置?
【问题讨论】:
【参考方案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();
【讨论】:
以上是关于旋转内部有动画的对话框时出现问题的主要内容,如果未能解决你的问题,请参考以下文章
Qt炫酷动画demo04-仿Android旋转图标的等待对话框动画
Qt炫酷动画demo04-仿Android旋转图标的等待对话框动画
Qt炫酷动画demo04-仿Android旋转图标的等待对话框动画