package sunzong.guo;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.TypedArray;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGestureListener;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.provider.OpenableColumns;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
public class ImageDemodActivity extends Activity {
// 定义图片数组
// private static int[] imageId = new int[] { R.drawable.a, R.drawable.b,
// R.drawable.c, R.drawable.d, R.drawable.e, R.drawable.f };
private static String[] imageUri = new String[] {
"http://photo.tuhigh.com/pics/915/0409/1889281581055925_o.jpg",
"http://photo.tuhigh.com/pics/915/0409/1889281581055925_o.jpg",
"http://www.rouding.com/uploadfile/201008/29/42204031909.jpg",
"http://img.91536.com/editor/zhanglh/1%E8%8A%B10.jpg" };
// 定义默认显示的图片
private static String[] imageName = new String[] { "a.jpg", "b.jpg",
"c.jpg", "d.jpg" };
Bitmap bitmap;
BaseAdapter adapter;
ImageView imageView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
final Gallery gallery = (Gallery) findViewById(R.id.gallery1);
adapter = new BaseAdapter() {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
imageView = new ImageView(ImageDemodActivity.this);
bitmap = readBitMap(ImageDemodActivity.this, position
% imageUri.length);
imageView.setImageBitmap(bitmap);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
} else {
imageView = (ImageView) convertView;
}
return imageView;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return imageUri.length;
}
};
gallery.setAdapter(adapter);
gallery.setSelection(5);
gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
if (arg2 > 4) {
gallery.setSelection(4 + imageUri.length);
}
adapter.notifyDataSetChanged();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
/**
 * 以最省内存的方式读取本地资源的图片
 * 
 */
public Bitmap readBitMap(Context context, int resId) {
InputStream is = null;
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.RGB_565;
opt.inPurgeable = true;
opt.inInputShareable = true;
// 获取资源图片
try {
URL url = new URL(imageUri[resId]);
is = url.openStream();
OutputStream os = openFileOutput(imageName[resId],
MODE_WORLD_READABLE);
byte[] buff = new byte[1024];
int hasRead = 0;
while ((hasRead = is.read(buff)) > 0) {
os.write(buff, 0, hasRead);
}
if (is != null && resId == 3) {
is.close();
os.close();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return BitmapFactory.decodeStream(is, null, opt);
}
}