android URL含进度条异步加载简单实例

本文介绍了一个简单的Android应用案例,展示了如何使用多线程从网络加载图片,并将其显示在ImageView上,同时将图片缓存到本地文件系统,以减少重复下载。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<pre style="font-family: 宋体; font-size: 10.5pt; background-color: rgb(255, 255, 255);"><pre name="code" class="java">public class URLActivity extends BaseActivity {
    //显示URL下的网络图片
    private ImageView iv_urlTest;
    //bitmap存放图片资源
    private Bitmap bitmap;
    //向主线程发送消息
    private final static int MYMSG=111;
    //向主线程发送消息
    private final static int PBSHOW=222;
    //读取文件是显示的ProgressDialog
    private ProgressBar pb_isLoading;

    Handler handler=new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case MYMSG:
                    iv_urlTest.setImageBitmap(bitmap);
                    pb_isLoading.setVisibility(View.GONE);
                    break;
                case PBSHOW:
                    pb_isLoading.setVisibility(View.VISIBLE);
                    break;
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simpleclient);
        initViews();
    }

    //初始化该activity下的views
    private void initViews() {
        //findViewById
        iv_urlTest = (ImageView) findViewById(R.id.iv_urlTest);
        pb_isLoading= (ProgressBar) findViewById(R.id.pb_isLoading);
        new Thread(){
            @Override
            public void run() {
                handler.sendEmptyMessage(PBSHOW);
                try {
                    //定义URL对象http://www.people.com.cn/mediafile/pic/20150821/79/14266681576234779463.jpg
                    //http://img1.gtimg.com/news/pics/hv1/37/195/1468/95506462.jpg
                    URL url=new URL("http://www.people.com.cn/mediafile/pic/20150821/79/14266681576234779463.jpg");
                    //打开该URL对应资源的输入流
                    InputStream is=url.openStream();
                    Log.d("Thread","--------Thread进来了");
                    //从输入流中解析出图片
                    bitmap= BitmapFactory.decodeStream(is);
                    //发送消息更新UI
                    handler.sendEmptyMessage(MYMSG);
                    Log.d("sendEmptyMessage", "--------sendEmptyMessage发送了");
                    is.close();

                   //再次打开该URL对应资源的输入流
                     is=url.openStream();
                    //打开手机文件输出流
                    OutputStream os=openFileOutput("14266681576234779463.jpg",MODE_WORLD_READABLE);
                    byte[] bytes=new byte[1024];
                    int read=0;
                    //将URL对应的资源下载到本地
                    while((read=is.read(bytes))>0)
                    {
                        os.write(bytes,0,read);
                    }
                    is.close();
                    os.close();
                } catch(Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
}





进度条样式:

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:pivotX="50%" android:pivotY="50%" android:fromDegrees="0"
    android:toDegrees="360">
    <shape android:shape="ring" android:innerRadiusRatio="3"
        android:thicknessRatio="8" android:useLevel="false">
        <gradient android:type="sweep" android:useLevel="false"
            android:startColor="#FFFFFF" android:centerColor="#FFDC35"
            android:centerY="0.50" android:endColor="#CE0000" />
    </shape>
</rotate>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

<pre style="font-family: 宋体; font-size: 10.5pt; background-color: rgb(255, 255, 255);"><pre name="code" class="html" style="font-size: 13.3333px;"><ProgressBar
    android:id="@+id/pb_isLoading"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:indeterminate="false"
    android:indeterminateDrawable="@drawable/dialog_style_xml_color"
    android:visibility="gone"
    />

<ImageView android:id="@+id/iv_urlTest" android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout>


你还会在手机系统中,/data/data/你的项目包名/files/目录下找到资源文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值