Android實現(xiàn)標(biāo)題上顯示隱藏進度條效果
一個界面,實現(xiàn)在向頁面添加圖片時,在標(biāo)題上顯示一個水平進度條,當(dāng)圖片載入完畢后,隱藏進度條并顯示圖片
具體實現(xiàn)方法:
res/layout/main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:id="@+id/layout1" android:gravity="center"> </LinearLayout>
MainActivity:
package com.example.test; import android.app.Activity; import android.os.AsyncTask; import android.os.Bundle; import android.view.ViewGroup.LayoutParams; import android.view.Window; import android.widget.ImageView; import android.widget.LinearLayout; public class MainActivity extends Activity{ private int imageId[]=new int[]{R.drawable.img01,R.drawable.img02, R.drawable.img03,R.drawable.img04};//定義并初始化一個保存要顯示圖片id的數(shù)組 private LinearLayout layout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_PROGRESS);//顯示水平進度條 setContentView(R.layout.main); layout=(LinearLayout)findViewById(R.id.layout1); new MyTack().execute(); } /* * 創(chuàng)建繼承自AsyncTask的異步類,并重寫onPreExecute()、doInBackground()、onProgressUpdate() * 和onPostExecute方法,實現(xiàn)在向頁面添加圖片時,在標(biāo)題上顯示一個水平進度條,當(dāng)圖片載入完畢后, * 隱藏進度條并顯示圖片 * */ //功能:創(chuàng)建異步任務(wù),添加4張圖片 class MyTack extends AsyncTask<Void,Integer,LinearLayout>{ @Override protected void onPreExecute() { setProgressBarVisibility(true);//執(zhí)行任務(wù)前讓進度條可見 super.onPreExecute(); } //功能:要執(zhí)行的耗時任務(wù)(此方法異步執(zhí)行) @Override protected LinearLayout doInBackground(Void... params) { LinearLayout layout2=new LinearLayout(MainActivity.this); for (int i = 1; i < 5; i++) { ImageView imageView=new ImageView(MainActivity.this);//創(chuàng)建一個ImageView對象 imageView.setLayoutParams(new LayoutParams(245,108)); imageView.setImageResource(imageId[i-1]);//設(shè)置要顯示的圖片 layout2.addView(imageView);//將imageView添加到線形布局管理器中 try { Thread.sleep(10);//為了更好的觀察到效果,我們讓線程休眠10毫秒 } catch (InterruptedException e) { e.printStackTrace(); } publishProgress(i);//觸發(fā)onProgressUpdate(Progress...)方法更新進度 } return layout2; } //功能:更新進度(此方法在主線程中運行) @Override protected void onProgressUpdate(Integer... values) { setProgress(values[0]*2500);//動態(tài)更新最新進度 super.onProgressUpdate(values); } //功能:執(zhí)行任務(wù)后(此方法在主線程中運行) @Override protected void onPostExecute(LinearLayout result) { setProgressBarVisibility(false);//任務(wù)執(zhí)行后隱藏進度條 layout.addView(result);//將水平線性布局管理器添加到布局文件中添加的垂直線性布局管理器中 super.onPostExecute(result); } } }
運行效果如下:
下圖是加載過程,標(biāo)題欄上方有一個進度條顯示的是加載圖片的進度
下圖是加載完成,顯示出圖片
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android 下載文件通知欄顯示進度條功能的實例代碼
- Android編程實現(xiàn)顯示在標(biāo)題上的進度條功能【附源碼下載】
- android多線程斷點下載-帶進度條和百分比進度顯示效果
- Android多線程+單線程+斷點續(xù)傳+進度條顯示下載功能
- Android自定義多節(jié)點進度條顯示的實現(xiàn)代碼(附源碼)
- Android使用AsyncTask下載圖片并顯示進度條功能
- Android 進度條顯示在標(biāo)題欄的實現(xiàn)方法
- Android上傳文件到服務(wù)端并顯示進度條
- Android實現(xiàn)支持進度條顯示的短信備份工具類
- android實現(xiàn)動態(tài)顯示隱藏進度條
相關(guān)文章
Android 應(yīng)用中跳轉(zhuǎn)到應(yīng)用市場評分示例
本篇文章主要介紹了Android 應(yīng)用中跳轉(zhuǎn)到應(yīng)用市場評分示例,非常具有實用價值,需要的朋友可以參考下。2017-02-02Retrofit網(wǎng)絡(luò)請求框架之注解解析和動態(tài)代理
這篇文章主要介紹了Retrofit網(wǎng)絡(luò)請求框架之注解解析和動態(tài)代理,Retrofit是目前Android平臺上比較流行的網(wǎng)絡(luò)請求框架之一,它提供了一種簡潔、靈活的方式來處理HTTP請求和響應(yīng)2023-03-03android studio實現(xiàn)簡單的計算器功能
這篇文章主要為大家詳細介紹了android studio實現(xiàn)簡單的計算器功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-05-05使用PHP開發(fā)Android應(yīng)用程序技術(shù)介紹
這篇文章主要介紹了使用PHP開發(fā)Android應(yīng)用程序技術(shù)介紹,本文講解了安裝PHP for Android、設(shè)置PHP for Android開發(fā)環(huán)境、使用PHP構(gòu)建Android應(yīng)用程序,需要的朋友可以參考下2015-03-03