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/目录下找到资源文件
相关文章推荐
- 《android 学习》三、Intent 的概念及应用
- Ant 打包全攻略
- Android的系统服务一览
- Android简洁漂亮的引导页
- 用android studio将assets打包到jar文件的方法
- 解决Android Studio中Terminal无法输入问题
- Android定位&地图&导航——基于百度地图实现的定位功能
- Android 原生listview item伸展收缩效果 (续)
- Android4.2 获取手机各种状态集合
- Android 中三种使用线程的方法
- AndroidManifest.xml文件详解(uses-feature)
- Android触摸屏事件派发机制详解与源码分析三(Activity篇)
- Android触摸屏事件派发机制详解与源码分析二(ViewGroup篇)
- Android触摸屏事件派发机制详解与源码分析一(View篇)
- Service
- Android Process 进程通信(一)
- Android4.2 第一次开机引导界面
- android 解决按钮、imageView 背景图片拉伸变大的问题
- Android C/JAVA 输出CallStack
- android 各个版本对应的api等级