回転し、アプリケーションのデータを含む円を作成する必要があります。アプリケーション用にカスタマイズされたオブジェクトを作成する必要がありますか、それともアプリケーション内ウィジェットを作成する必要がありますか?
Android デスクトップのスタンドアロン ウィジェットではなく、アプリケーション内のウィジェットをどのように参照しますか?
これは回転可能な LinearLayout で、すべてを入れることができ、カスタマイズすれば角度ごとに回転させることができます。それを回転させるには、rotate() メソッドを使用します。
楽しい!;)
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.LinearLayout;
public class RotateLinearLayout extends LinearLayout {
private Matrix mForward = new Matrix();
private Matrix mReverse = new Matrix();
private float[] mTemp = new float[2];
private float degree = 0;
public RotateLinearLayout(Context context) {
super(context);
}
public RotateLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void dispatchDraw(Canvas canvas) {
try {
if (degree == 0) {
super.dispatchDraw(canvas);
return;
}
canvas.rotate(degree, getWidth() / 2, getHeight() / 2);
mForward = canvas.getMatrix();
mForward.invert(mReverse);
canvas.save();
canvas.setMatrix(mForward); // This is the matrix we need to use for
// proper positioning of touch events
super.dispatchDraw(canvas);
canvas.restore();
invalidate();
} catch (Exception e) {
}
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
if (degree == 0) {
return super.dispatchTouchEvent(event);
}
// final float[] temp = mTemp;
// temp[0] = event.getX();
// temp[1] = event.getY();
// mReverse.mapPoints(temp);
// event.setLocation(temp[0], temp[1]);
event.setLocation(getWidth() - event.getX(), getHeight() - event.getY());
return super.dispatchTouchEvent(event);
}
public void rotate() {
if (degree == 0) {
degree = 180;
} else {
degree = 0;
}
}
}
アップデート:
このコードを xml レイアウトに追加し、ImageView や別の LinearLayout などのビューを配置します。
<org.mabna.order.ui.RotateLinearLayout android:id="@+id/llParent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:gravity="center"
android:orientation="horizontal" >
<ImageView
android:id="@+id/myImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:src="@drawable/main01" />
</org.mabna.order.ui.RotateLinearLayout>
onCreate() メソッドで:
llParent = (RotateLinearLayout) this.findViewById(R.id.llParent);
ボタンの onClickListener で:
protected void btnRotate_onClick() {
llParent.rotate();
}
アップデート2:
実際の回転の前にアニメーションを使用して回転させることができます ( llParent.rotate();
)。これには、rotate_dialog.xml のようなアニメーション レイアウトが必要です。
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000" android:fromDegrees="-180" android:toDegrees="0"
android:pivotX="50%" android:pivotY="50%" android:fillAfter="true" />
そしてあなたのコードで:
protected void btnRotate_onClick() {
// rotate
Animation rotateAnimation = AnimationUtils.loadAnimation(this,
R.anim.rotate_dialog);
llParent.startAnimation(rotateAnimation);
llParent.rotate();
}
View
クラスから派生したカスタム ウィジェットから回転アニメーションを作成するかなり簡単な方法があります。ビューを作成してレイアウトに配置したら、ビューに を指定してView.setAnimation(Animation)
またはView.startAnimation(Animation)
を呼び出しRotateAnimation
てビューを開始できます。xml で定義された回転アニメーションの例を次に示します。これは、アクティビティから でロードできますgetResources().getAnimation(int)
。
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="float"
android:toDegrees="float"
android:pivotX="float"
android:pivotY="float" />