转载地址:http://blog.csdn.net/loongggdroid/article/details/50476116

这次是你想要的效果哦!高兴了吧?高兴地话,你不用给我打赏,分享到朋友圈,我就很高兴了。

群里有人问我要竖着的滑动尺效果,前天我贱贱地分享了一个横向的滑动效果,让大家模仿者,自己尝试着去改编一下,不知道有多少人弄出来了,尝试着去弄得请举手,好吧,我也不用说放下了,根本就没人举。我再说一句废话吧,努力去尝试,才会有进步,不要坐等着别人来帮助你,这样你会饿死的,因为很多人不会像我一样去给你分享。

我大声喊一句:盖茨(翠花),上代码。

效果如下:


效果图

第一步:activity_mian.xml布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <LinearLayout        android:id="@+id/user_weight"        android:layout_width="fill_parent"        android:layout_height="0dp"        android:layout_weight="1"        android:orientation="horizontal" >        <LinearLayout            android:layout_width="0dp"            android:layout_height="fill_parent"            android:layout_weight="1"            android:orientation="vertical" >            <TextView                android:id="@+id/user_height_value"                android:layout_width="fill_parent"                android:layout_height="0dp"                android:layout_weight="1"                android:gravity="center"                android:textColor="#000000"                android:textSize="20sp" />            <ImageView                android:id="@+id/userheight"                android:layout_width="fill_parent"                android:layout_height="0dp"                android:layout_margin="10dp"                android:layout_weight="1"                android:background="@null"                android:contentDescription="@null"                android:src="@drawable/my_self_girl" />        LinearLayout>        <LinearLayout            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:orientation="vertical" >            <RelativeLayout                android:id="@+id/v_ruler"                android:layout_width="80dp"                android:layout_height="fill_parent"                android:background="@drawable/my_self_h_ruler" >                <ImageView                    android:layout_width="80dp"                    android:layout_height="2dp"                    android:layout_centerVertical="true"                    android:background="@null"                    android:contentDescription="@null"                    android:src="@drawable/rulerscale_vertical_selected" />                <ScrollView                    android:id="@+id/vruler"                    android:layout_width="100dp"                    android:layout_height="fill_parent"                    android:scrollbars="none" >                    <LinearLayout                        android:id="@+id/vruler_layout"                        android:layout_width="wrap_content"                        android:layout_height="wrap_content"                        android:layout_marginLeft="10dp"                        android:layout_marginRight="10dp"                        android:gravity="center_vertical"                        android:orientation="vertical" >                    LinearLayout>                ScrollView>            RelativeLayout>        LinearLayout>    LinearLayout>LinearLayout>

第二步:竖向空白刻度布局,blankvrulerunit.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:layout_marginBottom="20dp"    android:layout_marginTop="5dp"    android:background="@null"    android:orientation="vertical" >    <TextView        android:id="@+id/vrulerunit"        android:layout_width="50dp"        android:layout_height="20dp"        android:layout_alignParentLeft="true"        android:layout_alignParentTop="true"        android:background="@null"        android:textColor="#000000"        android:textSize="14sp" />RelativeLayout>

第三步:中间刻度尺布局,vrulerunit.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:layout_marginBottom="20dp"    android:layout_marginTop="5dp"    android:orientation="vertical" >    <ImageView        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:layout_marginRight="30dp"        android:background="@null"        android:contentDescription="@null"        android:scaleType="fitXY"        android:src="@drawable/rulerscale_vertical" />    <TextView        android:id="@+id/vrulerunit"        android:layout_width="50dp"        android:layout_height="20dp"        android:layout_alignParentRight="true"        android:layout_alignParentTop="true"        android:background="@null"        android:textColor="#000000"        android:textSize="14sp" />RelativeLayout>

第四步:MainActivity.java主代码实现:

public class MainActivity extends Activity {        private ScrollView ruler;        private TextView user_height_value;        private LinearLayout rulerlayout;        private String height = "170";        @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initViews();    }        @Override    protected void onResume() {                super.onResume();                new Handler().postDelayed(new Runnable() {                        @Override            public void run() {                scroll();            }        }, 400);    }        private void scroll() {        ruler.smoothScrollTo(0, 1700);    }        protected void initViews() {        user_height_value = (TextView) findViewById(R.id.user_height_value);        user_height_value.setText("身高是: " + height + " CM");        ruler = (ScrollView) findViewById(R.id.vruler);        ruler.setOnTouchListener(new OnTouchListener() {            @Override            public boolean onTouch(View v, MotionEvent event) {                int action = event.getAction();             user_height_value.setText("身高是: "                        + String.valueOf((int) Math.ceil((ruler.getScrollY()) / 10))                        + " CM");                            switch (action) {                                case MotionEvent.ACTION_DOWN:                                case MotionEvent.ACTION_MOVE:                                    break;                                case MotionEvent.ACTION_UP:                                           new Handler().postDelayed(new Runnable() {                        @Override                        public void run() {                            user_height_value.setText("身高是: "                                    + String.valueOf((int) Math.ceil((ruler                                            .getScrollY()) / 10)) + " CM");                        }                    }, 1000);                                        break;                }                                return false;            }        });        rulerlayout = (LinearLayout) findViewById(R.id.vruler_layout);        new Handler().postDelayed((new Runnable() {            @Override            public void run() {                constructRuler();            }        }), 300);    }        private void constructRuler() {                int rulerHeight = ruler.getHeight();        View topview = (View) LayoutInflater.from(this).inflate(                R.layout.blankvrulerunit, null);        topview.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,                rulerHeight / 2));        rulerlayout.addView(topview);                for (int i = 0; i < 25; i++) {            View view = (View) LayoutInflater.from(this).inflate(                    R.layout.vrulerunit, null);            view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,                    100));            TextView tv = (TextView) view.findViewById(R.id.vrulerunit);            tv.setText(String.valueOf(i * 10));            rulerlayout.addView(view);        }        View bottomview = (View) LayoutInflater.from(this).inflate(                R.layout.blankvrulerunit, null);        bottomview.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,                rulerHeight / 2));        rulerlayout.addView(bottomview);    }}

索要源码的方式很简单,跟以前一样,在公众号(非著名程序员)里回复关键字“懒得做”即可获得,欢迎大家关注,转发和分享。

友情提示:

关键字别打错,也别给我私人微信号里去回复关键字,是公众号,最近老有人向我个人私人微信号发关键字,你们还挺瞧得起我,显得我很智能啊


更多相关文章

  1. Android简单、高性能的高斯模糊(毛玻璃)效果(附源码)
  2. android 仿 ios 搜索界面跳转效果
  3. Android(安卓)应用的动画实践--View Animation篇
  4. (4.2.2.1)【android开源工具】【Android(安卓)UI设计与开发】第16
  5. Android实用视图动画及工具系列之八:带头部的Viewpaper,结合头部的
  6. 分享 Ionic 开发 Hybrid App 中遇到的问题以及后期发布 iOS/Andr
  7. Android(安卓)沉浸式状态栏(QQ5.0透明状态栏的效果)
  8. Android桌面悬浮窗进阶,QQ手机管家小火箭效果实现
  9. Android学习小Demo(5)结合Matrix跟Porperty Animatin 实现推拉门效

随机推荐

  1. ch08 Android Intent
  2. Android(安卓)仿美团网,大众点评购买框悬
  3. android 中buildToolsVersion和dependenc
  4. android zxing生成二维码
  5. .net程序员转战android第一篇---环境部署
  6. android submenu
  7. android的Drawable
  8. android assets/raw 大文件读取
  9. Android 双击和手势的图片缩放
  10. android HttpClient 上传图片