欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android自定義水平漸變進(jìn)度條

 更新時間:2017年09月27日 14:09:08   作者:hust_twj  
這篇文章主要為大家詳細(xì)介紹了Android自定義水平漸變進(jìn)度條,具有一定的參考價值,感興趣的小伙伴們可以參考一下

先看進(jìn)度條的效果:

具體實(shí)現(xiàn):

新建類,繼承自View,在onDraw中進(jìn)行繪制:

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 *
 * 自定義 進(jìn)度條
 * Created by wenjing.tang on 2017/8/7.
 */

public class CustomizedProgressBar extends View {

 private float maxCount = 100; //進(jìn)度條最大值
 private float currentCount; //進(jìn)度條當(dāng)前值
 // private Paint mPaint ;
 private int mWidth,mHeight;
 private Context mContext;

 public CustomizedProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
  initView(context);
 }

 public CustomizedProgressBar(Context context, AttributeSet attrs) {
  super(context, attrs);
  initView(context);
 }

 public CustomizedProgressBar(Context context) {
  super(context);
  initView(context);
 }

 private void initView(Context context) {
  mContext=context;
 }

 @Override
 protected void onDraw(Canvas canvas) {
  super.onDraw(canvas);
  Paint mPaint = new Paint();
  mPaint.setAntiAlias(true);
  int round = mHeight/2; //半徑

  mPaint.setColor(getResources().getColor(R.color.white_alpha)); //設(shè)置邊框背景顏色
  RectF rectBg = new RectF(0, 0, mWidth, mHeight);
  canvas.drawRoundRect(rectBg, round, round, mPaint);//繪制 最外面的大 圓角矩形,背景為白色

  float section = currentCount/maxCount; //進(jìn)度條的比例
  RectF rectProgressBg = new RectF(0, 0, mWidth*section, mHeight);

  Log.e("CustomizedProgressBar", currentCount+"");
  Log.e("CustomizedProgressBar", section+"");

  //Paint設(shè)置setColor(白色無透明)和setShader,只讓setShader生效;不然前面setColor設(shè)置了透明度,透明度會生效,和setShader效果疊加
  mPaint.setColor(getResources().getColor(R.color.white));
  mPaint.setShader(getLinearGradient());
  canvas.drawRoundRect(rectProgressBg, round, round, mPaint); //最左邊的圓角矩形

  if (maxCount != currentCount){ //如果不是100%,繪制第三段矩形
   RectF rectProgressBg2 = new RectF(mWidth*section-round, 0, mWidth*section, mHeight);
   mPaint.setShader(getLinearGradient());
   canvas.drawRect(rectProgressBg2, mPaint);
  }
 }

 private LinearGradient linearGradient;
 private LinearGradient getLinearGradient(){
  if(linearGradient==null){
   linearGradient = new LinearGradient(0, 0, getWidth(), mHeight, new int[]{mContext.getResources().getColor(R.color.progress_color_1),
     mContext.getResources().getColor(R.color.progress_color_2)}, null, Shader.TileMode.CLAMP); //根據(jù)R文件中的id獲取到color
  }
  return linearGradient;
 }

 private int dipToPx(int dip) {
  float scale = getContext().getResources().getDisplayMetrics().density;
  return (int) (dip * scale + 0.5f * (dip >= 0 ? 1 : -1));
 }

 /***
  * 設(shè)置最大的進(jìn)度值
  * @param maxCount 最大的進(jìn)度值
  */
 public void setMaxCount(float maxCount) {
  this.maxCount = maxCount;
 }

 /***
  * 設(shè)置當(dāng)前的進(jìn)度值
  * @param currentCount 當(dāng)前進(jìn)度值
  */
 public void setCurrentCount(float currentCount) {
  this.currentCount = currentCount > maxCount ? maxCount : currentCount;
  invalidate();
 }

 public float getMaxCount() {
  return maxCount;
 }

 public float getCurrentCount() {
  return currentCount;
 }

 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
  int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
  int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
  int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
  if (widthSpecMode == MeasureSpec.EXACTLY || widthSpecMode == MeasureSpec.AT_MOST) {
   mWidth = widthSpecSize;
  } else {
   mWidth = 0;
  }
  if (heightSpecMode == MeasureSpec.AT_MOST || heightSpecMode == MeasureSpec.UNSPECIFIED) {
   mHeight = dipToPx(18);
  } else {
   mHeight = heightSpecSize;
  }
  setMeasuredDimension(mWidth, mHeight);
 }
}

其中用到的一些資源文件如下:

<!--自定義進(jìn)度條背景顏色-->
<color name="white_alpha">#0c000000</color>

<!--自定義進(jìn)度條漸變顏色-->
<color name="progress_color_1">#ff916b</color>
<color name="progress_color_2">#ffa94c</color>

要注意的是,在上面Java代碼中,mPaint.setColor(getResources().getColor(R.color.white));這行很重要,因?yàn)檫M(jìn)度條總共有三層,第一層是最外面的背景,第二層是進(jìn)度,第三層如果不是100%才繪制,由于第一層背景有透明度,所以setColor設(shè)置了透明度,但雖然setShader,透明度還是會生效,兩者效果疊加,效果是這樣:

加上之后,Paint 第二次設(shè)置 setColor (白色無透明)和 setShader,只讓 setShader 生效,進(jìn)度條才會達(dá)到滿意的效果;

用法:

Java代碼中:

 customizedProgressBar.setMaxCount(100);
 integrity = dataCount/TOTAL_COUNT *100; //根據(jù)自己情況來初始化完整度
 customizedProgressBar.setCurrentCount((int) integrity);
 mTvtDataIntegrity.setText("完整度" + (int) integrity +"%");      

xml文件中(不需要文字顯示也可以):

 <RelativeLayout
     android:layout_width="match_parent"
     android:layout_height="18dp"
     android:layout_marginStart="66dp"
     android:layout_marginEnd="66dp"
     android:layout_centerVertical="true">

     <com.text.widget.CustomizedProgressBar
      android:id="@+id/progress"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_centerInParent="true"/>

     <TextView
      android:id="@+id/tv_data_integrity"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:gravity="center"
      tools:text="完整度35%"
      android:textSize="10sp"
      android:layout_centerInParent="true"/>
 </RelativeLayout>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 基于Android ContentProvider的總結(jié)詳解

    基于Android ContentProvider的總結(jié)詳解

    本篇文章是對Android ContentProvider進(jìn)行了詳細(xì)的總結(jié)與分析,需要的朋友參考下
    2013-05-05
  • Android集成微信支付功能

    Android集成微信支付功能

    這篇文章主要為大家詳細(xì)介紹了Android集成微信支付功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Android實(shí)現(xiàn)用文字生成圖片的示例代碼

    Android實(shí)現(xiàn)用文字生成圖片的示例代碼

    本篇文章主要介紹了Android實(shí)現(xiàn)用文字生成圖片的示例代碼,這里整理了詳細(xì)的代碼,非常具有實(shí)用價值,需要的朋友可以參考下
    2017-08-08
  • Android自定義拋出異常的方法詳解

    Android自定義拋出異常的方法詳解

    這篇文章主要給大家介紹了關(guān)于Android自定義拋出異常的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • 詳解Android?GLide圖片加載常用幾種方法

    詳解Android?GLide圖片加載常用幾種方法

    這篇文章主要為大家介紹了詳解Android?GLide圖片加載常用幾種方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • Android實(shí)現(xiàn)旋轉(zhuǎn),放大,縮小圖片的方法

    Android實(shí)現(xiàn)旋轉(zhuǎn),放大,縮小圖片的方法

    這篇文章主要介紹了Android實(shí)現(xiàn)旋轉(zhuǎn),放大,縮小圖片的方法,結(jié)合實(shí)例形式分析了Android基于Drawable針對圖片的縮放與旋轉(zhuǎn)等處理技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2016-10-10
  • 解析Kotlin?JSON格式

    解析Kotlin?JSON格式

    這篇文章主要介紹了Kotlin?JSON格式解析,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-03-03
  • Android實(shí)現(xiàn)手機(jī)游戲隱藏虛擬按鍵

    Android實(shí)現(xiàn)手機(jī)游戲隱藏虛擬按鍵

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)手機(jī)游戲隱藏虛擬按鍵,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • Android OkHttp基本使用詳解

    Android OkHttp基本使用詳解

    OkHttp是一個相對成熟的解決方案,需要的朋友可以參考下
    2018-03-03
  • Android SDK Manager無法更新問題解決辦法

    Android SDK Manager無法更新問題解決辦法

    這篇文章主要介紹了Android SDK Manager無法更新問題解決辦法的相關(guān)資料,需要的朋友可以參考下
    2017-04-04

最新評論