实现功能:通过地址网上去多张图片,然后显示viewpager上,可以实现侧滑。
首先新建MainImageDetailActivityy继承FragmentActivity
package com.example.httpviewpagerfragmnet;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
public class MainImageDetailActivityy extends FragmentActivity {
ViewPager mViewPager;
private ImagePagerAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_image_detail_activityy);
mViewPager=(ViewPager) findViewById(R.id.viewpagerone);
mAdapter = new ImagePagerAdapter(getSupportFragmentManager(),Constances.imageThumbUrls);
mViewPager.setAdapter(mAdapter);
mViewPager.setOffscreenPageLimit(3);//ViewPager预加载页设置
}
public class ImagePagerAdapter extends FragmentStatePagerAdapter{
private String[] mImageUrls;
private final int mSize;
public ImagePagerAdapter(FragmentManager fm,String[] mn) {
super(fm);
mImageUrls = mn;
mSize = mImageUrls.length;
}
@Override
public Fragment getItem(int arg0) {
return ImageDetailFragment.newInstance(mImageUrls[arg0]);
}
@Override
public int getCount() {
return mSize; }
}
}
对应的XML:<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/viewpagerone"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>
</LinearLayout>
第二步:
package com.example.httpviewpagerfragmnet;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Binder;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import com.example.httpviewpagerfragmnet.AsyncImageLoader.ImageCallbackForBitmap;
public class ImageDetailFragment extends Fragment {
private static final String IMAGE_DATA_EXTRA = "extra_image_data";
private String mImageUr;
private ImageView mImageView;
private ProgressBar mProgressBar;
public static ImageDetailFragment newInstance(String imageUrl) {
ImageDetailFragment f = new ImageDetailFragment();
final Bundle args = new Bundle();
args.putString(IMAGE_DATA_EXTRA, imageUrl);
f.setArguments(args);
return f;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mImageUr = getArguments() != null ? getArguments().getString(
IMAGE_DATA_EXTRA) : null;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.image_detail_fragment,
container, false);
mImageView = (ImageView) v.findViewById(R.id.imageView);
mProgressBar = (ProgressBar) v.findViewById(R.id.progressBarone);
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
AsyncImageLoader asyncImageLoader = new AsyncImageLoader();
mProgressBar.setVisibility(View.VISIBLE);
mProgressBar.setVisibility(View.GONE);
final Handler handle=new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
mImageView.setImageBitmap((Bitmap)msg.obj);
mProgressBar.setVisibility(View.GONE);
}
};
// }
Bundle b=new Bundle();
b=getArguments();
final String path=b.getString(IMAGE_DATA_EXTRA);
new Thread(){
public void run() {
URL url=null;
InputStream is=null;
try {
url=new URL(path);
is=url.openStream();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Bitmap bitmap=BitmapFactory.decodeStream(is);
handle.sendMessage(handle.obtainMessage(0,bitmap));
};
}.start();
}
@Override
public void onDestroy() {
super.onDestroy();
if (mImageView != null) {
mImageView.setImageDrawable(null);
}
}
}
XML:
显示图片和转圈的两个空间<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ProgressBar
android:id="@+id/progressBarone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:layout_marginLeft="50dp"/>
</LinearLayout>
-------------------------------------------------------------------------------------------------------
这是一个网络取图片的封装工具类,也可以自己写:
package com.example.httpviewpagerfragmnet;
import java.io.InputStream;
import java.net.URL;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.util.LruCache;
public class AsyncImageLoader {
/**
* 图片缓存技术的核心类,用于缓存所有下载好的图片,在程序内存达到设定值时会将最少最近使用的图片移除掉。
*/
private LruCache<String, Bitmap> mMemoryCache;
public AsyncImageLoader() {
// 获取应用程序最大可用内存
int maxMemory = (int) Runtime.getRuntime().maxMemory();
int cacheSize = maxMemory / 8;
// 设置图片缓存大小为程序最大可用内存的1/8
mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
return bitmap.getByteCount();
}
};
}
/**
* 将一张图片存储到LruCache中。
*
* @param key
* LruCache的键,这里传入图片的URL地址。
* @param bitmap
* LruCache的键,这里传入从网络上下载的Bitmap对象。
*/
public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
if (getBitmapFromMemoryCache(key) == null) {
mMemoryCache.put(key, bitmap);
}
}
/**
* 从LruCache中获取一张图片,如果不存在就返回null。
*
* @param key
* LruCache的键,这里传入图片的URL地址。
* @return 对应传入键的Bitmap对象,或者null。
*/
public Bitmap getBitmapFromMemoryCache(String key) {
return mMemoryCache.get(key);
}
public Bitmap loadBitmap(final String imageUrl,final ImageCallbackForBitmap callback) {
if (imageUrl == null) {
return null;
}
final Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
callback.imageLoaded((Bitmap) msg.obj, imageUrl);
}
};
Bitmap bitmap = getBitmapFromMemoryCache(imageUrl);
if( bitmap != null){
return bitmap;
}
new Thread() {
public void run() {
Bitmap bitmap = loadImageBitmapFromUrl(imageUrl);
addBitmapToMemoryCache(imageUrl,bitmap);
if (bitmap != null) {
handler.sendMessage(handler.obtainMessage(0, bitmap));
}
}
}.start();
return null;
}
protected Bitmap loadImageBitmapFromUrl(String imageUrl) {
try {
URL rul = new URL(imageUrl);
InputStream is = rul.openStream();
Bitmap bitmap = BitmapFactory.decodeStream(is);
return bitmap;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public interface ImageCallbackForBitmap {
public void imageLoaded(Bitmap bitmap, String imageUrl);
}
}
------------------------------------------------------------------------------------------------------
这里把网络取图片的地址一并附上,还是封装:
package com.example.httpviewpagerfragmnet;
public class Constances {
public static final String ACTION_PLAYER = "action.scxh.android.receiver.MyBroadCastReceiver.play";
public static final String ACTION_STOP = "action.scxh.android.receiver.MyBroadCastReceiver.stop";
public static final String BASE_URL = "http://192.168.1.127:8080";
public static String tag = "UIDemo";
public final static String[] imageThumbUrls = new String[] {
"https://img-my.csdn.net/uploads/201407/26/1406383299_1976.jpg",
"https://img-my.csdn.net/uploads/201407/26/1406383291_6518.jpg",
"https://img-my.csdn.net/uploads/201407/26/1406383291_8239.jpg",
"https://img-my.csdn.net/uploads/201407/26/1406383290_9329.jpg",
"https://img-my.csdn.net/uploads/201407/26/1406383290_1042.jpg",
"https://img-my.csdn.net/uploads/201407/26/1406383275_3977.jpg",
"https://img-my.csdn.net/uploads/201407/26/1406383265_8550.jpg",
"https://img-my.csdn.net/uploads/201407/26/1406383264_3954.jpg",
"https://img-my.csdn.net/uploads/201407/26/1406383264_4787.jpg", };
}
这样一个简单地功能就实现了