您的位置:首页 > 移动开发 > Android开发

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

2015-11-12 10:11 507 查看
<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;"><ProgressBarandroid: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/目录下找到资源文件
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: