图片处理,如何避免大图片加载的OOM

/**    是通过网上的一个开源框改写的,支持2.2以上版本

运行版本是2.2,正常

     * 缩放图片
     * 
     * @param imagePath
     * @return 一个缩放好的bitmap
     */
    public static Bitmap getZoomBitmap(String imagePath) {
// 解决图片内存溢出问题
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;// 这样就只返回图片参数
// 获取这个图片的宽和高
Bitmap bm = BitmapFactory.decodeFile(imagePath, options); // 此时返回bm为空
options.inJustDecodeBounds = false;// 上面操作完后,要设回来,不然下面同样获取不到实际图片
// 计算缩放比
int scale = (int) (options.outHeight / (float) 300);
// 上面算完后一下如果比200大,那就scale就大于1,那么就压缩scale,如果比200小,那图片肯定很小了,直接按原图比例显示就行
if (scale <= 0) {
scale = 1;
}
options.inSampleSize = scale;// scale=2.表示压缩为原来的1/2,以此类推
// 重新读入图片,注意在这之前要把options.inJustDecodeBounds 设为 false!
bm = BitmapFactory.decodeFile(imagePath, options);
return bm;
    }

以是处理加载的图片处理,效果图如下:


Galley图片放大缩小,双击放大或者缩放,双指放大,效果如下:

public class PhotoGallery extends Gallery implements OnTouchListener{
private int kEvent = KEY_INVALID; //invalid
public static final int KEY_INVALID = -1;

private GestureDetector gesture;
private MyImageView imageView;
private int screenWidth,screenHeight;
private float v[] = new float[9];
public PhotoGallery(Context context) {
super(context);
init();
}


public PhotoGallery(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}


public PhotoGallery(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}


/****************************
* 初始化方法
*/
private void init(){
screenWidth = getContext().getResources().getDisplayMetrics().widthPixels;
screenHeight = getContext().getResources().getDisplayMetrics().heightPixels;
this.setOnTouchListener(this);
}

public void setGestureDetector(GestureDetector dectector) {
gesture = dectector;
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
View view = this.getSelectedView();
if (view instanceof MyImageView) {
float xdistance = calXdistance(e1, e2);
float min_distance = screenWidth / 4f;
if (isScrollingLeft(e1, e2) && xdistance > min_distance) {
kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
} else if (!isScrollingLeft(e1, e2) && xdistance > min_distance) {
kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
Log.i("TAG", "xdistance="+xdistance);
xdistance = min_distance;
}

imageView = (MyImageView) view;


Matrix m = imageView.getImageMatrix();
m.getValues(v);
// 图片实时的上下左右坐标
float left, right;
// 图片的实时宽,高
float width = imageView.getScale() * imageView.getImageWidth();
float height = imageView.getScale() * imageView.getImageHeight();
// 如果图片当前大小<屏幕大小,直接处理滑屏事件
if ((int) width <= screenWidth&& (int) height <= screenHeight){
super.onScroll(e1, e2, distanceX, distanceY);
} else {
left = v[Matrix.MTRANS_X];
right = left + width;
Rect r = new Rect();
imageView.getGlobalVisibleRect(r);


if (distanceX > 0)// 向左滑动
{
if (r.left > 0 || right < screenWidth) {// 判断当前ImageView是否显示完全
super.onScroll(e1, e2, distanceX, distanceY);
} else {
imageView.postTranslate(-distanceX, -distanceY);
}
} else if (distanceX < 0)// 向右滑动
{
if (r.right < screenWidth || left > 0) {
super.onScroll(e1, e2, distanceX, distanceY);
} else {
imageView.postTranslate(-distanceX, -distanceY);
}
}


}


} else {
super.onScroll(e1, e2, distanceX, distanceY);
}
return false;
}


private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2) {
return e2.getX() > e1.getX();
}


private float calXdistance(MotionEvent e1, MotionEvent e2) {
return Math.abs(e2.getX() - e1.getX());
}



@Override//这个方法必须重载,否则无阻
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
return false;//设置为false
}

@Override
public boolean onTouchEvent(MotionEvent event) {
if (gesture != null) {
gesture.onTouchEvent(event);
}
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
// 判断边界是否越界
View view = PhotoGallery.this.getSelectedView();
if (view instanceof MyImageView) {

if(kEvent != KEY_INVALID) { // 是否切换上一页或下一页
onKeyDown(kEvent, null);
kEvent = KEY_INVALID;
}

imageView = (MyImageView) view;
float width = imageView.getScale() * imageView.getImageWidth();
float height = imageView.getScale()
* imageView.getImageHeight();
// 如果图片当前大小<屏幕大小,判断边界
if ((int) width <= screenWidth && (int) height <= screenHeight) {
break;
}
float v[] = new float[9];
Matrix m = imageView.getImageMatrix();
m.getValues(v);
float top = v[Matrix.MTRANS_Y];
float bottom = top + height;
if (top < 0 && bottom < screenHeight) {
// imageView.postTranslateDur(-top, 200f);
imageView.postTranslateDur(screenHeight
- bottom, 200f);
}
if (top > 0 && bottom > screenHeight) {
// imageView.postTranslateDur(PictureViewActivity.screenHeight
// - bottom, 200f);
imageView.postTranslateDur(-top, 200f);
}

float left =v[Matrix.MTRANS_X];
float right = left + width;
if(left<0 && right< screenWidth){
// imageView.postTranslateXDur(-left, 200f);
imageView.postTranslateXDur(screenWidth
- right, 200f);
}
if(left>0 && right>screenWidth){
// imageView.postTranslateXDur(PictureViewActivity.screenWidth
// - right, 200f);
imageView.postTranslateXDur(-left, 200f);
}
}
break;
}
return super.onTouchEvent(event);
}


private float baseValue;
private float originalScale;
@Override
public boolean onTouch(View v, MotionEvent event) {
View view = PhotoGallery.this.getSelectedView();
if (view instanceof MyImageView) {
imageView = (MyImageView) view;


if (event.getAction() == MotionEvent.ACTION_DOWN) {
baseValue = 0;
originalScale = imageView.getScale();
}
if (event.getAction() == MotionEvent.ACTION_MOVE) {
if (event.getPointerCount() == 2) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
float value = (float) Math.sqrt(x * x + y * y);// 计算两点的距离
// System.out.println("value:" + value);
if (baseValue == 0) {
baseValue = value;
} else {
float scale = value / baseValue;// 当前两点间的距离除以手指落下时两点间的距离就是需要缩放的比例。
// scale the image
imageView.zoomTo(originalScale * scale, x
+ event.getX(1), y + event.getY(1));


}
}
}
}
return false;
}
}


public class MyImageView extends ImageView {

protected Matrix mBaseMatrix = new Matrix();



protected Matrix mSuppMatrix = new Matrix();



private final Matrix mDisplayMatrix = new Matrix();

private final float[] mMatrixValues = new float[9];


// The current bitmap being displayed.
protected Bitmap image = null;


protected Handler mHandler = new Handler();


int mThisWidth = -1, mThisHeight = -1;//布局后的宽度和高度,由于是全屏显示,这两个值等于屏幕分辨率


float mMaxZoom;// 最大缩放比例
float mMinZoom;// 最小缩放比例


private int imageWidth;// 图片的原始宽度
private int imageHeight;// 图片的原始高度


// float scaleRate;// 图片适应屏幕的缩放比例
static final float SCALE_RATE = 1.25F;


public MyImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}


public MyImageView(Context context) {
super(context);
init();
}


@Override
public void setImageBitmap(Bitmap bitmap) {
super.setImageBitmap(bitmap);
image = bitmap;
setImageHeight(bitmap.getHeight());
setImageWidth(bitmap.getWidth());
Drawable d = getDrawable();
if (d != null) {
d.setDither(true);
}
}


// Center as much as possible in one or both axis. Centering is
// defined as follows: if the image is scaled down below the
// view's dimensions then center it (literally). If the image
// is scaled larger than the view and is translated out of view
// then translate it back into view (i.e. eliminate black bars).
protected void center(boolean horizontal, boolean vertical) {
if (image == null) {
return;
}


Matrix m = getImageViewMatrix();


RectF rect = new RectF(0, 0, image.getWidth(), image.getHeight());
// RectF rect = new RectF(0, 0, imageWidth*getScale(),
// imageHeight*getScale());


m.mapRect(rect);


float height = rect.height();
float width = rect.width();


float deltaX = 0, deltaY = 0;


if (vertical) {
int viewHeight = getHeight();
if (height < viewHeight) {
deltaY = (viewHeight - height) / 2 - rect.top;
} else if (rect.top > 0) {
deltaY = -rect.top;
} else if (rect.bottom < viewHeight) {
deltaY = getHeight() - rect.bottom;
}
}


if (horizontal) {
int viewWidth = getWidth();
if (width < viewWidth) {
deltaX = (viewWidth - width) / 2 - rect.left;
} else if (rect.left > 0) {
deltaX = -rect.left;
} else if (rect.right < viewWidth) {
deltaX = viewWidth - rect.right;
}
}


postTranslate(deltaX, deltaY);
setImageMatrix(getImageViewMatrix());
}


private void init() {
setScaleType(ImageView.ScaleType.MATRIX);
}


// Setup the base matrix so that the image is centered and scaled properly.
private void getProperBaseMatrix(Bitmap bitmap, Matrix matrix) {
float viewWidth = getWidth();
float viewHeight = getHeight();


float w = bitmap.getWidth();
float h = bitmap.getHeight();
matrix.reset();


// We limit up-scaling to 3x otherwise the result may look bad if it's
// a small icon.
float scale = Math.min(viewWidth / w, viewHeight / h);

mMinZoom = scale;
mMaxZoom = 2*scale;

matrix.postScale(scale, scale);


matrix.postTranslate((viewWidth - w * scale) / 2F, (viewHeight - h
* scale) / 2F);
}


protected float getValue(Matrix matrix, int whichValue) {
matrix.getValues(mMatrixValues);
return mMatrixValues[whichValue];
}


// Get the scale factor out of the matrix.
protected float getScale(Matrix matrix) {
return getValue(matrix, Matrix.MSCALE_X);
}


protected float getScale() {
return getScale(mSuppMatrix)*mMinZoom;
}

public float getScaleRate() {
return getScale(mSuppMatrix);
}

public float getMiniZoom() {
return mMinZoom;
}

public float getMaxZoom() {
return mMaxZoom;
}


// Combine the base matrix and the supp matrix to make the final matrix.
protected Matrix getImageViewMatrix() {
// The final matrix is computed as the concatentation of the base matrix
// and the supplementary matrix.
mDisplayMatrix.set(mBaseMatrix);
mDisplayMatrix.postConcat(mSuppMatrix);
return mDisplayMatrix;
}


@Override
protected void onLayout(boolean changed, int left, int top, int right,
int bottom) {
super.onLayout(changed, left, top, right, bottom);
mThisWidth = right - left;
mThisHeight = bottom - top;
if (image != null) {
getProperBaseMatrix(image, mBaseMatrix);
setImageMatrix(getImageViewMatrix());
}
}


public void zoomTo(float scale, float centerX, float centerY) {
if (scale > mMaxZoom) {
scale = mMaxZoom;
} else if (scale < mMinZoom) {
scale = mMinZoom;
}


// Logger.d(DEBUG, "[MyImageView.zoomTo]" + "scale=" + scale
// + ",mMaxZoom=" + mMaxZoom + ",mMinZoom=" + mMinZoom);
float oldScale = getScale();
float deltaScale = scale / oldScale;


mSuppMatrix.postScale(deltaScale, deltaScale, centerX, centerY);
setImageMatrix(getImageViewMatrix());
center(true, true);
}


protected void zoomTo(final float scale, final float centerX,
final float centerY, final float durationMs) {
final float incrementPerMs = (scale - getScale()) / durationMs;
final float oldScale = getScale();
final long startTime = System.currentTimeMillis();


mHandler.post(new Runnable() {
public void run() {
long now = System.currentTimeMillis();
float currentMs = Math.min(durationMs, now - startTime);
float target = oldScale + (incrementPerMs * currentMs);
zoomTo(target, centerX, centerY);
if (currentMs < durationMs) {
mHandler.post(this);
}
}
});
}


protected void zoomTo(float scale) {
float cx = getWidth() / 2F;
float cy = getHeight() / 2F;


zoomTo(scale, cx, cy);
}


protected void zoomToPoint(float scale, float pointX, float pointY) {
float cx = getWidth() / 2F;
float cy = getHeight() / 2F;


panBy(cx - pointX, cy - pointY);
zoomTo(scale, cx, cy);
}


protected void zoomIn() {
zoomIn(SCALE_RATE);
}


protected void zoomOut() {
zoomOut(SCALE_RATE);
}


protected void zoomIn(float rate) {
if (getScale() >= mMaxZoom) {
return; // Don't let the user zoom into the molecular level.
}
if (image == null) {
return;
}


float cx = getWidth() / 2F;
float cy = getHeight() / 2F;


mSuppMatrix.postScale(rate, rate, cx, cy);
setImageMatrix(getImageViewMatrix());
}


protected void zoomOut(float rate) {
if (image == null) {
return;
}


float cx = getWidth() / 2F;
float cy = getHeight() / 2F;


// Zoom out to at most 1x.
Matrix tmp = new Matrix(mSuppMatrix);
tmp.postScale(1F / rate, 1F / rate, cx, cy);


if (getScale(tmp) < 1F) {
mSuppMatrix.setScale(1F, 1F, cx, cy);
} else {
mSuppMatrix.postScale(1F / rate, 1F / rate, cx, cy);
}
setImageMatrix(getImageViewMatrix());
center(true, true);
}


public void postTranslate(float dx, float dy) {
mSuppMatrix.postTranslate(dx, dy);
setImageMatrix(getImageViewMatrix());
}


float _dy = 0.0f;


protected void postTranslateDur(final float dy, final float durationMs) {
_dy = 0.0f;
final float incrementPerMs = dy / durationMs;
final long startTime = System.currentTimeMillis();
mHandler.post(new Runnable() {
public void run() {
long now = System.currentTimeMillis();
float currentMs = Math.min(durationMs, now - startTime);


postTranslate(0, incrementPerMs * currentMs - _dy);
_dy = incrementPerMs * currentMs;


if (currentMs < durationMs) {
mHandler.post(this);
}
}
});
}


float _dx = 0.0f;


protected void postTranslateXDur(final float dx, final float durationMs) {
_dx = 0.0f;
final float incrementPerMs = dx / durationMs;
final long startTime = System.currentTimeMillis();
mHandler.post(new Runnable() {
public void run() {
long now = System.currentTimeMillis();
float currentMs = Math.min(durationMs, now - startTime);


postTranslate(incrementPerMs * currentMs - _dx, 0);
_dx = incrementPerMs * currentMs;


if (currentMs < durationMs) {
mHandler.post(this);
}
}
});
}


protected void panBy(float dx, float dy) {
postTranslate(dx, dy);
setImageMatrix(getImageViewMatrix());
}


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
event.startTracking();
return true;
}
return super.onKeyDown(keyCode, event);
}


@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.isTracking()
&& !event.isCanceled()) {
if (getScale() > mMinZoom) {
zoomTo(mMinZoom);
return true;
}
}
return super.onKeyUp(keyCode, event);
}


public int getImageWidth() {
return imageWidth;
}


public void setImageWidth(int imageWidth) {
this.imageWidth = imageWidth;
}


public int getImageHeight() {
return imageHeight;
}


public void setImageHeight(int imageHeight) {
this.imageHeight = imageHeight;
}


}


gallery = new PhotoGallery(this);
gallery.setVerticalFadingEdgeEnabled(false);// 取消竖直渐变边框
gallery.setHorizontalFadingEdgeEnabled(false);// 取消水平渐变边框
//设置间距
gallery.setSpacing(this.getResources().getDimensionPixelSize(R.dimen.activity_horizontal_margin));
gallery.setGestureDetector(new GestureDetector(this,new MySimpleGesture()));

adapter = new MyAdapter(this,datas);
gallery.setAdapter(adapter);
setContentView(gallery);










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值