Touch and slide to achieve Straight Line Selection

Source: Internet
Author: User

First look:


Source code:

package com.momo.selectvalue;import android.app.Activity;import android.os.Bundle;import android.view.MotionEvent;import android.view.View;import android.view.View.OnTouchListener;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;public class MainActivity extends Activity implements OnTouchListener {private ImageView strength_select;private LinearLayout strength_select_bg;private LinearLayout strength_select_tvs;private int typeStrength = 1 ;private int lastX;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);strength_select = (ImageView) findViewById(R.id.strength_select);strength_select.setOnTouchListener(this);strength_select_bg = (LinearLayout)findViewById(R.id.strength_select_bg);strength_select_tvs = (LinearLayout) findViewById(R.id.strength_select_tvs);strength_select_bg.setOnTouchListener(this);}@Overridepublic boolean onTouch(View v, MotionEvent event) {// TODO Auto-generated method stubif(v.getId() == R.id.strength_select_bg){setSlidePointPosition((int) event.getX());return false;}int width = strength_select_bg.getMeasuredWidth();int pointWidth = v.getWidth();switch(event.getAction()){case MotionEvent.ACTION_DOWN:lastX = (int) event.getRawX();strength_select_bg.requestDisallowInterceptTouchEvent(true);break;case MotionEvent.ACTION_MOVE:int detelX = (int) (event.getRawX() - lastX);int left = v.getLeft() + detelX;int right = left + pointWidth;if(right > width || left < 0){return false;}v.layout(left, v.getTop(), right, v.getBottom());lastX = (int) event.getRawX();break;case MotionEvent.ACTION_CANCEL:case MotionEvent.ACTION_UP:int left2 = (int) (v.getLeft() + pointWidth*0.5);setSlidePointPosition(left2);break;}return false;}private void setSlidePointPosition(int leftPosition){((TextView)strength_select_tvs.getChildAt(typeStrength*2-2)).setTextSize(12);int pointWidth = strength_select.getMeasuredWidth();int width = strength_select_bg.getMeasuredWidth();int unitWidth = (width - pointWidth)/6;ImageView v = strength_select;if(leftPosition > unitWidth*5+pointWidth/2){v.layout(width-pointWidth, v.getTop(), width, v.getBottom());((TextView)strength_select_tvs.getChildAt(6)).setTextSize(18);typeStrength = 4;}else if(leftPosition <= unitWidth*5+pointWidth/2 && leftPosition > unitWidth*3+pointWidth/2){v.layout(unitWidth*4, v.getTop(), unitWidth*4+pointWidth, v.getBottom());((TextView)strength_select_tvs.getChildAt(4)).setTextSize(18);typeStrength = 3;}else if(leftPosition > unitWidth+pointWidth/2 && leftPosition <= unitWidth*3+pointWidth/2){v.layout(unitWidth*2, v.getTop(), unitWidth*2+pointWidth, v.getBottom());((TextView)strength_select_tvs.getChildAt(2)).setTextSize(18);typeStrength = 2;}else{v.layout(0, v.getTop(), pointWidth, v.getBottom());((TextView)strength_select_tvs.getChildAt(0)).setTextSize(18);typeStrength = 1;}}}


Touch and slide to achieve Straight Line Selection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.