盛年不重来,一日难再晨,及时当勉励,岁月不待人。这篇文章主要讲述试图在android中旋转布局,画布似乎不会旋转相关的知识,希望能为你提供帮助。
我已经学习了教程here(它在触摸/拖动时动态旋转图像)并试图用整个布局而不是仅仅一个图像重构它,所以我从TutorialActivity改变了rotateDialer()方法,如下所示:
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
+"
Angle = "+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="https://www.songbingjia.com/android/@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="https://www.songbingjia.com/android/@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="https://www.songbingjia.com/android/@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="https://www.songbingjia.com/android/@drawable/icon" />
<
/RelativeLayout>
为什么画布不会明显旋转?在计算度数后,我在onTouch()中使拨号器无效,并且计算工作正常(我正在更改日志中的值)但视图似乎没有旋转,我在做什么错误?
答案好吧事实证明onDraw()方法从未被调用过,我只是简单地添加
setWillNotDraw(false)
【试图在android中旋转布局,画布似乎不会旋转】到我的init()函数和嘿presto正在调用onDraw()方法,我的视图正在旋转!
推荐阅读
- Android(围绕Y的第二次180度旋转镜像会反映图像)
- Android两指旋转
- 如何在Android中旋转位图,使图像中心平滑,无振荡运动
- Android Camera2预览偶尔会旋转90度
- Android以XML格式访问SDK属性
- 如何解决Windows中的cmd或node.js命令提示符问题(git未安装或未在PATH中)
- 使用Javascript,HTML和CSS创建C#Windows .NET应用程序
- symfony项目中的供应商文件夹不会与(netbeans或svn)subversion一起上传
- Zoho邮件在Hotmail和Outlook用户的垃圾邮件文件夹中结束(解决方案)