前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android实现竖着的滑动刻度尺效果,选择身高(竖向的)

Android实现竖着的滑动刻度尺效果,选择身高(竖向的)

作者头像
非著名程序员
发布2018-02-02 11:56:10
1K0
发布2018-02-02 11:56:10
举报
文章被收录于专栏:非著名程序员非著名程序员

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

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

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

效果如下:

第一步:activity_mian.xml布局:

代码语言:javascript
复制
<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:

代码语言:javascript
复制
<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:

代码语言:javascript
复制
<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主代码实现:

代码语言:javascript
复制
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);
    }

}
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2016-01-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 非著名程序员 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档