android中实现指针滑动的动态效果方法

 更新时间:2013年03月15日 10:27:12   作者:  
本次实现的是类似于墨迹天气中轨迹图片上指针随着数值滚动滑动的效果,基本思路是开启线程,控制指针所在的imageview控件的padding属性。

复制代码 代码如下:

<FrameLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#fff"
            android:paddingBottom="5dp"
            android:paddingLeft="5dp"
            android:paddingRight="2dp"
            >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:src="@drawable/up_icon"
                android:layout_marginTop="0dp"
                android:paddingTop="0dp" />

            <LinearLayout
                android:layout_width="240dip"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="7dp"
                android:orientation="horizontal" >

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="left"
                    android:text="优"
                    android:textSize="12sp" />

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="left"
                    android:text="良"
                    android:textSize="12sp" />

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="left"
                    android:text="中等"
                    android:textSize="12sp" />

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="left"
                    android:text="不健康"
                    android:textSize="12sp" />

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="left"
                    android:text="有毒害"
                    android:textSize="12sp" />
            </LinearLayout>

           
            <LinearLayout
                android:layout_width="240dip"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="7dp"
                android:layout_marginTop="40dp"
                android:orientation="horizontal" >

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center_horizontal"
                    android:text="80"
                    android:textSize="12sp" />

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center_horizontal"
                    android:text="120"
                    android:textSize="12sp" />

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center_horizontal"
                    android:text="160"
                    android:textSize="12sp" />

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center_horizontal"
                    android:text="200"
                    android:textSize="12sp" />

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center_horizontal"
                    android:text="400"
                    android:textSize="12sp" />
            </LinearLayout>

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:src="@drawable/zhizhen"
                android:id="@+id/zhizhen"/>

            <ImageView
                android:id="@+id/dengji_img"
                android:layout_width="250dip"
                android:layout_height="wrap_content"
                android:layout_marginTop="25dp"
                android:src="@drawable/dengji_icon" />
        </FrameLayout>

上面这段代码实现的布局为

首先,因为指针有压着下面的滚动条,因此这是一个framelayout的布局。其次,要实现指针的匀速滚动,需要开启一个线程,在线程中能够实现利用循环,以及线程的休眠,通过控制指针所在图标的padding属性来实现滚动的动画效果

复制代码 代码如下:

Handler myHandler =new Handler(){

        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
            //对于c的更改和循环应该是在线程中跑,要不run仅仅执行一次,           
            zhizhen.setPadding(c, 0, 0, 0);
        }

       
    };
    class MyThread extends Thread{

        @Override
        public void run() {
            //发送一个消息,通知主线程改变UI
         try {

             while(c<=input){
                 c=c+1;
                 this.sleep(10);
                 myHandler.sendEmptyMessage(0);                
             }

               
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

相关文章

  • Flutter使用socketIo实现实时通讯

    Flutter使用socketIo实现实时通讯

    本文主要介绍了Flutter使用socketIo实现实时通讯,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-07-07
  • Android编程实现的一键锁屏程序详解

    Android编程实现的一键锁屏程序详解

    这篇文章主要介绍了Android编程实现的一键锁屏程序,结合实例形式详细分析了Android一键锁屏的原理与相关实现技巧,需要的朋友可以参考下
    2016-10-10
  • Android ViewBinding使用介绍

    Android ViewBinding使用介绍

    最近这段时间在学习Kotlin,突然发现谷歌已经把kotlin-android-extensions插件废弃,目前推荐使用ViewBinding来进行替代,接下来通过本文给大家分享Android使用ViewBinding的详细步骤,感兴趣的朋友一起学习吧
    2022-09-09
  • android获取联系人示例分享

    android获取联系人示例分享

    这篇文章主要介绍了android获取联系人示例,需要的朋友可以参考下
    2014-02-02
  • Android 使用SwipeRefreshLayout控件仿抖音做的视频下拉刷新效果

    Android 使用SwipeRefreshLayout控件仿抖音做的视频下拉刷新效果

    这篇文章主要介绍了Android 使用SwipeRefreshLayout控件仿抖音做的视频下拉刷新效果,需要的朋友可以参考下
    2018-05-05
  • Android studio导入项目的方法详解(简单快速)

    Android studio导入项目的方法详解(简单快速)

    最近开课移动互联网应用开发,实验课老师发了代码让我们导入,在网上找了各种方法,发现不是每一个项目都适合,有些能够成功,有些还是有错,头大的很。后面发现一个比较简单的方法,没翻过车,新手可以试试
    2017-06-06
  • Android 13新功能及适配工作详解

    Android 13新功能及适配工作详解

    这篇文章主要为大家介绍了Android 13的新功能及需要哪些适配工作详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-03-03
  • Android 实现会旋转的饼状统计图实例代码

    Android 实现会旋转的饼状统计图实例代码

    这篇文章主要介绍了Android 实现会旋转的饼状统计图实例代码的相关资料,这里附有实例代码及实现效果图,需要的朋友可以参考下
    2016-12-12
  • Android开发中那些需要注意的坑

    Android开发中那些需要注意的坑

    这篇文章主要介绍了Android开发过程中那些需要注意的坑,有一些是自己遇到的,特分享给大家,需要的朋友可以参考下
    2016-04-04
  • android实现系统信息推送

    android实现系统信息推送

    这篇文章主要为大家详细介绍了android实现系统信息推送,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-04-04

最新评论