91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

在Android如何使用DoubleSeekBar雙向選擇控件

發布時間:2020-11-06 17:37:43 來源:億速云 閱讀:482 作者:Leah 欄目:開發技術

本篇文章為大家展示了在Android如何使用DoubleSeekBar雙向選擇控件,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

先看效果圖

在Android如何使用DoubleSeekBar雙向選擇控件

1.DoubleSlideSeekBar

public class DoubleSlideSeekBar extends View {
 /**
 * 線條(進度條)的寬度
 */
 private int lineWidth;
 /**
 * 線條(進度條)的長度
 */
 private int lineLength = 400;
 /**
 * 字所在的高度 100$
 */
 private int textHeight;
 /**
 * 游標 圖片寬度
 */
 private int imageWidth;
 /**
 * 游標 圖片高度
 */
 private int imageHeight;
 /**
 * 是否有刻度線
 */
 private boolean hasRule;
 /**
 * 左邊的游標是否在動
 */
 private boolean isLowerMoving;
 /**
 * 右邊的游標是否在動
 */
 private boolean isUpperMoving;
 /**
 * 字的大小 100$
 */
 private int textSize;
 /**
 * 字的顏色 100$
 */
 private int textColor;
 /**
 * 兩個游標內部 線(進度條)的顏色
 */
 private int inColor = Color.BLUE;
 /**
 * 兩個游標外部 線(進度條)的顏色
 */
 private int outColor = Color.BLUE;
 /**
 * 刻度的顏色
 */
 private int ruleColor = Color.BLUE;
 /**
 * 刻度上邊的字 的顏色
 */
 private int ruleTextColor = Color.BLUE;
 /**
 * 左邊圖標的圖片
 */
 private Bitmap bitmapLow;
 /**
 * 右邊圖標 的圖片
 */
 private Bitmap bitmapBig;
 /**
 * 左邊圖標所在X軸的位置
 */
 private int slideLowX;
 /**
 * 右邊圖標所在X軸的位置
 */
 private int slideBigX;
 /**
 * 圖標(游標) 高度
 */
 private int bitmapHeight;
 /**
 * 圖標(游標) 寬度
 */
 private int bitmapWidth;
 /**
 * 加一些padding 大小酌情考慮 為了我們的自定義view可以顯示完整
 */
 private int paddingLeft = 100;
 private int paddingRight = 100;
 private int paddingTop = 50;
 private int paddingBottom = 10;
 /**
 * 線(進度條) 開始的位置
 */
 private int lineStart = paddingLeft;
 /**
 * 線的Y軸位置
 */
 private int lineY;
 /**
 * 線(進度條)的結束位置
 */
 private int lineEnd = lineLength + paddingLeft;
 /**
 * 選擇器的最大值
 */
 private int bigValue = 100;
 /**
 * 選擇器的最小值
 */
 private int smallValue = 0;
 /**
 * 選擇器的當前最小值
 */
 private float smallRange;
 /**
 * 選擇器的當前最大值
 */
 private float bigRange;
 /**
 * 單位 元
 */
 private String unit = " ";
 /**
 * 單位份數
 */
 private int equal = 20;
 /**
 * 刻度單位 $
 */
 private String ruleUnit = " ";
 /**
 * 刻度上邊文字的size
 */
 private int ruleTextSize = 20;
 /**
 * 刻度線的高度
 */
 private int ruleLineHeight = 20;
 private Paint linePaint;
 private Paint bitmapPaint;
 private Paint textPaint;
 private Paint paintRule;
 
 public DoubleSlideSeekBar(Context context) {
 this(context, null);
 }
 
 public DoubleSlideSeekBar(Context context, @Nullable AttributeSet attrs) {
 this(context, attrs, 0);
 }
 
 public DoubleSlideSeekBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
 super(context, attrs, defStyleAttr);
 TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.DoubleSlideSeekBar, defStyleAttr, 0);
 int size = typedArray.getIndexCount();
 for (int i = 0; i < size; i++) {
  int type = typedArray.getIndex(i);
  switch (type) {
  case R.styleable.DoubleSlideSeekBar_inColor:
   inColor = typedArray.getColor(type, Color.BLACK);
   break;
  case R.styleable.DoubleSlideSeekBar_lineHeight:
   lineWidth = (int) typedArray.getDimension(type, dip2px(getContext(), 10));
   break;
  case R.styleable.DoubleSlideSeekBar_outColor:
   outColor = typedArray.getColor(type, Color.YELLOW);
   break;
  case R.styleable.DoubleSlideSeekBar_textColor:
   textColor = typedArray.getColor(type, Color.BLUE);
   break;
  case R.styleable.DoubleSlideSeekBar_textSize:
   textSize = typedArray.getDimensionPixelSize(type, (int) TypedValue.applyDimension(
    TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));
   break;
  case R.styleable.DoubleSlideSeekBar_imageLow:
   bitmapLow = BitmapFactory.decodeResource(getResources(), typedArray.getResourceId(type, 0));
   break;
  case R.styleable.DoubleSlideSeekBar_imageBig:
   bitmapBig = BitmapFactory.decodeResource(getResources(), typedArray.getResourceId(type, 0));
   break;
  case R.styleable.DoubleSlideSeekBar_imageheight:
   imageHeight = (int) typedArray.getDimension(type, dip2px(getContext(), 20));
   break;
  case R.styleable.DoubleSlideSeekBar_imagewidth:
   imageWidth = (int) typedArray.getDimension(type, dip2px(getContext(), 20));
   break;
  case R.styleable.DoubleSlideSeekBar_hasRule:
   hasRule = typedArray.getBoolean(type, false);
   break;
  case R.styleable.DoubleSlideSeekBar_ruleColor:
   ruleColor = typedArray.getColor(type, Color.BLUE);
   break;
  case R.styleable.DoubleSlideSeekBar_ruleTextColor:
   ruleTextColor = typedArray.getColor(type, Color.BLUE);
   break;
  case R.styleable.DoubleSlideSeekBar_unit:
   unit = typedArray.getString(type);
   break;
  case R.styleable.DoubleSlideSeekBar_equal:
   equal = typedArray.getInt(type, 10);
   break;
  case R.styleable.DoubleSlideSeekBar_ruleUnit:
   ruleUnit = typedArray.getString(type);
   break;
  case R.styleable.DoubleSlideSeekBar_ruleTextSize:
   ruleTextSize = typedArray.getDimensionPixelSize(type, (int) TypedValue.applyDimension(
    TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));
   break;
  case R.styleable.DoubleSlideSeekBar_ruleLineHeight:
   ruleLineHeight = (int) typedArray.getDimension(type, dip2px(getContext(), 10));
   break;
  case R.styleable.DoubleSlideSeekBar_bigValue:
   bigValue = typedArray.getInteger(type, 100);
   break;
  case R.styleable.DoubleSlideSeekBar_smallValue:
   smallValue = typedArray.getInteger(type, 100);
   break;
 
 
  default:
   break;
  }
 }
 typedArray.recycle();
 init();
 }
 
 private void init() {
 /**游標的默認圖*/
 if (bitmapLow == null) {
  bitmapLow = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
 }
 if (bitmapBig == null) {
  bitmapBig = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
 }
 /**游標圖片的真實高度 之后通過縮放比例可以把圖片設置成想要的大小*/
 bitmapHeight = bitmapLow.getHeight();
 bitmapWidth = bitmapLow.getWidth();
 // 設置想要的大小
 int newWidth = imageWidth;
 int newHeight = imageHeight;
 // 計算縮放比例
 float scaleWidth = ((float) newWidth) / bitmapWidth;
 float scaleHeight = ((float) newHeight) / bitmapHeight;
 Matrix matrix = new Matrix();
 matrix.postScale(scaleWidth, scaleHeight);
 /**縮放圖片*/
 bitmapLow = Bitmap.createBitmap(bitmapLow, 0, 0, bitmapWidth, bitmapHeight, matrix, true);
 bitmapBig = Bitmap.createBitmap(bitmapBig, 0, 0, bitmapWidth, bitmapHeight, matrix, true);
 /**重新獲取游標圖片的寬高*/
 bitmapHeight = bitmapLow.getHeight();
 bitmapWidth = bitmapLow.getWidth();
 /**初始化兩個游標的位置*/
 slideLowX = lineStart;
 slideBigX = lineEnd;
 smallRange = smallValue;
 bigRange = bigValue;
 if (hasRule) {
  //有刻度時 paddingTop 要加上(text高度)和(刻度線高度加刻度線上邊文字的高度和) 之間的最大值
  paddingTop = paddingTop + Math.max(textSize, ruleLineHeight + ruleTextSize);
 } else {
  //沒有刻度時 paddingTop 加上 text的高度
  paddingTop = paddingTop + textSize;
 }
 }
 
 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
 int width = getMyMeasureWidth(widthMeasureSpec);
 int height = getMyMeasureHeight(heightMeasureSpec);
 setMeasuredDimension(width, height);
 }
 
 private int getMyMeasureHeight(int heightMeasureSpec) {
 int mode = MeasureSpec.getMode(heightMeasureSpec);
 int size = MeasureSpec.getSize(heightMeasureSpec);
 if (mode == MeasureSpec.EXACTLY) {
  // matchparent 或者 固定大小 view最小應為 paddingBottom + paddingTop + bitmapHeight + 10 否則顯示不全
  size = Math.max(size, paddingBottom + paddingTop + bitmapHeight + 10);
 } else {
  //wrap content
  int height = paddingBottom + paddingTop + bitmapHeight + 10;
  size = Math.min(size, height);
 }
 return size;
 }
 
 private int getMyMeasureWidth(int widthMeasureSpec) {
 int mode = MeasureSpec.getMode(widthMeasureSpec);
 int size = MeasureSpec.getSize(widthMeasureSpec);
 if (mode == MeasureSpec.EXACTLY) {
 
  size = Math.max(size, paddingLeft + paddingRight + bitmapWidth * 2);
 
 } else {
  //wrap content
  int width = paddingLeft + paddingRight + bitmapWidth * 2;
  size = Math.min(size, width);
 }
 // match parent 或者 固定大小 此時可以獲取線(進度條)的長度
 lineLength = size - paddingLeft - paddingRight - bitmapWidth;
 //線(進度條)的結束位置
 lineEnd = lineLength + paddingLeft + bitmapWidth / 2;
 //線(進度條)的開始位置
 lineStart = paddingLeft + bitmapWidth / 2;
 //初始化 游標位置
 slideBigX = lineEnd;
 slideLowX = lineStart;
 return size;
 }
 
 @Override
 protected void onDraw(Canvas canvas) {
 super.onDraw(canvas);
 // Y軸 坐標
 lineY = getHeight() - paddingBottom - bitmapHeight / 2;
 // 字所在高度 100$
 textHeight = lineY - bitmapHeight / 2 - 10;
// //是否畫刻度
// if (hasRule) {
//  drawRule(canvas);
// }
// if (linePaint == null) {
//  linePaint = new Paint();
// }
 linePaint = new Paint();
 //畫內部線
 linePaint.setAntiAlias(true);
 linePaint.setStrokeWidth(lineWidth);
 linePaint.setColor(inColor);
 linePaint.setStrokeCap(Paint.Cap.ROUND);
 canvas.drawLine(slideLowX, lineY, slideBigX, lineY, linePaint);
 linePaint.setColor(outColor);
 linePaint.setStrokeCap(Paint.Cap.ROUND);
 //畫 外部線
 canvas.drawLine(lineStart, lineY, slideLowX, lineY, linePaint);
 canvas.drawLine(slideBigX, lineY, lineEnd, lineY, linePaint);
 //畫游標
 if (bitmapPaint == null) {
  bitmapPaint = new Paint();
 }
 canvas.drawBitmap(bitmapLow, slideLowX - bitmapWidth / 2, lineY - bitmapHeight / 2, bitmapPaint);
 canvas.drawBitmap(bitmapBig, slideBigX - bitmapWidth / 2, lineY - bitmapHeight / 2, bitmapPaint);
 //畫 游標上邊的字
 if (textPaint == null) {
  textPaint = new Paint();
 }
 textPaint.setColor(textColor);
 textPaint.setTextSize(textSize);
 textPaint.setAntiAlias(true);
 canvas.drawText(String.format("%.0f" + unit, smallRange), slideLowX - bitmapWidth / 2, textHeight, textPaint);
 canvas.drawText(String.format("%.0f" + unit, bigRange), slideBigX - bitmapWidth / 2, textHeight, textPaint);
 }
 
 @Override
 public boolean onTouchEvent(MotionEvent event) {
 //事件機制
 super.onTouchEvent(event);
 float nowX = event.getX();
 float nowY = event.getY();
 switch (event.getAction()) {
  case MotionEvent.ACTION_DOWN:
  //按下 在線(進度條)范圍上
  boolean rightY = Math.abs(nowY - lineY) < bitmapHeight / 2;
  //按下 在左邊游標上
  boolean lowSlide = Math.abs(nowX - slideLowX) < bitmapWidth / 2;
  //按下 在右邊游標上
  boolean bigSlide = Math.abs(nowX - slideBigX) < bitmapWidth / 2;
  if (rightY && lowSlide) {
   isLowerMoving = true;
  } else if (rightY && bigSlide) {
   isUpperMoving = true;
   //點擊了游標外部 的線上
  } else if (nowX >= lineStart && nowX <= slideLowX - bitmapWidth / 2 && rightY) {
   slideLowX = (int) nowX;
   updateRange();
   postInvalidate();
  } else if (nowX <= lineEnd && nowX >= slideBigX + bitmapWidth / 2 && rightY) {
   slideBigX = (int) nowX;
   updateRange();
   postInvalidate();
  }
  break;
  case MotionEvent.ACTION_MOVE:
  //左邊游標是運動狀態
  if (isLowerMoving) {
   //當前 X坐標在線上 且在右邊游標的左邊
   if (nowX <= slideBigX - bitmapWidth && nowX >= lineStart - bitmapWidth / 2) {
   slideLowX = (int) nowX;
   if (slideLowX < lineStart) {
    slideLowX = lineStart;
   }
   //更新進度
   updateRange();
   postInvalidate();
   }
 
  } else if (isUpperMoving) {
   //當前 X坐標在線上 且在左邊游標的右邊
   if (nowX >= slideLowX + bitmapWidth && nowX <= lineEnd + bitmapWidth / 2) {
   slideBigX = (int) nowX;
   if (slideBigX > lineEnd) {
    slideBigX = lineEnd;
   }
   //更新進度
   updateRange();
   postInvalidate();
 
   }
  }
  break;
  //手指抬起
  case MotionEvent.ACTION_UP:
  isUpperMoving = false;
  isLowerMoving = false;
  break;
  default:
  break;
 }
 
 return true;
 }
 
 private void updateRange() {
 //當前 左邊游標數值
 smallRange = computRange(slideLowX);
 //當前 右邊游標數值
 bigRange = computRange(slideBigX);
 //接口 實現值的傳遞
 if (onRangeListener != null) {
  onRangeListener.onRange(smallRange, bigRange);
 }
 }
 
 /**
 * 獲取當前值
 */
 private float computRange(float range) {
 return (range - lineStart) * (bigValue - smallValue) / lineLength + smallValue;
 }
 
 public int dip2px(Context context, float dpValue) {
 final float scale = context.getResources().getDisplayMetrics().density;
 return (int) (dpValue * scale + 0.5f);
 }
 
// /**
// * 畫刻度
// */
// protected void drawRule(Canvas canvas) {
// if (paintRule == null) {
//  paintRule = new Paint();
// }
// paintRule.setStrokeWidth(1);
// paintRule.setTextSize(ruleTextSize);
// paintRule.setTextAlign(Paint.Align.CENTER);
// paintRule.setAntiAlias(true);
// //遍歷 equal份,畫刻度
// for (int i = smallValue; i <= bigValue; i += (bigValue - smallValue) / equal) {
//  float degX = lineStart + i * lineLength / (bigValue - smallValue);
//  int degY = lineY - ruleLineHeight;
//  paintRule.setColor(ruleColor);
//  canvas.drawLine(degX, lineY, degX, degY, paintRule);
//  paintRule.setColor(ruleTextColor);
//  canvas.drawText(String.valueOf(i) + ruleUnit, degX, degY, paintRule);
// }
// }
 
 /**
 * 寫個接口 用來傳遞最大最小值
 */
 public interface onRangeListener {
 void onRange(float low, float big);
 }
 
 private onRangeListener onRangeListener;
 
 public void setOnRangeListener(DoubleSlideSeekBar.onRangeListener onRangeListener) {
 this.onRangeListener = onRangeListener;
 }
}

2.在values文件夾中創建attrs.xml

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<resources>
 <!--線(進度條)寬度-->
 <attr name="lineHeight" format="dimension" />
 <!--字的大小 100元-->
 <attr name="textSize" format="dimension" />
 <!--字的顏色 100元-->
 <attr name="textColor" format="color" />
 <!--兩個游標內部 線(進度條)的顏色-->
 <attr name="inColor" format="color" />
 <!--兩個游標外部 線(進度條)的顏色-->
 <attr name="outColor" format="color" />
 <!--左邊圖標的圖片-->
 <attr name="imageLow" format="reference"/>
 <!--右邊圖標 的圖片-->
 <attr name="imageBig" format="reference"/>
 <!--游標 圖片寬度-->
 <attr name="imagewidth" format="dimension" />
 <!--游標 圖片高度-->
 <attr name="imageheight" format="dimension" />
 <!--是否有刻度線-->
 <attr name="hasRule" format="boolean" />
 <!--刻度的顏色-->
 <attr name="ruleColor" format="color" />
 <!--刻度上邊的字 的顏色-->
 <attr name="ruleTextColor" format="color" />
 <!--單位 元-->
 <attr name="unit" format="string"/>
 <!--單位份數-->
 <attr name="equal" format="integer"/>
 <!--刻度單位 $-->
 <attr name="ruleUnit" format="string"/>
 <!--刻度上邊文字的size-->
 <attr name="ruleTextSize" format="dimension" />
 <!--刻度線的高度-->
 <attr name="ruleLineHeight" format="dimension" />
 <!--選擇器的最大值-->
 <attr name="bigValue" format="integer"/>
 <!--選擇器的最小值-->
 <attr name="smallValue" format="integer"/>
 <declare-styleable name="DoubleSlideSeekBar">
  <attr name="lineHeight" />
  <attr name="textSize" />
  <attr name="textColor" />
  <attr name="inColor" />
  <attr name="outColor" />
  <attr name="imageLow"/>
  <attr name="imageBig"/>
  <attr name="imagewidth" />
  <attr name="imageheight" />
  <attr name="hasRule" />
  <attr name="ruleColor" />
  <attr name="ruleTextColor" />
  <attr name="unit" />
  <attr name="equal" />
  <attr name="ruleUnit" />
  <attr name="ruleTextSize" />
  <attr name="ruleLineHeight" />
  <attr name="bigValue" />
  <attr name="smallValue" />
 </declare-styleable>
</resources> 

3.activity_main.XML布局

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 xmlns:custom="http://schemas.android.com/apk/res-auto"
 tools:context=".MainActivity"
 android:orientation="vertical">
 
 <com.XXX.XXX.DoubleSlideSeekBar
  android:id="@+id/doubleslide_withrule"
  android:layout_marginTop="20dp"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  custom:lineHeight="6dp"
  custom:textSize="12sp"
  custom:textColor="#0628e4"
  custom:inColor="#f10a0a"
  custom:outColor="#af08e2"
  custom:imagewidth="20dp"
  custom:imageheight="20dp"
  custom:hasRule="true"
  custom:ruleColor="#0e0e0e"
  custom:ruleTextColor="#f74104"
  custom:equal="10"
  custom:ruleTextSize="8sp"
  custom:ruleLineHeight="10dp"
  />
 <TextView
  android:id="@+id/tv_min_rule"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="0"
  android:layout_marginTop="10dp"></TextView>
 <TextView
  android:id="@+id/tv_max_rule"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="0"
  android:layout_marginTop="10dp"></TextView>
 
 
</LinearLayout>

4.MainActivity

public class MainActivity extends AppCompatActivity {
 
 private DoubleSlideSeekBar mDoubleslideWithrule;
 private TextView mTvMinRule;
 private TextView mTvMaxRule;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  mDoubleslideWithrule = (DoubleSlideSeekBar) findViewById(R.id.doubleslide_withrule);
  mTvMinRule = (TextView) findViewById(R.id.tv_min_rule);
  mTvMaxRule = (TextView) findViewById(R.id.tv_max_rule);
  setListener();
 }
 private void setListener() {
 
  mDoubleslideWithrule.setOnRangeListener(new DoubleSlideSeekBar.onRangeListener() {
   @Override
   public void onRange(float low, float big) {
    mTvMinRule.setText("最小值" + String.format("%.0f" , low));
    mTvMaxRule.setText("最大值" + String.format("%.0f" , big));
   }
  });
 }
}

上述內容就是在Android如何使用DoubleSeekBar雙向選擇控件,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

德兴市| 惠东县| 安塞县| 翼城县| 潮安县| 南平市| 大城县| 南投县| 正阳县| 双江| 桓台县| 西吉县| 栖霞市| 永宁县| 平和县| 聊城市| 西乡县| 明星| 新绛县| 灵璧县| 广德县| 桐城市| 日照市| 余干县| 周宁县| 靖江市| 财经| 定西市| 冷水江市| 淮滨县| 米易县| 青浦区| 临汾市| 邵阳县| 穆棱市| 南城县| 龙里县| 广灵县| 武定县| 崇文区| 微山县|