zoukankan      html  css  js  c++  java
  • 安卓开发--AsyncTask

    package com.cnn.asynctask;
    
    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 {
    	private 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, ImageTest.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.asynctask;
    
    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.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.opengl.Visibility;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.ImageView;
    import android.widget.ProgressBar;
    
    public class ImageTest extends Activity {
    	private ImageView imageView;
    	private ProgressBar bar;
    	private String urlString="http://pic2.ooopic.com/01/03/51/25b1OOOPIC19.jpg";
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		// TODO 自动生成的方法存根
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.image);
    		
    		imageView=(ImageView) findViewById(R.id.image);
    		bar=(ProgressBar) findViewById(R.id.bar);
    		
    		new ImageAsync().execute(urlString);
    	}
    	
    	class ImageAsync extends AsyncTask<String, Void, Bitmap> {
    
    		@Override
    		protected Bitmap doInBackground(String... params) {
    			// TODO 自动生成的方法存根
    			//获取传递进来的参数
    			String urlString=params[0];
    			Bitmap bitmap=null;
    			URLConnection connection;
    			InputStream inputStream;
    			try {
    				connection=new URL(urlString).openConnection();
    				inputStream=connection.getInputStream();
    				BufferedInputStream bisBufferedInputStream= new BufferedInputStream(inputStream);
    				//通过decodeStream方法解析输入流 转化为bitmap 
    				bitmap=BitmapFactory.decodeStream(bisBufferedInputStream);
    				inputStream.close();
    				bisBufferedInputStream.close();
    			} catch (MalformedURLException e) {
    				// TODO 自动生成的 catch 块
    				e.printStackTrace();
    			} catch (IOException e) {
    				// TODO 自动生成的 catch 块
    				e.printStackTrace();
    			}
    			//bitmap作为返回值返回给后面调用的方法
    			return bitmap;
    		}
    		
    		@Override
    		protected void onPreExecute() {
    			// TODO 自动生成的方法存根
    			super.onPreExecute();
    			bar.setVisibility(View.VISIBLE);
    		}
    		
    		@Override
    		protected void onPostExecute(Bitmap result) {
    			// TODO 自动生成的方法存根
    			super.onPostExecute(result);
    			bar.setVisibility(View.GONE);
    			imageView.setImageBitmap(result);
    		}
    	}
    }
    

      

    <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.asynctask.MainActivity" >
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:text="Button" />
    
    </RelativeLayout>
    

      

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:padding="16dp"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    	<ImageView 
    	    android:id="@+id/image"
    	    android:layout_width="match_parent"
    	    android:layout_height="match_parent"
    	    />
    	<ProgressBar 
    	    android:id="@+id/bar"
    	    android:visibility="gone"
    	    android:layout_width="wrap_content"
    	    android:layout_height="wrap_content"
    	    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.asynctask"
        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=".ImageTest"
                android:label="@string/app_name" >
            </activity>
        </application>
    
    </manifest>
    

      

  • 相关阅读:
    C#中自动增量字段值的获取方法
    Mio改造第一步
    酒后
    阳朔自驾行-行程篇
    如何在C#中获取新插入的identity列值
    格格生了
    扯皮
    nPdl的翻译
    asp.net和asp的互相调用
    NetBPM前进了一步,解决了上一个问题
  • 原文地址:https://www.cnblogs.com/zxcnn/p/5071574.html
Copyright © 2011-2022 走看看