标签:
1.布局xml文件
activity_main.xml
<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=".MainActivity" >
<Button
android:id="@+id/btn"
android:onClick="download"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="点击循环加载网上图片" />
<ImageView
android:id="@+id/iv"
android:layout_below="@id/btn"
android:layout_height="match_parent"
android:layout_width="match_parent"
/>
</RelativeLayout>
2.优化工具类ImageCach.java
package com.example.day15_ex_loadimage;
import java.util.HashMap;
import android.graphics.Bitmap;
/**
* 图片下载存储优化工具类
* @author Administrator
*
*/
public class ImageCach {
//存储url 和 图片解析流 Bitmap对象
public static HashMap<String,Bitmap>map=new HashMap<String, Bitmap>();
//获取Bitmap对象
public static Bitmap getBitmap(String urlPath){
return ImageCach.map.get(urlPath);
}
//存放url Bitmap
public void put(String urlPath,Bitmap bm){
ImageCach.map.put(urlPath, bm);
}
}
3.联网获取图片类 HttpUtils.java
package com.example.day15_ex_loadimage;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
public class HttpUtils {
public static Bitmap getBitmap(String urlPath){
Bitmap bm=null;
bm=ImageCach.getBitmap(urlPath);//优化 防止重复下载
if(bm==null){
try {
URL url=new URL(urlPath);
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
InputStream in=conn.getInputStream();
bm=BitmapFactory.decodeStream(in);
ImageCach.map.put(urlPath, bm);
Log.e("img","img");
in.close();
conn.disconnect();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return bm;
}
}
4.实现类 MainActivity.java
package com.example.day15_ex_loadimage;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.LoaderManager;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.AsyncTaskLoader;
import android.content.Context;
import android.content.Loader;
import android.graphics.Bitmap;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
@SuppressLint("NewApi")
public class MainActivity extends Activity implements LoaderCallbacks<Bitmap>{
static int i=0;//控制图片url路径
ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
iv=(ImageView) findViewById(R.id.iv);
}
public void download(View view){
LoaderManager loader=getLoaderManager();
loader.restartLoader(0,null,this);
}
@Override
public Loader<Bitmap> onCreateLoader(int id, Bundle args) {
MyLoader loader=new MyLoader(this);//创建loader对象
return loader;
}
@Override
public void onLoadFinished(Loader<Bitmap> arg0, Bitmap arg1) {
iv.setImageBitmap(arg1);//设置图片在ImageView中显示
}
@Override
public void onLoaderReset(Loader<Bitmap> arg0) {
// TODO Auto-generated method stub
}
private static class MyLoader extends AsyncTaskLoader<Bitmap>{
public MyLoader(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
public Bitmap loadInBackground() {
//只能加载4张图片 1-4.jpg
Bitmap bm=HttpUtils.getBitmap("http://www.ytmfdw.com/image/img"+(++i%4+1)+".jpg");
return bm;
}
@Override
protected void onStartLoading() {
// TODO Auto-generated method stub
super.onStartLoading();
forceLoad();
}
}
}
实现LoaderCallbacks接口动态循环加载网上图片并展示在手机屏幕上 ...
标签:
原文地址:http://www.cnblogs.com/android-blogs/p/5703300.html