Splash Screen加载网络图片实现欢迎页的两种方法

Splash Screen加载网络图片可以用两种方法,一般用在开启app时显示公司logo,广告,或者欢迎页,介绍两种方法来处理:异步加载和子线程处理。

第一种是异步加载的方法
在onCreate中设置
/**异步加载*/
new FetchDataTask().execute(url);

然后编写异步加载的方法
/**异步加载*/
	public class FetchDataTask extends AsyncTask{

        //执行前调用
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        //执行后台任务
        @Override
        protected Bitmap doInBackground(String... strings) {
            Bitmap bitmap=null;
            try {
                bitmap= Utils.getBitmap(url);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return bitmap;
        }

        //任务完成时调用
        @Override
        protected void onPostExecute(Bitmap bitmap) {
            super.onPostExecute(bitmap);
            imageView.setImageBitmap(bitmap);
        }
    }


完整代码
package com.dxsport;

import com.dxsport.common.Utils;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

public class SplashScreenActivity extends Activity {

	private String url = "http://tupian.enterdesk.com/2015/saraxuss/04/17/gou/1/3.jpg";
	private ImageView imageView;
	Bitmap bitmap = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏 
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//去掉信息栏
		setContentView(R.layout.splash_screen);
		
		imageView = (ImageView) findViewById(R.id.adImage);
		
		/**异步加载*/
		new FetchDataTask().execute(url);
		
	
		new Handler().postDelayed(new Runnable() {  
 			@Override  
 			public void run() {  
 				Intent  i = new Intent(SplashScreenActivity.this, MainActivity.class);
 				startActivity(i);
 				SplashScreenActivity.this.finish(); 
 			}
 		},3000); 
	}
	
	/**异步加载*/
	public class FetchDataTask extends AsyncTask{

        //执行前调用
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        //执行后台任务
        @Override
        protected Bitmap doInBackground(String... strings) {
            Bitmap bitmap=null;
            try {
                bitmap= Utils.getBitmap(url);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return bitmap;
        }

        //任务完成时调用
        @Override
        protected void onPostExecute(Bitmap bitmap) {
            super.onPostExecute(bitmap);
            imageView.setImageBitmap(bitmap);
        }
    }
	
	
}


第二种是添加子线程的方法
在onCreate中设置

// 开启一个子线程,进行网络操作,等待有返回结果,使用handler通知UI  
		new Thread(networkTask).start();

然后编写子线程方法

Handler handler = new Handler() {  
		@Override  
		public void handleMessage(Message msg) {  
			super.handleMessage(msg);  
			Bundle data = msg.getData();  
			Bitmap bitmap = data.getParcelable("bitmap");
			// UI界面的更新等相关操作  
			imageView.setImageBitmap(bitmap);
		}  
	};  
	/** 
	 * 网络操作相关的子线程 
	 */  
	Runnable networkTask = new Runnable() {  
  
		@Override  
		public void run() {  
			// 在这里进行 http request.网络请求相关操作  
			Message msg = new Message();  
			Bundle data = new Bundle();  
			Bitmap bitmap = Utils.getBitmap(url);
			data.putParcelable("bitmap", bitmap);
			msg.setData(data);  
			handler.sendMessage(msg);  
		}  
	};  



完整代码
package com.dxsport;

import com.dxsport.common.Utils;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

public class SplashScreenActivity extends Activity {

	private String url = "http://tupian.enterdesk.com/2015/saraxuss/04/17/gou/1/3.jpg";
	private ImageView imageView;
	Bitmap bitmap = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏 
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//去掉信息栏
		setContentView(R.layout.splash_screen);
		
		imageView = (ImageView) findViewById(R.id.adImage);
		
		
		
		// 开启一个子线程,进行网络操作,等待有返回结果,使用handler通知UI  
		new Thread(networkTask).start();  

		
		new Handler().postDelayed(new Runnable() {  
 			@Override  
 			public void run() {  
 				Intent  i = new Intent(SplashScreenActivity.this, MainActivity.class);
 				startActivity(i);
 				SplashScreenActivity.this.finish(); 
 			}
 		},3000); 
	}
	

	Handler handler = new Handler() {  
		@Override  
		public void handleMessage(Message msg) {  
			super.handleMessage(msg);  
			Bundle data = msg.getData();  
			Bitmap bitmap = data.getParcelable("bitmap");
			// UI界面的更新等相关操作  
			imageView.setImageBitmap(bitmap);
		}  
	};  
	/** 
	 * 网络操作相关的子线程 
	 */  
	Runnable networkTask = new Runnable() {  
  
		@Override  
		public void run() {  
			// 在这里进行 http request.网络请求相关操作  
			Message msg = new Message();  
			Bundle data = new Bundle();  
			Bitmap bitmap = Utils.getBitmap(url);
			data.putParcelable("bitmap", bitmap);
			msg.setData(data);  
			handler.sendMessage(msg);  
		}  
	};  

}


Utils 类
package com.dxsport.common;


import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class Utils {
	
	 /**通过传入的图片地址,获取图片*/
	public static Bitmap getBitmap(String url){
		Bitmap bitmap = null;
		try {
            HttpURLConnection connection= (HttpURLConnection) (new URL(url)).openConnection();
            InputStream is=connection.getInputStream();
            bitmap= BitmapFactory.decodeStream(is);
        } catch (Exception e) {
            e.printStackTrace();
        }
		return bitmap;
	}

}

splash_screen.xml

http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    
        android:id="@+id/adImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

LinearLayout>

你可能感兴趣的:(android,Splash,Screen,android,欢迎页,android,加载网络图片,异步加载网络图片)