日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > php >内容正文

php

android 自定义 进度条 旋转,Android_Android ProgressBar进度条使用详解,ProgressBar进度条,分为旋转进 - phpStudy...

發(fā)布時(shí)間:2024/9/18 php 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 自定义 进度条 旋转,Android_Android ProgressBar进度条使用详解,ProgressBar进度条,分为旋转进 - phpStudy... 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Android ProgressBar進(jìn)度條使用詳解

ProgressBar進(jìn)度條,分為旋轉(zhuǎn)進(jìn)度條和水平進(jìn)度條,進(jìn)度條的樣式根據(jù)需要自定義,之前一直不明白進(jìn)度條如何在實(shí)際項(xiàng)目中使用,網(wǎng)上演示進(jìn)度條的案例大多都是通過(guò)Button點(diǎn)擊增加、減少進(jìn)度值,使用方法incrementProgressBy(int),最簡(jiǎn)單的做法是在xml布局文件中放置ProgressBar空間,然后再M(fèi)ainActivity中觸發(fā)事件后執(zhí)行incrementProgressBy(int),代碼如下:

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context="cn.teachcourse.www.MainActivity"

android:orientation="vertical"

android:gravity="center"

android:id="@+id/onclick_ll" >

android:id="@+id/progressBar_horizontal"

style="?android:attr/progressBarStyleHorizontal"

android:layout_width="match_parent"

android:layout_height="15dp"

android:layout_marginTop="28dp"/>

在Java代碼中非常的簡(jiǎn)單,如下圖:

public class MainActivity extends ActionBarActivity implements View.OnClickListener{

private ProgressBar pb_large;

private ProgressBar pb_normal;

private ProgressBar pb_small;

private ProgressBar pb_horizontal;

private int readed;

private int progressValue;

private LinearLayout onclick_ll;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initView();

Drawable drawable = getResources().getDrawable(

R.drawable.progress_bar_states);

pb_horizontal.setProgressDrawable(drawable);//設(shè)置水平滾動(dòng)條的樣式

pb_horizontal.setMax(100);//設(shè)置進(jìn)度條的最大值

onclick_ll.setOnClickListener(this);//LinearLayout添加監(jiān)視器

}

private void initView() {

pb_horizontal = (ProgressBar) findViewById(R.id.progressBar_horizontal);

onclick_ll=(LinearLayout)findViewById(R.id.onclick_ll);

}

@Override

public void onClick(View v) {

pb_horizontal.incrementProgressBy(5);

if(pb_horizontal.getProgress()==pb_horizontal.getMax()){

Toast.makeText(this, "進(jìn)度條已經(jīng)達(dá)到最大值,進(jìn)度條消失", Toast.LENGTH_LONG).show();

pb_horizontal.setProgress(0); //當(dāng)達(dá)到最大之后,進(jìn)度值歸0

}

}

}

我們這里展示的是ProgressBar讀取文件字節(jié)流后,展示的讀取進(jìn)度條,上面的簡(jiǎn)單演示就不說(shuō)了。

效果圖:ProgressBar讀取文件字節(jié)流演示

這里展示文件讀取字節(jié)流過(guò)程中,ProgressBar進(jìn)度變化情況,當(dāng)讀取完成,進(jìn)度條剛好滿,這在我們實(shí)際項(xiàng)目開發(fā)中,經(jīng)常需要使用到,如何成為我寫這篇文章的原因。

布局文件progress_horizontal_read_data.xml

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context="cn.teachcourse.www.MainActivity"

android:orientation="vertical"

android:gravity="center" >

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="1"

android:id="@+id/read_data_tv"

android:text="@string/read_info"

android:scrollbars="vertical"

android:layout_marginLeft="10dp"

android:layout_marginRight="10dp"

android:lineSpacingExtra="10dp"/>

android:id="@+id/progressBar_horizontal_read_data"

style="?android:attr/progressBarStyleHorizontal"

android:layout_width="match_parent"

android:layout_height="15dp"

android:layout_marginTop="28dp"/>

Java代碼ProgressBarActivity.java

public class ProgressBarActivity extends ActionBarActivity {

private ProgressBar pb_horizontal;

private int readed;

private int progressValue;

private TextView mReadProgress_TV;

private String mData;

private StringBuffer sb;//每次讀取到的字節(jié)數(shù)據(jù)存儲(chǔ)

int length;//標(biāo)記文件大小

private Thread thread;//讀取文件線程

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.progress_horizontal_read_data);

initView();

Drawable drawable = getResources().getDrawable(

R.drawable.progress_bar_states);

String path = Environment.getExternalStorageDirectory()

+ "/ProgressMonitor.txt";// ProgressMonitor.txt是我存到sdcard中的一個(gè)文件,可以自定義文件名大小位置

final File file = new File(path);

if(file.exists()){

length=(int) file.length();

pb_horizontal.setMax(length);//設(shè)置進(jìn)度條最大值

pb_horizontal.setProgressDrawable(drawable);//自定義進(jìn)度條樣式

}

//啟動(dòng)一個(gè)線程讀取文件內(nèi)容

thread=new Thread() {

@Override

public void run() {

readFromFile(file.getAbsolutePath());

}

};

thread.start();

}

Handler handler = new Handler() {

@Override

public void handleMessage(Message msg) {

switch (msg.what) {

case 110:

progressValue += msg.arg1;

pb_horizontal.setProgress(progressValue);

mReadProgress_TV.setText(sb.toString());

if(progressValue==length){

Toast.makeText(ProgressBarActivity.this, "文件讀取完成", Toast.LENGTH_LONG).show();

}

Log.d("progressValue-------------->", progressValue + "");

break;

}

}

};

public void readFromFile(String path) {

FileInputStream fis;

try {

fis = new FileInputStream(path);

DataInputStream dis = new DataInputStream(fis);

sb=new StringBuffer();

byte b[] = new byte[10];// 每次讀取1字節(jié)

while ((readed = dis.read(b)) != -1) {

Message msg = new Message();

msg.arg1 = readed;

msg.what = 110;

mData=new String(b,0,readed);

sb.append(mData);

handler.sendMessage(msg);

try {

Thread.sleep(1000);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

dis.close();

fis.close();

} catch (Exception e) {

e.printStackTrace();

}

}

private void initView() {

pb_horizontal = (ProgressBar) findViewById(R.id.progressBar_horizontal_read_data);

mReadProgress_TV=(TextView)findViewById(R.id.read_data_tv);

}

}

這里使用Handler消息處理,每次讀取到數(shù)據(jù)后更新進(jìn)度條,將讀取的字節(jié)數(shù)據(jù)放置在arg1中,在Handler的handleMessage方法中設(shè)置進(jìn)度值,同時(shí)刷新TextView顯示的內(nèi)容,這里難就難在如何獲取每次讀取的字節(jié)數(shù),然后刷新進(jìn)度條,在字節(jié)流的學(xué)習(xí)中,我們可以自定義每次讀取字節(jié)大小,File類中可以獲取到文件的總大小,最終我們得到上面圖片中的效果。在這個(gè)例子里面,我們可以將讀取手機(jī)卡的文件,換成下載文件,傳輸文件同樣適應(yīng)。

以上就是本文的全部?jī)?nèi)容,希望大家對(duì)Android軟件編程有所幫助。相關(guān)閱讀:

使用asp.net MVC4中的Bundle遇到的問(wèn)題及解決辦法分享

jquery動(dòng)態(tài)改變onclick屬性導(dǎo)致失效的問(wèn)題解決方法

Linux下PureFtpd的基本安裝使用與超時(shí)問(wèn)題解決

php+ajax 實(shí)現(xiàn)輸入讀取數(shù)據(jù)庫(kù)顯示匹配信息

實(shí)例教程 利用html5和css3打造一款創(chuàng)意404頁(yè)面

C#寫入對(duì)象或集合類型數(shù)據(jù)到xml文件的方法

php通過(guò)curl添加cookie偽造登陸抓取數(shù)據(jù)的方法

Oracle 跨庫(kù) 查詢 復(fù)制表數(shù)據(jù) 分布式查詢介紹

Mac怎么將Time Machine備份的系統(tǒng)恢復(fù)到新的硬盤?

詳解JavaScript的回調(diào)函數(shù)

jQuery插件ImageDrawer.js實(shí)現(xiàn)動(dòng)態(tài)繪制圖片動(dòng)畫(附源碼下載)

js實(shí)現(xiàn)input密碼框提示信息的方法(附html5實(shí)現(xiàn)方法)

MySQL性能優(yōu)化之Open_Table配置參數(shù)的合理配置建議

jQuery檢測(cè)滾動(dòng)條是否到達(dá)底部

總結(jié)

以上是生活随笔為你收集整理的android 自定义 进度条 旋转,Android_Android ProgressBar进度条使用详解,ProgressBar进度条,分为旋转进 - phpStudy...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。