我一直在學習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