安卓开发--AsyncTask2
package com.cnn.imageasyncdemo01; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { Button btnButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnButton=(Button) findViewById(R.id.button1); btnButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO 自动生成的方法存根 Intent intent = new Intent(MainActivity.this,ImageActivity.class); startActivity(intent); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
package com.cnn.imageasyncdemo01; import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.widget.ImageView; import android.widget.ProgressBar; public class ImageAsync1 extends AsyncTask<String, Void, Bitmap> { private ImageView imageView; private ProgressBar progressBar; public ImageAsync1(ImageView imageView,ProgressBar progressBar) { this.imageView=imageView; this.progressBar=progressBar; } @Override protected Bitmap doInBackground(String... params) { // TODO 自动生成的方法存根 Bitmap bitmap = null; try { URLConnection connection=new URL(params[0]).openConnection(); InputStream inputStream = connection.getInputStream(); BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream); bitmap = BitmapFactory.decodeStream(bufferedInputStream); inputStream.close(); bufferedInputStream.close(); } catch (MalformedURLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } return bitmap; } //doInBackground执行完成后调用 @Override protected void onPostExecute(Bitmap result) { // TODO 自动生成的方法存根 super.onPostExecute(result); imageView.setImageBitmap(result); progressBar.setVisibility(ProgressBar.VISIBLE); } //执行后台耗时操作前调用 @Override protected void onPreExecute() { // TODO 自动生成的方法存根 super.onPreExecute(); progressBar.setVisibility(ProgressBar.GONE); } }
package com.cnn.imageasyncdemo01; import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import android.widget.ProgressBar; public class ImageActivity extends Activity { private ImageView imageView; private ProgressBar progressBar; private String imgUrl; ImageAsync1 taskAsync1; @Override protected void onCreate(Bundle savedInstanceState) { // TODO 自动生成的方法存根 super.onCreate(savedInstanceState); setContentView(R.layout.imageitem); imgUrl = "http://pic2.ooopic.com/01/03/51/25b1OOOPIC19.jpg"; imageView = (ImageView) findViewById(R.id.imageView1); progressBar = (ProgressBar) findViewById(R.id.bar1); taskAsync1 = new ImageAsync1(imageView, progressBar); taskAsync1.execute(imgUrl); } }
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" 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" tools:context="com.cnn.imageasyncdemo01.MainActivity" > <Button android:id="@+id/button1" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="53dp" android:layout_marginTop="28dp" android:text="Button" /> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:id="@+id/imageView1" android:layout_width="match_parent" android:layout_height="match_parent"/> <ProgressBar android:id="@+id/bar1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="gone" android:layout_centerInParent="true" /> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.cnn.imageasyncdemo01" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="21" /> <uses-permission android:name="android.permission.INTERNET"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".ImageActivity" > </activity> </application> </manifest>