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

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

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

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

效果如下:


效果图

第一步: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);
    }

}

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

友情提示:

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

你可能感兴趣的:(Android实现竖着的滑动刻度尺效果,选择身高(竖向的))