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

溫馨提示×

溫馨提示×

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

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

Android實現帶節點的進度條

發布時間:2020-09-16 11:29:41 來源:腳本之家 閱讀:221 作者:Silicon_Valley_DH 欄目:移動開發

日常的開發中經常會需要用到自定義View,這次剛好有個需求,需要用到帶有節點的進度條。東西很簡單直接繼承View就行了。

Android實現帶節點的進度條

首先定義一些需要的屬性

/**
 * 背景畫筆
 */
 private Paint bgPaint;
 /**
 * 前景畫筆
 */
 private Paint forePaint;
 /**
 * 選中畫筆
 */
 private Paint selectPaint;
 /**
 * 未選中畫筆
 */
 private Paint unselectPaint;
 /**
 * 背景顏色
 */
 private int bgColor = Color.parseColor("#9C9C9C");
 /**
 * 前景顏色
 */
 private int foreColor = Color.parseColor("#8A2BE2");
 /**
 * 默認高度
 */
 private int defaultHeight;
 /**
 * 節點文字
 */
 private List<String> nodeList;
 private List<Rect> mBounds;
 /**
 * 節點圓的半徑
 */
 private int radius;
 /**
 * 文字和節點進度條的top
 */
 private int marginTop;
 /**
 * 兩個節點之間的距離
 */
 private int dividWidth;
 /**
 * 選中位置
 */
 private int selectIndex;

然后在構造方法中初始化這些數據

private void init(Context context) {
 radius = SizeUtils.dp2px(context,4);
 defaultHeight = SizeUtils.dp2px(context,30);
 marginTop = SizeUtils.dp2px(context,5);

 bgPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
 bgPaint.setColor(bgColor);
 bgPaint.setStyle(Paint.Style.FILL);

 forePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
 forePaint.setColor(foreColor);
 forePaint.setStyle(Paint.Style.FILL);

 unselectPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
 unselectPaint.setColor(bgColor);
 unselectPaint.setTextSize(SizeUtils.sp2px(context,10));

 selectPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
 selectPaint.setColor(foreColor);
 selectPaint.setTextSize(SizeUtils.sp2px(context,10));
 }

設置節點文字

/**
 * 設置數據
 * @param nodeDatas
 */
 public void setNodeList(List<String> nodeDatas){
 if(nodeDatas != null){
 nodeList = nodeDatas;
 }
 //測量文字所占用的空間
 measureText();
 }

 /**
 * 設置選中位置
 * @param selectIndex
 */
 public void setSelectIndex(int selectIndex){
 this.selectIndex = selectIndex;
 invalidate();
 }
/**
 * 測量文字的長寬
 */
 private void measureText(){
 mBounds = new ArrayList<>();
 for (int i = 0; i < nodeList.size(); i++) {
 Rect mBound = new Rect();
 unselectPaint.getTextBounds(nodeList.get(i),0,nodeList.get(i).length(),mBound);
 mBounds.add(mBound);
 }
 }

最后重要的步驟,開始在onDraw中繪制節點進度條和繪制文字

1、繪制灰色背景線條

if(nodeList == null || nodeList.isEmpty()){
 return;
 }
 bgPaint.setStrokeWidth(radius/2);
 //繪制灰色的背景線條
 canvas.drawLine(radius,radius,getWidth()-radius,radius,bgPaint);

2、繪制節點上的圓和兩個節點之間的間隔線條

//畫節點圓
 //每個圓相隔的距離
 dividWidth = (getWidth()-radius*2)/(nodeList.size() - 1);
 forePaint.setStrokeWidth(radius/2);
 for (int i = 0; i < nodeList.size(); i++) {
 if(i == selectIndex){
 for (int j = 0; j <= i; j++) {
  canvas.drawCircle(radius+ j * dividWidth, radius, radius , forePaint);
  canvas.drawLine(radius,radius,j*dividWidth,radius,forePaint);
 }
 }else if(i>selectIndex){
 canvas.drawCircle(radius + i * dividWidth, radius, radius, bgPaint);
 }
 }

3、繪制節點上的文字

for (int i = 0; i < nodeList.size(); i++) {
 int currentTextWidth=mBounds.get(i).width();
 if (i==0){
 if (i==selectIndex){
  canvas.drawText(nodeList.get(i), 0, radius*2 + marginTop + mBounds.get(i).height()/2, selectPaint);
 }else if(i>selectIndex) {
  canvas.drawText(nodeList.get(i), 0, radius*2 + marginTop + mBounds.get(i).height()/2, unselectPaint);
 }
 }else if (i==nodeList.size()-1){
 if (i==selectIndex){
  for (int j = 0; j <= i; j++) {
  if(j == 0){
  canvas.drawText(nodeList.get(j), 0, radius*2 + marginTop + mBounds.get(j).height()/2, selectPaint);
  }else if(j == i){
  canvas.drawText(nodeList.get(j), getWidth() - currentTextWidth, radius*2 + marginTop + mBounds.get(j).height()/2, selectPaint);
  }else{
  canvas.drawText(nodeList.get(j), radius + j * dividWidth - currentTextWidth / 2, radius*2 + marginTop + mBounds.get(j).height()/2, selectPaint);
  }
  }
 }else if(i>selectIndex) {
  canvas.drawText(nodeList.get(i), getWidth() - currentTextWidth, radius*2 + marginTop + mBounds.get(i).height()/2, unselectPaint);
 }
 }else {
 if (i==selectIndex){
  for (int j = 0; j <= i; j++) {
  if(j>0){
  canvas.drawText(nodeList.get(j), radius + j * dividWidth - currentTextWidth / 2, radius*2 + marginTop + mBounds.get(j).height()/2, selectPaint);
  }else{
  canvas.drawText(nodeList.get(j), 0, radius*2 + marginTop + mBounds.get(j).height()/2, selectPaint);
  }
  }
 }else if(i>selectIndex) {
  canvas.drawText(nodeList.get(i), radius + i * dividWidth - currentTextWidth / 2, radius*2 + marginTop + mBounds.get(i).height()/2, unselectPaint);
 }
 }
 }

有時候可能需要的是下面這種進度條

Android實現帶節點的進度條

只需要修改onDraw中繪制節點圓和文字的方法就可以了

for (int i=0; i < nodeList.size();i++) {
 if (i==selectIndex){
 canvas.drawCircle(radius+ i * dividWidth, radius, radius , forePaint);
 }else {
 canvas.drawCircle(radius + i * dividWidth, radius, radius, bgPaint);
 }
 }

 for (int i=0; i<nodeList.size();i++){
 int currentTextWidth=mBounds.get(i).width();
 if (i==0){
 if (i==selectIndex){
  canvas.drawText(nodeList.get(i), 0, radius*2 + marginTop + mBounds.get(i).height()/2, selectPaint);
 }else {
  canvas.drawText(nodeList.get(i), 0, radius*2 + marginTop + mBounds.get(i).height()/2, unselectPaint);
 }
 }else if (i==nodeList.size()-1){
 if (i==selectIndex){
  canvas.drawText(nodeList.get(i), getWidth() - currentTextWidth, radius*2 + marginTop + mBounds.get(i).height()/2, selectPaint);
 }else {
  canvas.drawText(nodeList.get(i), getWidth() - currentTextWidth, radius*2 + marginTop + mBounds.get(i).height()/2, unselectPaint);
 }
 }else {
 if (i==selectIndex){
  canvas.drawText(nodeList.get(i), radius + i * dividWidth - currentTextWidth / 2, radius*2 + marginTop + mBounds.get(i).height()/2, selectPaint);
 }else {
  canvas.drawText(nodeList.get(i), radius + i * dividWidth - currentTextWidth / 2, radius*2 + marginTop + mBounds.get(i).height()/2, unselectPaint);
 }
 }
 }

點擊節點進行節點切換

// 實現節點切換,把注釋打開就行了
@Override
 public boolean onTouchEvent(MotionEvent event) {
 float eventX;
 float eventY;
 int i = event.getAction();
 if (i == MotionEvent.ACTION_DOWN) {
 } else if (i == MotionEvent.ACTION_MOVE) {
 } else if (i == MotionEvent.ACTION_UP) {
 eventX = event.getX();
 eventY = event.getY();
 //計算選中的index
 float select = eventX / dividWidth;
 float xs = select - (int) (select);
 //selectIndex = (int) select + (xs > 0.5 ? 1 : 0);
 }
 //invalidate();
 return true;
 }

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

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

AI

苏州市| 衢州市| 博爱县| 吐鲁番市| 崇明县| 陵川县| 安新县| 永宁县| 巫山县| 蒙城县| 深泽县| 拉孜县| 巴彦县| 南宁市| 徐水县| 乐平市| 合作市| 辽源市| 浠水县| 岑巩县| 台中市| 延庆县| 大洼县| 溧水县| 绍兴县| 泊头市| 莫力| 永平县| 江陵县| 舞钢市| 延津县| 石狮市| 蒲城县| 睢宁县| 长武县| 江都市| 临漳县| 小金县| 双辽市| 五华县| 勐海县|