我一直在学习Android大约两周(as2/3专家)。Android - 用手指旋转ImageView移动其他视图
我已经创建了一个简单LinearLayout1, which contains an
ImageView`,含有
我已经设置了这个ImageView
到旋转的黑胶唱片(只是一个圆盘基本上)的PNG当用户拖动手指在其上,如抓一个记录。
我的代码似乎正在工作(不知道它的最佳方式,但它的工作原理)。问题是,当ImageView
旋转时,它会推动其他视图。我确定这是因为我的圆形图像实际上是一个带有透明角的正方形。正在推动其他视角的这些角落,并且如果我有左对齐ImageView
,导致记录从屏幕左侧推开。
有几个尝试来解决这个在线问题,但他们都不是我所需要的,有些被包含在例子中如此强大,我无法从我的初始层面看出它应该采取什么措施。
如果有人可以阐明我能做些什么,那么解决这个问题将会很棒。我意识到一个简单的答案可能不存在,但请记住我是一个Java noob并尽可能简单地保持它!非常感谢您的帮助!
如果有人可以告诉我为什么我需要从旋转中减去50以使记录在用户触摸的确切位置下移动,那也是有帮助的!请参阅updateRotation()方法。
这里是我的XML标记:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/baseView"
android:background="#FFFFFFFF"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/turntable"
android:src="@drawable/turntable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
,这里是我的Java代码:
package com.codingfiend.android.turntable;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
public class Turntable extends Activity
{
View baseView;
ImageView turntable;
TextView bottomText;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
baseView = (View)findViewById(R.id.baseView);
turntable = (ImageView)findViewById(R.id.turntable);
turntable.setOnTouchListener(onTableTouched);
}
public android.view.View.OnTouchListener onTableTouched = new android.view.View.OnTouchListener()
{
public boolean onTouch(View v, MotionEvent evt)
{
double r = Math.atan2(evt.getX() - turntable.getWidth()/2, turntable.getHeight()/2 - evt.getY());
int rotation = (int) Math.toDegrees(r);
if (evt.getAction() == MotionEvent.ACTION_DOWN)
{
//
}
if (evt.getAction() == MotionEvent.ACTION_MOVE)
{
updateRotation(rotation);
}
if (evt.getAction() == MotionEvent.ACTION_UP)
{
//
}
return true;
}
};
private void updateRotation(double rot)
{
float newRot = new Float(rot);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.turntable);
Matrix matrix = new Matrix();
matrix.postRotate(newRot - 50);
Bitmap redrawnBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
turntable.setImageBitmap(redrawnBitmap);
}
}
编辑
这是谈到了将一个恼人的问题。必须有一种方法可以旋转比屏幕更大或更大的图像,而不缩放,以便整个图像保持在屏幕上。为什么旋转图像的一部分不能进入屏幕?非常令人沮丧。我试图将ImageView整合到一个FrameView,这部分帮助。现在,我可以添加其他视图,但不会受到影响,但我仍然无法将光盘放得足够大,因为它在旋转时不允许通过屏幕边框。我不明白为什么。我还没有检查出延伸ImageView呢。任何其他的想法仍然将不胜感激!
+1问的问题。欢迎来到SO。 – wheaties 2010-12-11 02:45:35