点击从网络下载图片(进度条,百分比)

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>



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值