android載入進度條
Ⅰ 怎樣實現在android實現帶進度條的上傳效果
實現在android實現帶進度條的上傳效果效果如圖:用到以下兩個類就可實現帶進度條的文件上傳:
1、CustomMultiPartEntity extends MultipartEntity,
2、HttpMultipartPost extends AsyncTask
代碼如下:
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
public class CustomMultipartEntity extends MultipartEntity {
private final ProgressListener listener;
public CustomMultipartEntity(final ProgressListener listener) {
super();
this.listener = listener;
}
public CustomMultipartEntity(final HttpMultipartMode mode, final ProgressListener listener) {
super(mode);
this.listener = listener;
}
public CustomMultipartEntity(HttpMultipartMode mode, final String boundary,
final Charset charset, final ProgressListener listener) {
super(mode, boundary, charset);
this.listener = listener;
}
@Override
public void writeTo(final OutputStream outstream) throws IOException {
super.writeTo(new CountingOutputStream(outstream, this.listener));
}
public static interface ProgressListener {
void transferred(long num);
}
public static class CountingOutputStream extends FilterOutputStream {
private final ProgressListener listener;
private long transferred;
public CountingOutputStream(final OutputStream out, final ProgressListener listener) {
super(out);
this.listener = listener;
this.transferred = 0;
}
public void write(byte[] b, int off, int len) throws IOException {
out.write(b, off, len);
this.transferred += len;
this.listener.transferred(this.transferred);
}
public void write(int b) throws IOException {
out.write(b);
this.transferred++;
this.listener.transferred(this.transferred);
}
}
}
該類計算寫入的位元組數,我們需要在實現ProgressListener中的trasnfered()方法,更行進度條
public class HttpMultipartPost extends AsyncTask<HttpResponse, Integer, TypeUploadImage> {
ProgressDialogpd;
longtotalSize;
@Override
protectedvoidonPreExecute(){
pd= newProgressDialog(this);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setMessage("Uploading Picture...");
pd.setCancelable(false);
pd.show();
}
@Override
(HttpResponse... arg0) {
HttpClienthttpClient = newDefaultHttpClient();
HttpContexthttpContext = newBasicHttpContext();
HttpPosthttpPost = newHttpPost("http://herpderp.com/UploadImage.php");
try{
= newCustomMultipartEntity(
newProgressListener() {
@Override
public void transferred(longnum){
publishProgress((int) ((num / (float) totalSize) * 100));
}
});
// We use FileBody to transfer an image
multipartContent.addPart("uploaded_file", newFileBody(
newFile(m_userSelectedImagePath)));
totalSize= multipartContent.getContentLength();
// Send it
httpPost.setEntity(multipartContent);
HttpResponseresponse = httpClient.execute(httpPost, httpContext);
String serverResponse = EntityUtils.toString(response.getEntity());
ResponseFactoryrp = newResponseFactory(serverResponse);
return(TypeImage) rp.getData();
}
catch(Exception e) {
System.out.println(e);
}
returnnull;
}
@Override
protectedvoidonProgressUpdate(Integer... progress){
pd.setProgress((int) (progress[0]));
}
@Override
protectedvoidonPostExecute(TypeUploadImageui) {
pd.dismiss();
}
}
在 transferred()函數中調用publishProgress((int) ((num / (float) totalSize) * 100));
在onProgressUpdate()實現上傳進度的更新操作
Ⅱ android 繪制進度條
進度條一般是用來顯示耗時操作的,如你圖示,最終完成的時候剛好繞一圈,是一個計時器來確定進度條跑完一圈的時間,然後按時間的流逝來繪制進度條(也就是邊框)。
我的思路是這樣的,首先確定進度條的起始位置,也就是黑色背景圖的上部中間(前提是獲取到背景圖片四個角角位置坐標(X,Y)),計算出圖片的長寬,這樣背景圖片四個角的坐標都有了,進度條的起始坐標也有了,然後根據周長和定時器的時間確定我們每毫秒需要繪制多長,遇到拐角的時候判斷一下進度條的實時坐標與背景圖的拐角坐標是否一致,然後就拐個彎,繼續繪制。
這是個思路,我過會兒調試一下
Ⅲ android 進度條,暫停,繼續怎麼弄
Handler和ProgressBar實現進度條的開始,暫停,停止,後退和循環
一,涉及的handler類方法
1,
post(Runnable r)
Causes the Runnable r to be added to the message queue.將要執行的線程對象加到隊列當中
2,
removeCallbacks(Runnable r)
Remove any pending posts of Runnable r that are in the message queue.移除隊列當中未執行的線程對象
3,
postDelayed(Runnable r, long delayMillis)
Causes the Runnable r to be added to the message queue, to be run after the specified amount of time elapses.
將要執行的線程對象放入到隊列當中,待時間結束後,運行制定的線程對象
二,編寫程序
程序效果:實現進度條的開始,暫停,停止,後退和循環
http://blog.csdn.net/superjunjin/article/details/7539844
Ⅳ Android自定義雙向進度條
想整個雙向的進度條,就是可以選取播放范圍的。
像這樣:
然而官方控制項里只有單向的。不要慌,我們自己畫一個。
繪制一個進度條主要是三方面。1.樣式,2.尺寸,3.操作監聽。
注釋基本上就把原理說明了一下。
然後就可以在程序中使用了。
相關GitHub項目地址: https://github.com/codeqian/android-class-lib
Ⅳ android 進度條樣式 怎麼改
Android系統提供了兩大類進度條樣式,長形進度條(progressBarStyleHorizontal) 和圓形進度條(progressBarStyleLarge)。
android 進度條樣式更改:
第一種
(默認樣式(中等圓形))
進度條用處很多,比如,應用程序裝載資源和網路連接時,可以提示用戶稍等,這一類進度條只是代表應用程序中某一部分的執行情況,而整個應用程序執行情況呢,則可以通過應用程序標題欄來顯示一個進度條,這就需要先對窗口的顯示風格進行設置"requestWindowFeature(Window.FEATURE_PROGRESS)"。
Ⅵ android progressbar 自動載入完畢
ndroid progressbar 自動載入完畢的處理方法為:
1、在主線程中創建子Thread,然後sendMessage給Handle。
2、Android asyncTask .,源碼為:
public class LoadingActivity extends Activity {
private ProgressDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//設置一個progressdialog的彈窗
dialog = ProgressDialog.show(this, null, "程序正在載入,請稍候...", true, false);
//啟動一個處理loading業務的線程
new LoadingThread(this).start();
}
private class LoadingThread extends Thread {
private LoadingActivity activity;
public LoadingThread(LoadingActivity act) {
Log.d(">>>>>LoadingThread", "構造方法");
activity = act;
}
public void run() {
Log.d(">>>>>LoadingThread", "傳遞message");
//*********************
// 處理業務
//*********************
//發送消息
activity.mHandler.sendEmptyMessage(0);
}
}
//處理跳轉到主Activity
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
Log.d(">>>>>Mhandler", "開始handleMessage");
Intent mIntent = new Intent();
mIntent.setClass(LoadingActivity.this, PalmHospital_main_Activity.class);
startActivity(mIntent);
LoadingActivity.this.finish();
Log.d(">>>>>Mhandler", "LoadActivity關閉");
if (dialog.isShowing())
dialog.dismiss();
}
};
}
Ⅶ android布局文件里的ProgressBar長形進度條怎麼自定義樣式
在windows操作系統下Android studio按照如下步驟自動義ProgressBar長形進度條的樣式。
1、首先創建一個android項目,打開其中的XML布局文件,如下圖:
Ⅷ Android水平進度條顏色和樣式
java.lang.Object
↳android.view.View
↳android.widget.ProgressBar
直接子類:AbsSeekBar extends ProgressBar;間接子類:RatingBar, SeekBar。
ProgressBar有兩種展示形式:
(1)android:progressBarStyle:默認進度條樣式,不確定模式;(分大小)
(2)android:progressBarStyleHorizontal:水平進度條樣式。
1、progress布局:
為改變進度條樣式和顏色,需要復寫Android系統自帶的樣式並引用:android:progressDrawable="@drawable/progressbar";
2、下面為drawable下progressbar.xml: