【问题标题】:Trying to rotate a layout in android, canvas does not appear to rotate试图在android中旋转布局,画布似乎没有旋转
【发布时间】:2012-04-16 13:16:20
【问题描述】:

我已经学习了教程here(它会在触摸/滑动时动态旋转图像)并尝试使用整个布局而不是仅一个图像来重构它,因此我更改了 rotateDialer() 方法TutorialActivity 像这样:

private void rotateDialer(float degrees) {
    //matrix.postRotate(degrees, dialerWidth / 2, dialerHeight / 2);
    //dialer.setImageMatrix(matrix);
    dialer.setNextRotation(degrees, dialerWidth / 2, dialerHeight  / 2);
    dialer.invalidate();
    info.setText("Current Quadrant Touched = "+currentQuadrantTouched
            +"\nAngle = "+degrees);
}

其中 dialer 是我使用重写的 onDraw() 方法创建的自定义布局,该方法可以旋转画布,但在 setNextRotation() 方法中设置了多少度,但画布似乎没有旋转!这是我的自定义布局:

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class DialView extends FrameLayout {
public static final String LOG_TAG = "DialView";
RelativeLayout dialView;
float degrees;
float px;
float py;

public DialView(Context context) {
    super(context);
    init();
    // TODO Auto-generated constructor stub
}

public DialView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
    // TODO Auto-generated constructor stub
}
public DialView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // TODO Auto-generated constructor stub
    init();
}


private void init()
{
    //initialise rotation
    this.degrees = 0;
    this.px = 0;
    this.py = 0;
    LayoutInflater inflator = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    dialView = (RelativeLayout) inflator.inflate(R.layout.dial_view, this,false);
    addView(dialView);
    Log.d(LOG_TAG, "DialView: init() "+ degrees+"degrees around ("+getWidth() / 2+","+getHeight() / 2+")");

}



public void setNextRotation(float degrees, float px, float py)
{
    this.degrees = degrees;
    this.px = px;
    this.py = py;
    Log.d(LOG_TAG, "Next Rotation = "+ degrees+"degrees around ("+px+","+py+")");
}

@Override
protected void onDraw(Canvas canvas) {
    canvas.save();
    // canvas.rotate(45,<appropriate x pivot value>,<appropriate y pivot value>);
     canvas.rotate(degrees, px, py);
     Log.d(LOG_TAG, "onDraw: Rotation = "+ degrees+"degrees around ("+px+","+py+")");
     super.onDraw(canvas);
     canvas.restore();

}


}

这是 dial_view.xml:

<ImageView
    android:id="@+id/topLeftImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="38dp"
    android:clickable="true"
    android:layout_marginTop="28dp"
    android:src="@drawable/icon" />

<ImageView
    android:id="@+id/topRightImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:clickable="true"
    android:layout_alignParentRight="true"
    android:layout_marginRight="50dp"
    android:src="@drawable/icon" />

<ImageView
    android:id="@+id/bottomLeftImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:clickable="true"
    android:layout_marginBottom="24dp"
    android:layout_marginLeft="22dp"
    android:src="@drawable/icon" />

<ImageView
    android:id="@+id/bottomRightImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:clickable="true"
    android:layout_alignParentRight="true"
    android:layout_marginRight="28dp"
    android:src="@drawable/icon" />

</RelativeLayout>

为什么画布没有明显旋转?我在计算度数后使 onTouch() 中的拨号器无效,并且计算工作正常(我在日志中更改值)但视图似乎没有旋转,我在做什么错了吗?

【问题讨论】:

  • 我知道这是一篇旧帖子,但只需花几个小时来理解这一点:如果您移动 canvas.save();之前 canvas.rotate(degrees, px, py);

标签: android android-layout touch rotation ondraw


【解决方案1】:

好吧,原来 onDraw() 方法从未被调用过,我只需要添加

setWillNotDraw(false)

到我的 init() 函数,嘿,onDraw() 方法被调用了,我的视图正在旋转!!!!

【讨论】:

  • 正如我上面所说,我只需将“setWillNotDraw(false)”写入我的 init() 函数。如果你把这条线放在你的类的构造方法上,它也可以工作。原始问题中的原始代码 sn-ps 将通过这样做来工作,随意复制它并将该行插入到 DialView() 构造函数或 init() 函数中
  • 我插入了你说的代码,但视图保持不变,..根本不旋转..我也可以看到角度日志
  • 日志是从onDraw方法调用还是从其他地方调用?
  • 我调试通过,每次触摸按钮后都会调用 onDraw,但它们正在移动,是的,日志也来自 onDraw..
  • 该代码看起来不错 - 问题很可能出在您调用 setNextRotation() 方法的任何位置(即您上面提到的这些“按钮”所在的位置)
猜你喜欢
  • 1970-01-01
  • 2015-10-01
  • 2014-02-01
  • 2016-03-13
  • 1970-01-01
  • 2013-04-10
  • 1970-01-01
  • 1970-01-01
  • 2021-09-27
相关资源
最近更新 更多