안 드 로 이 드 구현 연령 대 선택 기

본 논문 의 사례 는 안 드 로 이 드 가 연령 대 선택 기 를 실현 하 는 구체 적 인 코드 를 공유 하여 여러분 께 참고 하 시기 바 랍 니 다.구체 적 인 내용 은 다음 과 같 습 니 다.

효 과 는 미끄럼 원형 버튼 으로 시간 을 선택 하 는 것 입 니 다.쓸데없는 말 은 하지 않 고 도구 류 에 먼저 올 라 가 는 것 입 니 다.

import android.view.View;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.zhiziyun.dmptest.bot.R;


/**
 * Created by Administrator on 2018/7/27.
 */

public class RangeSeekBar extends View {
 private float lineWidth = 5.0f;
 private float textSize = 25.0f;

 private int inRangeColor = 0xff247ab7;
 private int outRangeColor = 0xff777777;
 private int textColor = 0xff247ab7;

 private int textMarginBottom = 10;

 private int lowerCenterX;
 private int upperCenterX;

 private int bmpWidth;
 private int bmpHeight;

 private Bitmap lowerBmp;
 private Bitmap upperBmp;

 private Paint inRangePaint;
 private Paint outRangePaint;
 private Paint bmpPaint;
 private Paint textPaint;

 private boolean isLowerMoving = false;
 private boolean isUpperMoving = false;

 private OnRangeChangedListener onRangeChangedListener;

 private int paddingLeft = 50;
 private int paddingRight = 50;
 private int paddingTop = 50;
 private int paddingBottom = 10;

 private int lineHeight;
 private int lineLength = 400;
 private int lineStart = paddingLeft;
 private int lineEnd = lineLength + paddingLeft;

 private float smallValue = 13.0f;//   
 private float bigValue = 60.0f;//   

 private float smallRange = smallValue;
 private float bigRange = bigValue;

 private int textHeight;

 public RangeSeekBar(Context context) {
  super(context);
  init();
 }

 public RangeSeekBar(Context context, AttributeSet attrs) {
  super(context, attrs);
  init();
 }

 public RangeSeekBar(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
  init();
 }

 private void init() {
  lowerBmp = BitmapFactory.decodeResource(getResources(),
    R.drawable.circular);//      ,    
  upperBmp = BitmapFactory.decodeResource(getResources(),
    R.drawable.circular);//      ,    

  bmpWidth = upperBmp.getWidth();
  bmpHeight = upperBmp.getHeight();

  lowerCenterX = lineStart;
  upperCenterX = lineEnd;

  lineHeight = getHeight() - paddingBottom - lowerBmp.getHeight() / 2;
  textHeight = lineHeight + lowerBmp.getHeight() / 2 + 10;

 }

 private void initPaint() {
  //         
  inRangePaint = new Paint();
  inRangePaint.setAntiAlias(true);
  inRangePaint.setStrokeWidth(lineWidth);
  inRangePaint.setColor(inRangeColor);

  //         
  outRangePaint = new Paint();
  outRangePaint.setAntiAlias(true);
  outRangePaint.setStrokeWidth(lineWidth);
  outRangePaint.setColor(outRangeColor);

  //      
  bmpPaint = new Paint();

  //      
  textPaint = new Paint();
  textPaint.setColor(textColor);
  textPaint.setTextSize(textSize);
  textPaint.setAntiAlias(true);
  textPaint.setStrokeWidth(lineWidth);
 }

 private int measureWidth(int measureSpec) {
  int result = 0;

  int specMode = MeasureSpec.getMode(measureSpec);
  int specSize = MeasureSpec.getSize(measureSpec);

  if (specMode == MeasureSpec.EXACTLY) {
   result = specSize;
  } else {
   result = paddingLeft + paddingRight + bmpWidth * 2;

   if (specMode == MeasureSpec.AT_MOST) {
    result = Math.min(result, specSize);
   }
  }

  return result;
 }

 private int measureHeight(int measureHeight) {
  int result = 0;

  int specMode = MeasureSpec.getMode(measureHeight);
  int specSize = MeasureSpec.getSize(measureHeight);

  if (specMode == MeasureSpec.EXACTLY) {
   result = bmpHeight * 2;
  } else {
   result = bmpHeight + paddingTop;

   if (specMode == MeasureSpec.AT_MOST) {
    result = Math.min(result, specSize);
   }
  }

  return result;
 }

 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  widthMeasureSpec = measureWidth(widthMeasureSpec);
  heightMeasureSpec = measureHeight(heightMeasureSpec);
  setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
 }

 @Override
 protected void onDraw(Canvas canvas) {
  super.onDraw(canvas);

  bmpWidth = upperBmp.getWidth();
  bmpHeight = upperBmp.getHeight();

  lineHeight = getHeight() - paddingBottom - lowerBmp.getHeight() / 2;
  textHeight = lineHeight - bmpHeight / 2 - textMarginBottom;

  //   
  Paint linePaint = new Paint();
  linePaint.setAntiAlias(true);
  linePaint.setStrokeWidth(lineWidth);

  //             
  linePaint.setColor(inRangeColor);
  canvas.drawLine(lowerCenterX, lineHeight, upperCenterX, lineHeight,
    linePaint);

  //              
  linePaint.setColor(outRangeColor);
  canvas.drawLine(lineStart, lineHeight, lowerCenterX, lineHeight,
    linePaint);
  canvas.drawLine(upperCenterX, lineHeight, lineEnd, lineHeight,
    linePaint);

  //      
  Paint bmpPaint = new Paint();
  canvas.drawBitmap(lowerBmp, lowerCenterX - bmpWidth / 2, lineHeight
    - bmpHeight / 2, bmpPaint);
  canvas.drawBitmap(lowerBmp, upperCenterX - bmpWidth / 2, lineHeight
    - bmpHeight / 2, bmpPaint);

  //      
  Paint textPaint = new Paint();
  textPaint.setColor(textColor);
  textPaint.setTextSize(textSize);
  textPaint.setAntiAlias(true);
  textPaint.setStrokeWidth(lineWidth);
  canvas.drawText(String.format("%.0f ", smallRange), lowerCenterX
    - bmpWidth / 2, textHeight, textPaint);
  canvas.drawText(String.format("%.0f ", bigRange), upperCenterX
    - bmpWidth / 2, textHeight, textPaint);
 }

 @Override
 public boolean onTouchEvent(MotionEvent event) {
  super.onTouchEvent(event);

  float xPos = event.getX();
  switch (event.getAction()) {
   case MotionEvent.ACTION_DOWN:
    //                    ,       
    float yPos = event.getY();
    if (Math.abs(yPos - lineHeight) > bmpHeight / 2) {
     return false;
    }

    //                
    if (Math.abs(xPos - lowerCenterX) < bmpWidth / 2) {
     isLowerMoving = true;
    }

    // //               
    if (Math.abs(xPos - upperCenterX) < bmpWidth / 2) {
     isUpperMoving = true;
    }

    //             ,            
    if (xPos >= lineStart && xPos <= lowerCenterX - bmpWidth / 2) {
     lowerCenterX = (int) xPos;
     updateRange();
     postInvalidate();
    }

    //             ,             
    if (xPos <= lineEnd && xPos >= upperCenterX + bmpWidth / 2) {
     upperCenterX = (int) xPos;
     updateRange();
     postInvalidate();
    }
    break;
   case MotionEvent.ACTION_MOVE:
    //        
    if (isLowerMoving) {
     if (xPos >= lineStart && xPos < upperCenterX - bmpWidth) {
      lowerCenterX = (int) xPos;
      updateRange();
      postInvalidate();
     }
    }

    //        
    if (isUpperMoving) {
     if (xPos > lowerCenterX + bmpWidth && xPos < lineEnd) {
      upperCenterX = (int) xPos;
      updateRange();
      postInvalidate();
     }
    }

    break;
   case MotionEvent.ACTION_UP:
    //               
    isLowerMoving = false;
    isUpperMoving = false;
    break;
   default:
    break;
  }

  return true;
 }

 //             
 private float computRange(float range) {
  return (range - lineStart) * (bigValue - smallValue) / lineLength
    + smallValue;
 }

 //         ,           
 private void updateRange() {
  smallRange = computRange(lowerCenterX);
  bigRange = computRange(upperCenterX);

  if (null != onRangeChangedListener) {
   onRangeChangedListener.onRangeChanged(smallRange, bigRange);
  }
 }

 //               
 public void setOnRangeChangedListener(
   OnRangeChangedListener onRangeChangedListener) {
  this.onRangeChangedListener = onRangeChangedListener;
 }

 //     ,            
 public interface OnRangeChangedListener {

  public void onRangeChanged(float lowerRange, float upperRange);

 }

}
xml 에서

<com.zhiziyun.dmptest.bot.util.RangeSeekBar
    android:id="@+id/rangeSeekBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
마지막 으로 코드 에서 호출

rangeSeekBar = (RangeSeekBar) findViewById(R.id.rangeSeekBar);

  rangeSeekBar.setOnRangeChangedListener(new RangeSeekBar.OnRangeChangedListener() {

   @Override
   public void onRangeChanged(float lowerRange, float upperRange) {
    tv_age.setText((int) lowerRange + "~" + (int) upperRange);
   }
  });
일 을 끝내다.
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기