国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android實(shí)現(xiàn)垂直進(jìn)度條VerticalSeekBar

2019-10-21 21:46:05
字體:
供稿:網(wǎng)友

本文實(shí)例為大家分享了Android實(shí)現(xiàn)垂直進(jìn)度條的具體代碼,供大家參考,具體內(nèi)容如下

水平的進(jìn)度條見多了,總會(huì)想見個(gè)垂直的進(jìn)度條開開眼。今天咱就試試。 
要說原理也簡單,就是把寬高倒置,其他的理論上都不需要?jiǎng)樱l(fā)現(xiàn)問題再補(bǔ)補(bǔ)也就行了。

官方提供

官方是提供了垂直進(jìn)度條的例子源碼的,位置在android-sdk-windows/sources/android-23/com/android/example/rscamera/VerticalSeekBar.java,當(dāng)然首先你SDK中要有Android 6.0。

VerticalSeekBar.java

/** * Class to create a vertical slider */public class VerticalSeekBar extends SeekBar {  public VerticalSeekBar(Context context) {    super(context);  }  public VerticalSeekBar(Context context, AttributeSet attrs, int defStyle) {    super(context, attrs, defStyle);  }  public VerticalSeekBar(Context context, AttributeSet attrs) {    super(context, attrs);  }  protected void onSizeChanged(int w, int h, int oldw, int oldh) {    super.onSizeChanged(h, w, oldh, oldw);  }  @Override  protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {    super.onMeasure(heightMeasureSpec, widthMeasureSpec);    setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());  }  protected void onDraw(Canvas c) {    c.rotate(-90);    c.translate(-getHeight(), 0);    super.onDraw(c);  }  @Override  public boolean onTouchEvent(MotionEvent event) {    if (!isEnabled()) {      return false;    }    switch (event.getAction()) {      case MotionEvent.ACTION_DOWN:      case MotionEvent.ACTION_MOVE:      case MotionEvent.ACTION_UP:        setProgress(getMax() - (int) (getMax() * event.getY() / getHeight()));        onSizeChanged(getWidth(), getHeight(), 0, 0);        break;      case MotionEvent.ACTION_CANCEL:        break;    }    return true;  }}

繼承SeekBar是最簡單快捷的,不用重寫太多方法,只需要把 

onMeasure 
onSizeChanged 
onDraw 

三個(gè)方法作一些改動(dòng);但也有一些問題,比如只能響應(yīng)onPregress方法,為了讓他能響應(yīng)onStartTrackingTouch和onStopTrackingTouch方法,只好再加一些代碼,于是有了改進(jìn)版。

稍作改進(jìn)

VerticalSeekBar2.java

public class VerticalSeekBar2 extends SeekBar {  private Drawable mThumb;  private OnSeekBarChangeListener mOnSeekBarChangeListener;  public VerticalSeekBar2(Context context) {    super(context);  }  public VerticalSeekBar2(Context context, AttributeSet attrs) {    super(context, attrs);  }  public VerticalSeekBar2(Context context, AttributeSet attrs, int defStyle) {    super(context, attrs, defStyle);  }  public void setOnSeekBarChangeListener(OnSeekBarChangeListener l) {    mOnSeekBarChangeListener = l;  }  protected void onSizeChanged(int w, int h, int oldw, int oldh) {    super.onSizeChanged(h, w, oldh, oldw);  }  @Override  protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {    super.onMeasure(heightMeasureSpec, widthMeasureSpec);    setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());  }  protected void onDraw(Canvas c) {    c.rotate(-90);    c.translate(-getHeight(), 0);    super.onDraw(c);  }  void onProgressRefresh(float scale, boolean fromUser) {    Drawable thumb = mThumb;    if (thumb != null) {      setThumbPos(getHeight(), thumb, scale, Integer.MIN_VALUE);      invalidate();    }    if (mOnSeekBarChangeListener != null) {      mOnSeekBarChangeListener.onProgressChanged(this, getProgress(), fromUser);    }  }  private void setThumbPos(int w, Drawable thumb, float scale, int gap) {    int available = w - getPaddingLeft() - getPaddingRight();    int thumbWidth = thumb.getIntrinsicWidth();    int thumbHeight = thumb.getIntrinsicHeight();    int thumbPos = (int) (scale * available + 0.5f);    // int topBound = getWidth() / 2 - thumbHeight / 2 - getPaddingTop();    // int bottomBound = getWidth() / 2 + thumbHeight / 2 - getPaddingTop();    int topBound, bottomBound;    if (gap == Integer.MIN_VALUE) {      Rect oldBounds = thumb.getBounds();      topBound = oldBounds.top;      bottomBound = oldBounds.bottom;    } else {      topBound = gap;      bottomBound = gap + thumbHeight;    }    thumb.setBounds(thumbPos, topBound, thumbPos + thumbWidth, bottomBound);  }  public void setThumb(Drawable thumb) {    mThumb = thumb;    super.setThumb(thumb);  }  void onStartTrackingTouch() {    if (mOnSeekBarChangeListener != null) {      mOnSeekBarChangeListener.onStartTrackingTouch(this);    }  }  void onStopTrackingTouch() {    if (mOnSeekBarChangeListener != null) {      mOnSeekBarChangeListener.onStopTrackingTouch(this);    }  }  private void attemptClaimDrag() {    if (getParent() != null) {      getParent().requestDisallowInterceptTouchEvent(true);    }  }  @Override  public boolean onTouchEvent(MotionEvent event) {    if (!isEnabled()) {      return false;    }    switch (event.getAction()) {    case MotionEvent.ACTION_DOWN:      setPressed(true);      onStartTrackingTouch();      break;    case MotionEvent.ACTION_MOVE:      attemptClaimDrag();      setProgress(getMax() - (int) (getMax() * event.getY() / getHeight()));      break;    case MotionEvent.ACTION_UP:      onStopTrackingTouch();      setPressed(false);      break;    case MotionEvent.ACTION_CANCEL:      onStopTrackingTouch();      setPressed(false);      break;    }    return true;  }

為了響應(yīng)另外兩個(gè)不知道怎么就被onPregress拋棄的方法,添了這么多代碼真是罪過,不過都是從SeekBar的父類AbsSeekBar中仿寫過來的,邏輯稍作改動(dòng)就能用。

對(duì)比測(cè)試

上圖。

Android,垂直進(jìn)度條,VerticalSeekBar

左邊是官方例子中的,右邊是改進(jìn)過的。

測(cè)試源碼:垂直進(jìn)度條VerticalSeekBar

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到Android開發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 临澧县| 左云县| 洪泽县| 巨野县| 繁峙县| 皋兰县| 利辛县| 隆子县| 巍山| 衡阳县| 凌源市| 林口县| 建阳市| 衡山县| 宿松县| 西青区| 咸宁市| 利辛县| 太白县| 长治市| 驻马店市| 呼和浩特市| 德惠市| 衡南县| 纳雍县| 麻栗坡县| 铁岭市| 左权县| 平谷区| 镇安县| 汉沽区| 镇坪县| 乌审旗| 合江县| 舟山市| 万全县| 耒阳市| 都匀市| 木里| 新竹市| 瑞丽市|