Gallery配合ImageSwitcher加载大图片展示
1.获得屏幕的宽高信息
WindowManager wm = getWindowManager();
int screenWidth = wm.getDefaultDisplay().getWidth();
int screenHeight = wm.getDefaultDisplay().getHeight();
2.得到图片的宽高
BitmapFactory.Options opt = new BitmapFactory.Options();// 解析位图的附加条件
opt.inJustDecodeBounds = true; // 不解析真实的位图,只是获取这个位图的头文件信息
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), imageResourceIds[position],opt);
int bitmapWidth = opt.outWidth;
int bitmapHeight = opt.outHeight;
3.缩放比例
int scale = 1;
if(dx>dy&&dy>1) {
System.out.println("按照水平方向缩放,缩放比例是:" + dx);
scale = dx;
}
if(dy>dx&&dx>1) {
System.out.println("按照竖直方向缩放,缩放比例是:" + dy);
scale = dy;
}
// 4.缩放图片加载到内存
opt.inSampleSize = scale;
opt.inJustDecodeBounds = false; // 真正的去解析位图
bitmap = BitmapFactory.decodeResource(getResources(), imageResourceIds[position],opt);
demo实现效果:
自己mx4 pro拍的图片。。。当然酷酷的华哥不是自己拍的。。。
代码展示:
MainActivity .java
package com.example.imageswitcher;
import java.io.InputStream;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;
public class MainActivity extends Activity {
private ImageSwitcher imageSwitcher;
private Gallery gallery;
// 准备数据
private int [] imageResourceIds = {
R.drawable.m1,
R.drawable.m2,
R.drawable.m3,
R.drawable.m4
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
gallery = (Gallery) findViewById(R.id.gallery);
// 1.给gallery设置图片 需要一个适配器
gallery.setAdapter(new MyAdapter(imageResourceIds));
// 这样的时候抛空指针异常了 原因是:imageSwitcher图片切换器,在使用时,需要去设置创建View对象的工厂类
// 给ImageSwitcher设置一个工厂类,工厂类给ImageSwitcher创建一个对象
imageSwitcher.setFactory(new MyViewFactory());
// 设置切换图片时的动画效果
// 进
Animation inAnimation = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
imageSwitcher.setInAnimation(inAnimation);
// 出
Animation outAnimation = AnimationUtils.loadAnimation(this, android.R.anim.fade_out);
imageSwitcher.setOutAnimation(outAnimation);
BitmapDrawable bd=new BitmapDrawable(getmBitmap(0));
imageSwitcher.setImageDrawable(bd);
gallery.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// 把点击的图片资源id设置给ImageSwitcher
BitmapDrawable bd=new BitmapDrawable(getmBitmap(position));
imageSwitcher.setImageDrawable(bd);
}
});
}
class MyViewFactory implements ViewFactory {
/**
* 创建一个对象返回给ImageSwitcher
*/
@Override
public View makeView() {
ImageView iv = new ImageView(MainActivity.this);
return iv;
}
}
/**
* 给imageView设置图片
* @return
*/
public Bitmap getmBitmap(int position) {
// 1.得到屏幕的宽高信息
WindowManager wm = getWindowManager();
// Point outSize = new Point();
// wm.getDefaultDisplay().getSize(outSize);
int screenWidth = wm.getDefaultDisplay().getWidth();
int screenHeight = wm.getDefaultDisplay().getHeight();
System.out.println("屏幕宽高:" + screenWidth + "-" + screenHeight);
// 2.得到图片的宽高
// iv.setImageResource(imageResIDs[position]);
BitmapFactory.Options opt = new BitmapFactory.Options();// 解析位图的附加条件
opt.inPreferredConfig = Bitmap.Config.ALPHA_8;
// opt.inPurgeable = true;
// opt.inInputShareable = true;
opt.inJustDecodeBounds = true; // 不解析真是的位图,只是获取这个位图的头文件信息
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), imageResourceIds[position],opt);
int bitmapWidth = opt.outWidth;
int bitmapHeight = opt.outHeight;
System.out.println("图片宽高:" + bitmapWidth + bitmapHeight);
int dx = bitmapWidth / screenWidth;
int dy = bitmapHeight / screenHeight;
int scale = 1;
if(dx>dy&&dy>1) {
System.out.println("按照水平方向缩放,缩放比例是:" + dx);
scale = dx;
}
if(dy>dx&&dx>1) {
System.out.println("按照竖直方向缩放,缩放比例是:" + dy);
scale = dy;
}
// 4.缩放图片加载到内存
opt.inSampleSize = scale;
opt.inJustDecodeBounds = false; // 真正的去解析位图
bitmap = BitmapFactory.decodeResource(getResources(), imageResourceIds[position],opt);
// iv.setImageBitmap(bitmap2);
return bitmap;
}
class MyAdapter extends BaseAdapter {
private int []imageResIDs;
public MyAdapter(int [] imageResIDs) {
super();
this.imageResIDs = imageResIDs;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView iv = null;
if(convertView == null) {
// 当前内存中没有缓存对象,应该自己创建一个新的
iv = new ImageView(MainActivity.this);
} else {
// 当前内存中有缓存对象,应该使用缓存对象
iv = (ImageView) convertView;
}
// setImageView();
iv.setImageBitmap(getmBitmap(position));
// InputStream is = MainActivity.this.getResources().openRawResource(imageResIDs[position]);
// Bitmap bitmap = BitmapFactory.decodeStream(is);
// iv.setImageResource(bitmap);
// iv.setImageBitmap(bitmap);
// if(!bitmap.isRecycled() ){
// bitmap.recycle(); //回收图片所占的内存
// System.gc(); //提醒系统及时回收
// }
// 设置图片的宽高比和当前控件一样
iv.setAdjustViewBounds(true);
// 设置图片一周圈的边距
iv.setPadding(2, 2, 2, 2);
iv.setBackgroundColor(Color.RED);
return iv;
}
@Override
public int getCount() {
return imageResourceIds.length;
}
@Override
public Object getItem(int position) {
return imageResourceIds[position];
}
@Override
public long getItemId(int position) {
return position;
}
}
}
activity_main.xml
<LinearLayout 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:background="@android:color/black"
android:orientation="vertical" >
<ImageSwitcher
android:id="@+id/imageSwitcher"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"></ImageSwitcher>
<Gallery
android:id="@+id/gallery"
android:layout_marginTop="5dip"
android:layout_width="fill_parent"
android:layout_height="100dip"
android:spacing="20dip"></Gallery>
</LinearLayout>
完整源代码下载:http://download.youkuaiyun.com/detail/wang725/8836733