RotateAnimation 实现表盘指针转动
RotateAnimation 實現表盤指針轉動
最近在做車助手這個項目,遇到這樣的一個功能需求:獲取車子啟動的實時數據讓指針轉動:
我這里做了一個Demo:demo的原理在于使用onTouchEvent事件,計算手指在屏幕上開始
和結束的位置來作為指針轉動的角度,并且跟著手指不斷的滑動,指針不斷的變化,效果圖如下:
??
代碼如下:
主activity:
public class MainActivity extends Activity {
private ImageView pointer;
LayoutInflater inflater;
RotateAnimation rotateAnimation;
Bitmap bitmap;
View view;
float start = 0;
float end = 0;
float distance = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pointer = (ImageView)findViewById(R.id.pointer_data1);
inflater = LayoutInflater.from(this);
view = inflater.inflate(R.layout.activity_main, null);
bitmap = ((BitmapDrawable) getResources().getDrawable(
R.drawable.point)).getBitmap();
pointer.setImageBitmap(bitmap);
rotateAnimation = new RotateAnimation(start - end,-7.8f,Animation.RELATIVE_TO_SELF,?
0.5f,Animation.RELATIVE_TO_SELF,0.5f);?
rotateAnimation.setFillAfter(true);
rotateAnimation.setDuration(1); // 持續時間
pointer.startAnimation(rotateAnimation);
}
public boolean onTouchEvent(MotionEvent event) {
? ? ? ? if (event.getAction() == MotionEvent.ACTION_DOWN) {
? ? ? ? ? ? start = event.getY();
? ? ? ? }else if (event.getAction() == MotionEvent.ACTION_MOVE) {
? ? ? ? end = event.getY();
? ? ? ? distance = end - start;
? ? ? ? animPlay(start, end);
? ? ? ? }if (event.getAction() == MotionEvent.ACTION_UP) {
? ? ? ? start = 0;
? ? ? ? end = 0;
? ? ? ? distance = 0;
? ? ? ? }
? ? ? ? return super.onTouchEvent(event);
}
public void animPlay(float star, float end) {
pointer.setImageBitmap(null);
bitmap = ((BitmapDrawable) getResources().getDrawable(
R.drawable.point)).getBitmap();
pointer.setImageBitmap(bitmap);
rotateAnimation = new RotateAnimation(start - end,-7.8f,Animation.RELATIVE_TO_SELF,?
0.5f,Animation.RELATIVE_TO_SELF,0.5f);?
rotateAnimation.setFillAfter(true);
// rotateAnimation.setDuration(10000); // 持續時間
float temp = end - start;
if(temp < 0){
temp = 0 - temp;
}
rotateAnimation.setDuration((long)((temp/360)*1000) ); // 持續時間
pointer.setAnimation(rotateAnimation); // 設置動畫
rotateAnimation.startNow();
rotateAnimation = null;
bitmap = null;
}
?? ?
}
主要布局文件:<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? xmlns:tools="http://schemas.android.com/tools"
? ? android:layout_width="match_parent"
? ? android:layout_height="match_parent"
? ? tools:context=".MainActivity"
?? >
? ? <ImageView
? ? ? ? android:id="@+id/container_data1"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="match_parent"
? ? ? ? android:src="@drawable/clock" />
? ? <ImageView
? ? ? ? android:id="@+id/pointer_data1"
? ? ? ? android:src="@drawable/point"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="match_parent" />
</FrameLayout>
圖片資源:
clock
point:
Demo下載地址:
http://download.csdn.net/download/lyhdream/5241123
總結
以上是生活随笔為你收集整理的RotateAnimation 实现表盘指针转动的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mac下简单绘图工具
- 下一篇: linux 下查看帮助信息