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

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

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

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

效果如下:

Android实现竖着的滑动刻度尺效果,选择身高(竖向的)_第1张图片
效果图

第一步:activity_mian.xml布局:



    

        

            

            
        

        

            

                

                

                    
                    
                
            
        
    


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



    


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



    

    


第四步: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实现竖着的滑动刻度尺效果,选择身高(竖向的))