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


你可能感兴趣的:(android开发)