MainActivity
public class MainActivity extends AppCompatActivity {
private ImageView iv;
String url="http://pic4.nipic.com/20091217/3885730_124701000519_2.jpg";
private ProgressBar bar;
private TextView tv_pro;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv=(ImageView)findViewById(R.id.iv);
bar=(ProgressBar)findViewById(R.id.bar);
tv_pro=(TextView)findViewById(R.id.tv_pro);
}
public void start(View view){
new AsyncTask<String, Integer, Bitmap>() {
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
bar.setProgress(values[0]);
tv_pro.setText("当前进度:"+values[0]+"%");
}
@Override
protected Bitmap doInBackground(String... params) {
try {
URL url1=new URL(params[0]);
HttpURLConnection huc=(HttpURLConnection)url1.openConnection();
huc.setConnectTimeout(3000);
huc.connect();
if(huc.getResponseCode()==200){
InputStream inputStream = huc.getInputStream();
int contentLength = huc.getContentLength();//获取图片字节的总长度
int length=0;//当前下载了多少
byte[] b=new byte[1024];
int len=0;
ByteArrayOutputStream out=new ByteArrayOutputStream();
while ((len=inputStream.read(b))!=-1){
out.write(b,0,len);
length+=len;
int progress=(length*100)/contentLength;//进度百分比
publishProgress(progress);//把进度返回,调用onProgressUpdate
}
byte[] imgs=out.toByteArray();
Bitmap bitmap = BitmapFactory.decodeByteArray(imgs, 0, imgs.length);
return bitmap;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
//INVISIBLE:不可见,但是占位置
//VISIBLE:可见,默认
//GONE:不可见,也不占位置,彻底隐藏
bar.setVisibility(View.GONE);
tv_pro.setVisibility(View.GONE);
iv.setImageBitmap(bitmap);
}
}.execute(url);
}
}
布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="org.frxm.day10_asynctaskimg.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下载"
android:onClick="start"
/>
<!--android:progress="0"设置进度条的初始值,max:最大进度数
默认样式是圈,style="?android:attr/progressBarStyleHorizontal":设置进度为水平进度条的样式
-->
<ProgressBar
android:id="@+id/bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
android:max="100"
android:progress="0"/>
<TextView
android:id="@+id/tv_pro"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="当前进度为:0%"/>
<ImageView
android:id="@+id/iv"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>