RotateAnimation 实现表盘指针转动

RotateAnimation 实现表盘指针转动

最近在做车助手这个项目,遇到这样的一个功能需求:获取车子启动的实时数据让指针转动:

我这里做了一个Demo:demo的原理在于使用onTouchEvent事件,计算手指在屏幕上开始

和结束的位置来作为指针转动的角度,并且跟着手指不断的滑动,指针不断的变化,效果图如下:

https://img-my.csdn.net/uploads/201304/10/1365562505_7410.png  https://img-my.csdn.net/uploads/201304/10/1365562532_3090.png

https://img-my.csdn.net/uploads/201304/10/1365562553_4946.png

代码如下:

主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

https://img-my.csdn.net/uploads/201304/10/1365562808_6583.png

point:

https://img-my.csdn.net/uploads/201304/10/1365562864_3927.png

Demo下载地址:

http://download.csdn.net/download/lyhdream/5241123


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值