首页 > 代码库 > 触摸滑动实现直线选择

触摸滑动实现直线选择

先看效果图:


源代码:

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;
	
	@Override
	protected 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);
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		if(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;
		}
	}
}


触摸滑动实现直线选择