日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

RotateAnimation 实现表盘指针转动

發布時間:2025/3/21 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 实现表盘指针转动的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。