我已经学习Android大约两周了(是as2/3的专家)。
我创建了一个简单的 LinearLayout
,其中包含一个ImageView
,里面包含一个黑胶唱片的png图片(基本上就是圆形盘)。
我设置了这个ImageView
当用户在其上拖动手指时旋转,就像刮唱片一样。
我的代码似乎正常工作(不确定是否是最好的方法,但它能够运行)。问题是当ImageView
被旋转时,它会推动其他视图。我已经确定这是因为我的圆形图片实际上是有透明角落的正方形。正是这些角落将其他视图推开,如果我将ImageView
左对齐,则会使唱片从屏幕左侧推开。
网上有一些尝试解决这个问题的方法,但似乎都不是我需要的,而且一些例子也过于复杂,我在初学阶段无法理解。
如果有人能为我提供一些帮助,解决这个问题,那将非常感谢。我意识到可能不存在简单的答案,但请记住我是Java新手,请尽可能保持简单!非常感谢您提前的帮助!
如果有人知道为什么我需要从旋转中减去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的方法。如果有其他想法仍将不胜感激!