android-顺时逆时旋转的仪表盘控件
来源:互联网 发布:酷狗软件 编辑:程序博客网 时间:2024/04/29 20:31
在一次项目需求中,第一次接触到了仪表盘,先来说下需求吧:定时获取数据然后根据数据动态改变指针位置。今天我在这里分享我自己写的一个仪表盘控件,这里我就不定时获取,而是定时使用随机数得到一个值。
先来看看效果截图
我的布局很简单,就三个控件,一个表盘,一个指针,一个中间的文本。在布局上为了方便,我使用了居于水平居中。这样三个控件都会叠在一起,形成上面图片的效果。
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout" android:layout_width="match_parent" android:layout_height="wrap_content" > <ImageView android:id="@+id/panelimage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="20dp" android:src="@drawable/temperature_bg" /> <ImageView android:id="@+id/needle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="20dp" android:src="@drawable/pointer" /> <TextView android:id="@+id/degreeID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="85dp" android:background="@drawable/small_semicircle" android:textSize="30sp" android:gravity="center" /></RelativeLayout>
布局看完了,最重要是实现,从网络上获取回来值,再把这个值显示在TextView中,并且对指针经行控制,通过动画实现旋转过程。这里我用Random()方法来设置这个值。
在初始化时new一个timer,每1秒触发一次,发送消息到handler触发随机函数产生随机数
@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.tmptest);// 指针needleView = (ImageView) findViewById(R.id.needle);// 表盘读数showText = (TextView) findViewById(R.id.degreeID);// 定时器,1秒启动一次Timer timer = new Timer();timer.schedule(mTimerTask, 0, 1000);}// TimerTask private TimerTask mTimerTask = new TimerTask() {public void run() {Message message1 = new Message();message1.what = 1;thandler.sendMessage(message1);}};private Handler thandler = new Handler() {public void handleMessage(Message msg1) {// 产生随机数0-40Random random = new Random();int a = -20 + random.nextInt(61);// 设置最高值maxDegree = Float.parseFloat(String.valueOf(a + 20));// 开始转动mTimer = new Timer();// 设置每10毫秒转动一下mTimer.schedule(new NeedleTask(), 0, 10);showText.setText(String.valueOf(a));flag = true;}};
thandler里我们再new了一个timer来对指针进行控制,通过animation动画来实现指针的跳转
private class NeedleTask extends TimerTask {@Overridepublic void run() {if (degree <= maxDegree * (3.0f)) {mHandler.sendEmptyMessage(0);}if (degree >= maxDegree * (3.0f) && flag == true) {mHandler2.sendEmptyMessage(0);}}}// 顺时private Handler mHandler = new Handler() {@Overridepublic void handleMessage(Message msg) {// 设置仪表盘指针转动动画// 仪表盘最大是180度if (degree >= maxDegree * (3.0f)) {mTimer.cancel();} else {degree += 5.0f;// 设置转动动画animation = new RotateAnimation(degree, maxDegree * (3.0f),Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f);}// 设置动画时间5毫秒animation.setDuration(5);animation.setFillAfter(true);needleView.startAnimation(animation);flag = false;}};// 逆时private Handler mHandler2 = new Handler() {@Overridepublic void handleMessage(Message msg) { // 设置仪表盘指针转动动画// 仪表盘最大是180度,这个是自己测出来的if (degree <= maxDegree * (3.0f)) {mTimer.cancel();} else {degree += -5.0f;animation = new RotateAnimation(degree, maxDegree * (3.0f),Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f);}// 设置动画时间5毫秒animation.setDuration(5);animation.setFillAfter(true);needleView.startAnimation(animation);flag = true;}};
大致思路就是这样,不过这里还有问题,在某些情况下会出现指针卡住没有跳转的情况,懒得完善。
demo下载
0 0
- android-顺时逆时旋转的仪表盘控件
- Android实现图片顺时逆时旋转及拖拽显示效果
- android一款仪表盘控件
- android 仪表盘控件
- android 仪表盘控件
- Android 仪表盘 图片 中心 旋转
- android 随旋转而旋转的控件
- android 自定义控件之汽车仪表盘
- android 简易仪表盘的设计
- Leetcode顺时钟旋转90度
- 如何把Android的一款自定义仪表盘控件整合到你的项目里
- Android自定义控件—仿仪表盘进度控件ArcProgressBar
- Android仪表盘
- Android 仪表盘
- android 实现控件的旋转动画效果
- android 实现控件的旋转动画效果
- timer控件 倒计时 顺时计时 重置计时
- Android仿支付宝信用分仪表盘控件
- ucOSII 学习之初识实时操作系统-时间管理
- 线段树入门(poj 3274 3468 2528)
- DevExpress GridControl 自动适应高度
- Protocol Buffer简介
- 集成百度语音sdk到智能家居平台中
- android-顺时逆时旋转的仪表盘控件
- 2014年8月18日 聚焦互联网金融
- Android中的ListView用法(自动滚动)
- 计算机系统要素:第五章 计算机体系结构
- POJ 2965 The Pilots Brothers' refrigerator(dfs+记录路径)
- axis 开发webservice
- PHP里$_SERVER['HTTP_HOST']和$_SERVER['PHP_SELF']区别
- C float类型 范围 详解
- TQ2440 在Keil V5 MDK下创建工程 并 使用Jlink 在RAM调试 方法