问题描述
我已经采取了教程这里一>(可动态旋转触摸/一扔图像),并试图与整体布局,而不是只是一个形象重构它,所以我从TutorialActivity改变rotateDialer()方法,像这样:
I've taken the tutorial here (which dynamically rotates an image on touch/fling) and tried to refactor it with a whole layout instead of just one image, so I've changed the rotateDialer() method from the TutorialActivity like so:
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);
}
,其中拨号器是我用了不过旋转画布多度的setNextRotation()方法来设置重写的onDraw()方法创建一个自定义的布局,但在画布上没有出现旋转!这里是我的自定义布局:
where dialer is a custom layout I've created with an overridden onDraw() method that rotates the canvas however many degrees set in the setNextRotation() method, but the canvas does not appear to rotate! Here is my custom layout:
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:
this is the 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()拨号程序,然后计算工作正常(我收到在日志中变化值),但观点似乎并不被转动,我在做什么错了?
Why does the canvas not visibly rotate? I'm invalidating the dialler in onTouch() after the calculation of the degrees, and the calculation is working correctly (I'm getting changing values in the log) but the view just doesn't seem to be rotating, what am I doing wrong?
推荐答案
好吧事实证明了的onDraw()从未被调用的方法,我只是简单地不得不添加
Ok it turns out the onDraw() method was never being called, I just simply had to add
setWillNotDraw(false)
我的init()函数哎preSTO中的onDraw()方法被调用,而我的观点是旋转!!!!
to my init() function and hey presto the onDraw() method is being called, and my view is rotating!!!!
这篇关于试图旋转Android的布局,帆布不会出现旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!