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

发布于 2024-12-03 01:06:44 字数 1766 浏览 0 评论 0原文

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一百个冬季 2024-12-10 01:06:44

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

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();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文