先看下运行结束
ok,看下实现方法
先在配置清单中加一个访问网络的权限!
<uses-permission android:name="android.permission.INTERNET" />
第一步:在image布局中添加控件
<ImageViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/imaged"/><ProgressBarandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:visibility="gone"android:layout_centerInParent="true"android:id="@+id/progressBar"/>
第二步:直接到ImageTeakJava类中来
public class ImageTeak extends AppCompatActivity {private ProgressBar mProgressbar;private ImageView mImage;private MyAsyncTask task;//网络图片地址private static String url="http://b365.photo.store.qq.com/psb?/V1112Bib4N2Zv2/yIw4AGwbwoCsuA5Zid.eAEE9YpH0znIsrS4os49LY7M!/c/dG0BAAAAAAAA&bo=OARoBQAAAAAREHY!&rf=mood_app";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.image);mProgressbar= (ProgressBar) findViewById(R.id.progressBar);mImage= (ImageView) findViewById(R.id.imaged);//创建一个类task =new MyAsyncTask();//启动task.execute(url);}/** AsyncTask<String,Void,Bitmap>第一个参数;为输入的url参数是String类型的第二个参数:我们没有进度条所以为Void类型的第三个参数;返回的是Bitmap*/class MyAsyncTask extends AsyncTask<String,Void,Bitmap>{@Override//第一个会调用的方法protected void onPreExecute() {// 开始之前要做的准备操作在这里面执行super.onPreExecute();mProgressbar.setVisibility(View.VISIBLE);//显示进度}@Override//第三个会调用的方法。用来展示处理的结果!// (当doInBackground方法完成异步处理之后会调用的方法)protected void onPostExecute(Bitmap bitmap) {super.onPostExecute(bitmap);mProgressbar.setVisibility(View.GONE);/*** 将doInBackground()方法中* 返回的bitmap解析的图片设置给ImageView* */mImage.setImageBitmap(bitmap);}@Override//第二个会调用的方法。真正的耗时操作!下载网络图片protected Bitmap doInBackground(String... strings) {//获取传递进来的参数,取出对应的URLString url=strings[0];//定义网络连接对象URLConnection connection;//获取需要的BitmapBitmap bitmap=null;//获取数据的输入流InputStream is;try {//获取网络连接对象connection=new URL(url).openConnection();//获取输入流is=connection.getInputStream();//包装下BufferedInputStream bis=new BufferedInputStream(is);//设置睡眠三秒Thread.sleep(3000);//通过decodeStream()方法解析输入流将输入流解析成Bitmap图片bitmap= BitmapFactory.decodeStream(bis);//关闭流is.close();bis.close();//捕获异常} catch (IOException e) {e.printStackTrace();} catch (InterruptedException e) {e.printStackTrace();}//返回的是解析后的网络图像return bitmap;}}
}
<Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="loadImage"android:text="加载图片"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/loadProgress"android:onClick="loadProgress"android:text="带进度条的获取图片"/>
第四步:在Mactivity.java中进行编辑
public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);/* MsyncTask task=new MsyncTask();task.execute();*/}public void loadImage(View view){Intent intent=new Intent(MainActivity.this,ImageTeak.class);startActivity(intent);}public void loadProgress(View view){Intent intent=new Intent(MainActivity.this,ProgressTesk.class);startActivity(intent);}
}
第一步:在progress.xml布局中添加控件
<ImageViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/imageView"/><ProgressBarandroid:visibility="gone"android:layout_width="match_parent"android:layout_height="wrap_content"style="?android:attr/progressBarStyleHorizontal"android:id="@+id/bar"android:layout_centerInParent="true"/>
第二步:对ProgressTesk.java类进行编辑
public class ProgressTesk extends AppCompatActivity {private ProgressBar bar;private MyAsyncTask task;private ImageView imageView;private static String url="http://b389.photo.store.qq.com/psb?/V1112Bib4N2Zv2/0Ahi.ZaLH*i0tsSy3dlWnUNIXOXdYXRbVdwdoUpfxwk!/m/dIUBAAAAAAAAnull&bo=OASgBQAAAAAFB70!&rf=photolist&t=5";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.progress);//控件实例化bar= (ProgressBar) findViewById(R.id.bar);imageView= (ImageView) findViewById(R.id.imageView);//创建对象task=new MyAsyncTask();//启动异步处理task.execute(url);}class MyAsyncTask extends AsyncTask<String,Integer,Bitmap>{@Overrideprotected void onPreExecute() {super.onPreExecute();bar.setVisibility(View.VISIBLE);}@Overrideprotected Bitmap doInBackground(String... strings) {for (int i=0;i<100;i++){publishProgress(i);//传递进度值给onProgressUpdate方法try {Thread.sleep(300);} catch (InterruptedException e) {e.printStackTrace();}}String url=strings[0];URLConnection connection;Bitmap bitmap=null;InputStream is;try {connection=new URL(url).openConnection();is=connection.getInputStream();BufferedInputStream bis=new BufferedInputStream(is);Thread.sleep(3000);bitmap= BitmapFactory.decodeStream(bis);is.close();bis.close();} catch (IOException e) {e.printStackTrace();} catch (InterruptedException e) {e.printStackTrace();}return bitmap;}/*** 这个方法是承接doInBackground()方法中的i的* 通过Integer方法所传递出来的i作用到了values数组上* */@Override //获取进度更新值protected void onProgressUpdate(Integer... values) {super.onProgressUpdate(values);/*** 从values数组中传递过来的i作为mProgressBar的进度值* 由于这里只传递进来一个数,所以这里只需要获取0就OK了~* */bar.setProgress(values[0]);}@Overrideprotected void onPostExecute(Bitmap bitmap) {super.onPostExecute(bitmap);bar.setVisibility(View.GONE);imageView.setImageBitmap(bitmap);}}
}
最后运行下!看效果如何...