极品Android应用程序的可旋转的圆圈。自定义对象或小工具? [英] Need a rotatable circle for Android application. Customized object or widget?
问题描述
我需要创建一个旋转并包含数据为我申请了一圈。我应该创建一个自定义对象为我的应用程序或我应该做一个在应用程序窗口小部件?
I need to create a circle that rotates and contains data for my application. Should I create a customized object for my application or should I make a in-application widget?
虽然上的话题,你怎么是指一个Widget应用程序,而不是为Android桌面独立的小部件中?
While on the topic, how do you refer to a widget within an application instead of a stand alone widget for the android desktop?
推荐答案
这是一个可旋转的LinearLayout,你可以把它的一切,你可以通过度,如果你自定义它旋转。使用旋转()方法来旋转和...
This is a rotatable LinearLayout that you can put everything in it and you can rotate it by degree if you customize it. use rotate() method to rotate it and...
享受! ;)
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;
}
}
}
更新:
添加此code到你的XML布局,把你的意见一样的ImageView或其他的LinearLayout它:
add this code to your xml layout and put your Views like ImageView or another LinearLayout in it :
<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()方法:
in onCreate() method:
llParent = (RotateLinearLayout) this.findViewById(R.id.llParent);
在一个按钮onClickListener:
in onClickListener of a button:
protected void btnRotate_onClick() {
llParent.rotate();
}
UPDATE2:
您可以使用动画之前,真正的旋转旋转( llParent.rotate();
)。它需要一个动画布局像rotate_dialog.xml:
You can use an animation for rotation before real rotation (llParent.rotate();
). it needs an animation layout like 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" />
和在你的code:
protected void btnRotate_onClick() {
// rotate
Animation rotateAnimation = AnimationUtils.loadAnimation(this,
R.anim.rotate_dialog);
llParent.startAnimation(rotateAnimation);
llParent.rotate();
}
这篇关于极品Android应用程序的可旋转的圆圈。自定义对象或小工具?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!