最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android實現(xiàn)百分比下載進度條效果

 更新時間:2018年12月31日 09:21:05   作者:一小沫一  
這篇文章主要為大家詳細介紹了Android實現(xiàn)百分比下載進度條效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下

現(xiàn)在很多APP中都會集成下載功能,所以有一個方便好看又實用的進度條來展示下載進度很有必要,也能提高用戶體驗,在這里我就把項目里的下載進度條抽取出來分享給大家,話不多說,先看效果圖:


這個進度條是自定義的一個View,其中有一個自定義屬性就是百分比文字的大?。ㄒ部梢园涯莾蓷l顯示顏色的進度條自定義屬性,這里就沒有實現(xiàn),在代碼里面寫的)。

先說說實現(xiàn)原理:

1:由于自定義了屬性,所以先獲取屬性的值。

2:繪制底色那條灰色的線。

3:根據(jù)傳入的數(shù)據(jù)計算當前百分比,然后繪制那條橘黃色的線。

4:再在橘黃色線后面把百分比的文字繪制出來就OK了。

現(xiàn)在來看看代碼:

一:屬性設置attrs.xml文件

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
 <declare-styleable name="downloadProgressBar"> 
 <attr name="dptextsize" format="dimension"/> 
 </declare-styleable> 
</resources> 

其中dptextsize就是自定義屬性的名字

二:自定義view DownLoadProgressbar.Java

package com.ywl5320.downloadprogressdemo.downloadview; 
 
import android.content.Context; 
import android.content.res.TypedArray; 
import android.graphics.Canvas; 
import android.graphics.Paint; 
import android.graphics.Rect; 
import android.util.AttributeSet; 
import android.view.View; 
import android.view.ViewTreeObserver.OnGlobalLayoutListener; 
 
import com.ywl5320.downloadprogressdemo.R; 
 
/** 
 * 下載進度條 
 * 
 * @author ywl 
 * 
 */ 
public class DownLoadProgressbar extends View { 
 
 private Paint paint = new Paint(); // 繪制背景灰色線條畫筆 
 private Paint paintText = new Paint(); // 繪制下載進度畫筆 
 private float offset = 0f; // 下載偏移量 
 private float maxvalue = 0f; // 下載的總大小 
 private float currentValue = 0f; // 下載了多少 
 private Rect mBound = new Rect(); // 獲取百分比數(shù)字的長寬 
 private String percentValue = "0%"; // 要顯示的現(xiàn)在百分比 
 private float offsetRight = 0f; // 灰色線條距離右邊的距離 
 private int textSize = 25; // 百分比的文字大小 
 private float offsetTop = 18f; // 距離頂部的偏移量 
 
 public DownLoadProgressbar(Context context) { 
 this(context, null); 
 // TODO Auto-generated constructor stub 
 } 
 
 public DownLoadProgressbar(Context context, AttributeSet attribute) { 
 this(context, attribute, 0); 
 } 
 
 public DownLoadProgressbar(Context context, AttributeSet attrs, 
  int defStyleAttr) { 
 super(context, attrs, defStyleAttr); 
 // TODO Auto-generated constructor stub 
 // 獲取自定義屬性,給textsize賦初始值 
 TypedArray t = getContext().obtainStyledAttributes(attrs, 
  R.styleable.downloadProgressBar); 
 textSize = (int) t.getDimension( 
  R.styleable.downloadProgressBar_dptextsize, 36); 
 getTextWidth(); 
 } 
 
 @Override 
 protected void onDraw(Canvas canvas) { 
 // TODO Auto-generated method stub 
 super.onDraw(canvas); 
 // 繪制底色 
 paint.setColor(getResources().getColor(R.color.no1_gray_light)); 
 paint.setStrokeWidth(1); 
 canvas.drawLine(0, offsetTop, getWidth(), offsetTop, paint); 
 // 繪制進度條顏色 
 paint.setColor(getResources().getColor(R.color.no2_orange)); 
 paint.setStrokeWidth(2); 
 canvas.drawLine(0, offsetTop, offset, offsetTop, paint); 
 // 繪制白色區(qū)域及百分比 
 paint.setColor(getResources().getColor(R.color.no3_white)); 
 paint.setStrokeWidth(1); 
 paintText.setColor(getResources().getColor(R.color.no2_orange)); 
 paintText.setTextSize(textSize); 
 paintText.setAntiAlias(true); 
 paintText.getTextBounds(percentValue, 0, percentValue.length(), mBound); 
 canvas.drawLine(offset, offsetTop, offset + mBound.width() + 4, offsetTop, paint); 
 canvas.drawText(percentValue, offset, offsetTop + mBound.height() / 2 - 2, paintText); 
 } 
 
 /** 
 * 設置當前進度值 
 * 
 * @param currentValue 
 */ 
 public void setCurrentValue(float currentValue) { 
 this.currentValue = currentValue; 
 int value = (int) (this.currentValue / maxvalue * 100); 
 if (value < 100) { 
  percentValue = value + "%"; 
 } else { 
  percentValue = "100%"; 
 } 
 initCurrentProgressBar(); 
 invalidate(); 
 } 
 
 /** 
 * 設置最大值 
 * 
 * @param maxValue 
 */ 
 public void setMaxValue(float maxValue) { 
 this.maxvalue = maxValue; 
 } 
 
 /** 
 * 獲取當前進度條長度 
 * 
 * @param maxValue 
 * @param currentValue 
 * @return 
 */ 
 public void initCurrentProgressBar() { 
 getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
 
  @Override 
  public void onGlobalLayout() { 
  // TODO Auto-generated method stub 
  if (currentValue < maxvalue) { 
   offset = (getWidth() - offsetRight) * currentValue / maxvalue; 
  } else { 
   offset = getWidth() - offsetRight; 
  } 
  } 
 }); 
 
 } 
 
 /** 
 * 獲取“100%”的寬度 
 */ 
 public void getTextWidth() { 
 Paint paint = new Paint(); 
 Rect rect = new Rect(); 
 paint.setTextSize(textSize); 
 paint.setAntiAlias(true); 
 paint.getTextBounds("100%", 0, "100%".length(), rect); 
 offsetRight = rect.width() + 5; 
 } 
} 

這就是實現(xiàn)代碼,代碼不多,注解也有,不是很難。使用時只需傳入文件最大值,當前下載了多少就能自動計算出百分比。如果循環(huán)傳入,就實現(xiàn)了動態(tài)跑動的效果。

三:Activity布局文件 activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:tools="http://schemas.android.com/tools" 
 xmlns:ywl="http://schemas.android.com/apk/res/com.ywl5320.downloadprogressdemo" <!-- 這就是為自定義屬性添加命名空間,注意res后的是:程序包名--> 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:background="@color/no3_white" 
 tools:context="${relativePackage}.${activityClass}" > 
 
 <TextView 
 android:id="@+id/tv_start" 
 android:layout_width="match_parent" 
 android:layout_height="50dip" 
 android:layout_below="@+id/rl_progress" 
 android:layout_marginTop="40dip" 
 android:layout_marginLeft="20dip" 
 android:layout_marginRight="20dip" 
 android:gravity="center" 
 android:background="@drawable/btn_blue_selector" 
 android:text="開始"/> 
 
 <RelativeLayout 
 android:id="@+id/rl_progress" 
 android:layout_width="match_parent" 
 android:layout_marginLeft="20dip" 
 android:layout_marginRight="20dip" 
 android:layout_marginTop="30dip" 
 android:layout_height="50dip"> 
 <TextView 
  android:id="@+id/tv_size" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:layout_marginTop="4dip" 
  android:textColor="@color/no5_gray_silver" 
  android:textSize="12sp" /> 
 
 <TextView 
  android:id="@+id/tv_speed" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:layout_alignParentRight="true" 
  android:layout_marginTop="6dip" 
  android:textColor="@color/no5_gray_silver" 
  android:textSize="12sp" /> 
 
 <com.ywl5320.downloadprogressdemo.downloadview.DownLoadProgressbar 
  android:id="@+id/dp_game_progress" 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  ywl:dptextsize="14sp" <!-- 這里設置控件的字體大小為14sp --> 
  android:layout_below="@+id/tv_size"> 
 </com.ywl5320.downloadprogressdemo.downloadview.DownLoadProgressbar> 
 </RelativeLayout> 
 
</RelativeLayout> 

程序中的文件大小,當前下載量和下載速度,都是在這里布局的,用的時候可以動態(tài)設置就行了,也可以把這個布局文件封裝為listview的item布局文件,那樣就可以制作下載列表了。

四:MainAcativity.java

package com.ywl5320.downloadprogressdemo; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.os.Handler; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.TextView; 
 
import com.ywl5320.downloadprogressdemo.downloadview.DownLoadProgressbar; 
 
public class MainActivity extends Activity { 
 
 private TextView mStart; 
 private TextView mSize; 
 private TextView mSpeed; 
 private DownLoadProgressbar mProgress; 
 private int max = 100; //總的大小 
 private int current = 0; //當前下載大小 
 private String speed = "1"; //下載速度 
 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
 super.onCreate(savedInstanceState); 
 setContentView(R.layout.activity_main); 
 mStart = (TextView) findViewById(R.id.tv_start); 
 mProgress = (DownLoadProgressbar) findViewById(R.id.dp_game_progress); 
 mSize = (TextView) findViewById(R.id.tv_size); 
 mSpeed = (TextView) findViewById(R.id.tv_speed); 
 //初始化下載進度 
 mSize.setText(current + "MB/" + max + "MB"); 
 //初始化下載速度 
 mSpeed.setText(speed + "MB/s"); 
 mStart.setOnClickListener(new OnClickListener() { 
 
  @Override 
  public void onClick(View v) { 
  // TODO Auto-generated method stub 
  start(); 
  } 
 }); 
 } 
 
 //循環(huán)模擬下載過程 
 public void start() { 
 if (current <= max) { 
  mSize.setText(current + "MB/" + max + "MB"); 
  mSpeed.setText(speed + "MB/s"); 
  mProgress.setMaxValue(max); 
  mProgress.setCurrentValue(current); 
  handler.postDelayed(runnable, 100); 
 } else { 
  handler.removeCallbacks(runnable); 
 } 
 
 } 
 
 Handler handler = new Handler(); 
 Runnable runnable = new Runnable() { 
 @Override 
 public void run() { 
  // TODO Auto-generated method stub 
  current = current + 1; 
  start(); 
 } 
 }; 
 
} 

就這樣一個簡單實用的下載百分比進度條就實現(xiàn)了,有需要可以直接用就行:Android百分比下載進度條

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

相關文章

  • Android使用自定義View繪制漸隱漸現(xiàn)動畫

    Android使用自定義View繪制漸隱漸現(xiàn)動畫

    這篇文章主要介紹了Android使用自定義View繪制漸隱漸現(xiàn)動畫效果的相關內(nèi)容,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-09-09
  • 刷新Activity中的scrollview示例(局部ui刷新)

    刷新Activity中的scrollview示例(局部ui刷新)

    代碼很簡單,但是很實用,適合在一個Activity中要刷新局部的UI,比如在掃描一維碼的時候,要把每次掃描的結(jié)果都顯示在界面上
    2014-01-01
  • Android 實現(xiàn)閃屏頁和右上角的倒計時跳轉(zhuǎn)實例代碼

    Android 實現(xiàn)閃屏頁和右上角的倒計時跳轉(zhuǎn)實例代碼

    本文給大家分享一段實例代碼給大家介紹android實現(xiàn)閃屏頁和右上角的倒計時跳轉(zhuǎn)實例代碼,閃屏頁用到了handler和CountDownTimer類,還需配置一下Activity的主題,感興趣的朋友參考下吧
    2016-02-02
  • Kotlin自定義實現(xiàn)支付密碼數(shù)字鍵盤的方法實例

    Kotlin自定義實現(xiàn)支付密碼數(shù)字鍵盤的方法實例

    這篇文章主要給大家介紹了關于Kotlin如何自定義實現(xiàn)支付密碼數(shù)字鍵盤的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-07-07
  • Android中圖片壓縮方案詳解及源碼下載

    Android中圖片壓縮方案詳解及源碼下載

    這篇文章主要介紹了Android中圖片壓縮方案詳解及源碼下載的相關資料,需要的朋友可以參考下
    2017-03-03
  • Android SdkVersion的區(qū)別及獲取版本信息方法

    Android SdkVersion的區(qū)別及獲取版本信息方法

    下面小編就為大家?guī)硪黄狝ndroid SdkVersion的區(qū)別及獲取版本信息方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-03-03
  • Android Studio preview 不固定及常見問題的解決辦法

    Android Studio preview 不固定及常見問題的解決辦法

    preview 可以幫助您預覽您的布局文件將如何在用戶的設備上呈現(xiàn)。這篇文章主要介紹了Android Studio preview 不固定及常見問題的解決辦法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • Android實現(xiàn)圖片輪播切換實例代碼

    Android實現(xiàn)圖片輪播切換實例代碼

    利用Android的ViewFlipper和AnimationUtils實現(xiàn)圖片帶有動畫的輪播切換,其中當點擊“上一張”圖片時,切換到上一張圖片;當點擊“下一張”圖片時,切換到下一張圖片,本文給大家介紹Android實現(xiàn)圖片輪播切換實例代碼,需要的朋友參考下
    2015-12-12
  • Android ellipsize的小問題介紹

    Android ellipsize的小問題介紹

    使用TextView的時候,需要長度過長自動顯示省略號,android里有原生的支持,本文將針對此問題進行深入剖析,需要的朋友可以參考
    2012-11-11
  • Android計步功能的實現(xiàn)代碼

    Android計步功能的實現(xiàn)代碼

    本篇文章主要介紹了Android計步功能的實現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-03-03

最新評論

梁河县| 荃湾区| 义马市| 武夷山市| 邮箱| 盐城市| 东源县| 武乡县| 吉安县| 利辛县| 吉林省| 西盟| 麦盖提县| 武乡县| 荔浦县| 叙永县| 尉氏县| 滕州市| 尼玛县| 石柱| 灵武市| 得荣县| 遂川县| 玉溪市| 顺昌县| 门源| 黄石市| 乌拉特中旗| 观塘区| 洪雅县| 富民县| 贵阳市| 松桃| 广饶县| 大渡口区| 汤阴县| 湘潭市| 牡丹江市| 康马县| 井陉县| 岳池县|