如何使用通用图像加载器和查看寻呼机在Android中缩放图像
我已经扩展了图像视图,并将其传递给Universal image Loader。它工作正常,但有时由于一些解码图像缩放问题(在log cat中找到),会出现内存不足问题 以下是我的代码:如何使用通用图像加载器和查看寻呼机在Android中缩放图像,android,android-viewpager,out-of-memory,universal-image-loader,zooming,Android,Android Viewpager,Out Of Memory,Universal Image Loader,Zooming,我已经扩展了图像视图,并将其传递给Universal image Loader。它工作正常,但有时由于一些解码图像缩放问题(在log cat中找到),会出现内存不足问题 以下是我的代码: public class TouchImageView extends ImageView { Matrix matrix = new Matrix(); // We can be in one of these 3 states static final int NONE = 0;
public class TouchImageView extends ImageView {
Matrix matrix = new Matrix();
// We can be in one of these 3 states
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;
// Remember some things for zooming
PointF last = new PointF();
PointF start = new PointF();
float minScale = 1f;
float maxScale = ImageConstants.DEFAULT_MAX_ZOOM;
float[] matrixScale;
float redundantXSpace;
float redundantYSpace;
float width;
float height;
static final int CLICK = 3;
final int measureValue = 2;
float saveScale = 1f;
float right;
float bottom;
float origWidth;
float origHeight;
float bmWidth;
float bmHeight;
ScaleGestureDetector mScaleDetector;
Context context;
public TouchImageView(Context context) {
super(context);
sharedConstructing(context);
}
public TouchImageView(Context context, AttributeSet attrs) {
super(context, attrs);
sharedConstructing(context);
}
private void sharedConstructing(Context context) {
super.setClickable(true);
this.context = context;
final int matrixScaleSize = 9;
mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
matrix.setTranslate(1f, 1f);
matrixScale = new float[matrixScaleSize];
setImageMatrix(matrix);
setScaleType(ScaleType.MATRIX);
setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
mScaleDetector.onTouchEvent(event);
matrix.getValues(matrixScale);
float x = matrixScale[Matrix.MTRANS_X];
float y = matrixScale[Matrix.MTRANS_Y];
PointF curr = new PointF(event.getX(), event.getY());
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
last.set(event.getX(), event.getY());
start.set(last);
mode = DRAG;
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) {
float deltaX = curr.x - last.x;
float deltaY = curr.y - last.y;
float scaleWidth = Math.round(origWidth * saveScale);
float scaleHeight = Math.round(origHeight * saveScale);
if (scaleWidth < width) {
deltaX = 0;
if (y + deltaY > 0)
deltaY = -y;
else if (y + deltaY < -bottom)
deltaY = -(y + bottom);
} else if (scaleHeight < height) {
deltaY = 0;
if (x + deltaX > 0)
deltaX = -x;
else if (x + deltaX < -right)
deltaX = -(x + right);
} else {
if (x + deltaX > 0)
deltaX = -x;
else if (x + deltaX < -right)
deltaX = -(x + right);
if (y + deltaY > 0)
deltaY = -y;
else if (y + deltaY < -bottom)
deltaY = -(y + bottom);
}
matrix.postTranslate(deltaX, deltaY);
last.set(curr.x, curr.y);
}
break;
case MotionEvent.ACTION_UP:
mode = NONE;
int xDiff = (int) Math.abs(curr.x - start.x);
int yDiff = (int) Math.abs(curr.y - start.y);
if (xDiff < CLICK && yDiff < CLICK)
performClick();
break;
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
break;
default:
break;
}
setImageMatrix(matrix);
invalidate();
return true; // indicate event was handled
}
});
}
@Override
public void setImageBitmap(Bitmap bm) {
super.setImageBitmap(bm);
if (bm != null) {
bmWidth = bm.getWidth();
bmHeight = bm.getHeight();
}
}
public void setMaxZoom(float x) {
maxScale = x;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width = MeasureSpec.getSize(widthMeasureSpec);
height = MeasureSpec.getSize(heightMeasureSpec);
// Fit to screen.
float scale;
float scaleX = (float) width / (float) bmWidth;
float scaleY = (float) height / (float) bmHeight;
scale = Math.min(scaleX, scaleY);
matrix.setScale(scale, scale);
setImageMatrix(matrix);
saveScale = 1f;
// Center the image
redundantYSpace = (float) height - (scale * (float) bmHeight);
redundantXSpace = (float) width - (scale * (float) bmWidth);
redundantYSpace /= (float) measureValue;
redundantXSpace /= (float) measureValue;
matrix.postTranslate(redundantXSpace, redundantYSpace);
origWidth = width - measureValue * redundantXSpace;
origHeight = height - measureValue * redundantYSpace;
right = width * saveScale - width - (measureValue * redundantXSpace * saveScale);
bottom = height * saveScale - height - (measureValue * redundantYSpace * saveScale);
setImageMatrix(matrix);
}
private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
mode = ZOOM;
return true;
}
@Override
public boolean onScale(ScaleGestureDetector detector) {
float mScaleFactor = detector.getScaleFactor();
float origScale = saveScale;
saveScale *= mScaleFactor;
if (saveScale > maxScale) {
saveScale = maxScale;
mScaleFactor = maxScale / origScale;
} else if (saveScale < minScale) {
saveScale = minScale;
mScaleFactor = minScale / origScale;
}
right = width * saveScale - width - (measureValue * redundantXSpace * saveScale);
bottom = height * saveScale - height - (measureValue * redundantYSpace * saveScale);
if (origWidth * saveScale <= width || origHeight * saveScale <= height) {
matrix.postScale(mScaleFactor, mScaleFactor, width / measureValue, height / measureValue);
if (mScaleFactor < 1) {
matrix.getValues(matrixScale);
float x = matrixScale[Matrix.MTRANS_X];
float y = matrixScale[Matrix.MTRANS_Y];
if (mScaleFactor < 1) {
if (Math.round(origWidth * saveScale) < width) {
if (y < -bottom)
matrix.postTranslate(0, -(y + bottom));
else if (y > 0)
matrix.postTranslate(0, -y);
} else {
if (x < -right)
matrix.postTranslate(-(x + right), 0);
else if (x > 0)
matrix.postTranslate(-x, 0);
}
}
}
} else {
matrix.postScale(mScaleFactor, mScaleFactor, detector.getFocusX(), detector.getFocusY());
matrix.getValues(matrixScale);
float x = matrixScale[Matrix.MTRANS_X];
float y = matrixScale[Matrix.MTRANS_Y];
if (mScaleFactor < 1) {
if (x < -right)
matrix.postTranslate(-(x + right), 0);
else if (x > 0)
matrix.postTranslate(-x, 0);
if (y < -bottom)
matrix.postTranslate(0, -(y + bottom));
else if (y > 0)
matrix.postTranslate(0, -y);
}
}
return true;
}
}
公共类TouchImageView扩展了ImageView{
矩阵=新矩阵();
//我们可能处于这三种状态之一
静态最终int NONE=0;
静态最终整数阻力=1;
静态最终整数缩放=2;
int模式=无;
//记住一些关于缩放的事情
PointF last=新的PointF();
PointF start=新的PointF();
浮动最小刻度=1f;
float maxScale=ImageConstants.DEFAULT\u MAX\u ZOOM;
浮动[]矩阵标度;
浮动冗余X空间;
浮动冗余空间;
浮动宽度;
浮动高度;
静态最终int CLICK=3;
最终int测量值=2;
浮动存储比例=1f;
向右浮动;
浮底;
浮栅宽度;
浮空灯;
浮动宽度;
浮动高度;
scalegestruedetector mScaleDetector;
语境;
公共TouchImageView(上下文){
超级(上下文);
共享构造(上下文);
}
公共TouchImageView(上下文、属性集属性){
超级(上下文,attrs);
共享构造(上下文);
}
私有void共享构造(上下文){
super.setClickable(true);
this.context=上下文;
最终整数矩阵calesize=9;
mScaleDetector=新的scalegestruedetector(上下文,新的ScaleListener());
矩阵.setTranslate(1f,1f);
matrixScale=新浮点[matrixScaleSize];
setImageMatrix(矩阵);
setScaleType(ScaleType.MATRIX);
setOnTouchListener(新的OnTouchListener(){
@凌驾
公共布尔onTouch(视图v,运动事件){
mScaleDetector.onTouchEvent(事件);
matrix.getValues(matrixScale);
float x=矩阵刻度[Matrix.MTRANS_x];
float y=矩阵刻度[Matrix.MTRANS_y];
PointF curr=新的PointF(event.getX(),event.getY());
开关(event.getAction()){
case MotionEvent.ACTION\u DOWN:
set(event.getX(),event.getY());
开始。设置(最后);
模式=拖动;
打破
case MotionEvent.ACTION\u移动:
如果(模式==拖动){
浮动deltaX=当前x-最后x;
浮动三角洲=当前y-最后y;
float scaleWidth=Math.round(origWidth*saveScale);
float scalehHeight=数学圆整(origHeight*saveScale);
if(标度宽度<宽度){
deltaX=0;
如果(y+deltaY>0)
deltaY=-y;
否则如果(y+deltaY<-底部)
三角洲=-(y+底部);
}else if(刻度高度<高度){
deltaY=0;
如果(x+deltaX>0)
deltaX=-x;
否则如果(x+deltaX<-右)
deltaX=-(x+右侧);
}否则{
如果(x+deltaX>0)
deltaX=-x;
否则如果(x+deltaX<-右)
deltaX=-(x+右侧);
如果(y+deltaY>0)
deltaY=-y;
否则如果(y+deltaY<-底部)
三角洲=-(y+底部);
}
矩阵。后翻译(deltaX、deltaY);
最后一组(当前x、当前y);
}
打破
case MotionEvent.ACTION\u UP:
模式=无;
intxdiff=(int)Math.abs(curr.x-start.x);
int yDiff=(int)Math.abs(curr.y-start.y);
if(xDiff