Android-相册效果(图片缩放 自由滑动)
先上图:
很多时候 我们会有这么一个需求:
展示一组图片 每个Item的图片 可以自由拉伸 滑动 焦点不冲突
网上有很多实现方法 通过自定义Gallery和ImageView来实现
个人不是很推荐 在这里推荐有ViewPager加ZoomImageView
ViewPager就不过多解释 主要是ZoomImageView
上代码:
public class ZoomImageView extends ImageView implements OnTouchListener, OnGestureListener{
private int WIDTH;
private int HEIGHT;
private int mStatusHeight; private static final String TAG = ZoomImageView.class.getSimpleName(); // This is the base transformation which is used to show the image
// initially. The current computation for this shows the image in
// it's entirety, letterboxing as needed. One could choose to
// show the image as cropped instead.
//
// This matrix is recomputed when we go from the thumbnail image to
// the full size image.
protected Matrix mBaseMatrix = new Matrix(); // This is the supplementary transformation which reflects what
// the user has done in terms of zooming and panning.
//
// This matrix reHonagAnGalleryActivitys the same when we go from the
// thumbnail image
// to the full size image.
protected Matrix mSuppMatrix = new Matrix(); // This is the final matrix which is computed as the concatentation
// of the base matrix and the supplementary matrix.
private final Matrix mDisplayMatrix = new Matrix(); // Temporary buffer used for getting the values out of a matrix.
private final float[] mMatrixValues = new float[]; // The current bitmap being displayed.
// protected final RotateBitmap mBitmapDisplayed = new RotateBitmap(null);
protected Bitmap image = null; int mThisWidth = -, mThisHeight = -; float mMaxZoom = 4.0f;
float mMinZoom = 0.1f; private int imageWidth;
private int imageHeight; private float scaleRate; public ZoomImageView(Context context) {
super(context);
init();
} public ZoomImageView(Context context, int imageWidth, int imageHeight) {
super(context);
this.imageHeight = imageHeight;
this.imageWidth = imageWidth;
init();
} public ZoomImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
} private void arithScaleRate() {
float scaleWidth = WIDTH / (float) imageWidth;
float scaleHeight = HEIGHT / (float) imageHeight;
scaleRate = Math.min(scaleWidth, scaleHeight);
if (scaleRate > ) {
scaleRate = 1f;
}
} public float getScaleRate() {
return scaleRate;
} 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;
} protected Handler mHandler = new Handler(); @Override
public void setImageResource(int resId) {
Bitmap bit = BitmapFactory.decodeResource(getResources(), resId);
setImageBitmap(bit);
} @Override
public void setImageBitmap(Bitmap bitmap) {
super.setImageBitmap(bitmap);
this.imageHeight = bitmap.getHeight();
this.imageWidth = bitmap.getWidth();
image = bitmap;
requestImage();
} public void requestImage() {
arithScaleRate();
zoomTo(scaleRate, WIDTH / 2f, WIDTH / 2f);
layoutToCenter();
} // 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 (mBitmapDisplayed.getBitmap() == null) {
// return;
// }
if (image == null) {
return;
} Matrix m = getImageViewMatrix(); RectF rect = new RectF(, , 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 = , deltaY = ; if (vertical) {
int viewHeight = getHeight();
if (height < viewHeight) {
deltaY = (viewHeight - height) / - rect.top;
} else if (rect.top > ) {
deltaY = -rect.top;
} else if (rect.bottom < viewHeight) {
deltaY = getHeight() - rect.bottom;
}
} if (horizontal) {
int viewWidth = getWidth();
if (width < viewWidth) {
deltaX = (viewWidth - width) / - rect.left;
} else if (rect.left > ) {
deltaX = -rect.left;
} else if (rect.right < viewWidth) {
deltaX = viewWidth - rect.right;
}
}
postTranslate(deltaX, deltaY);
setImageMatrix(getImageViewMatrix());
} public void setStatusHeight(int height) {
HEIGHT -= height;
} private void init() {
// except the height of the top bar
DisplayMetrics dm = getResources().getDisplayMetrics();
WIDTH = dm.widthPixels;
HEIGHT = dm.heightPixels; // HEIGHT -= getResources().getDimension(R.dimen.top_bar_height);
setScaleType(ImageView.ScaleType.MATRIX);
setOnTouchListener(this); mDetector = new GestureDetector(getContext(), this);
} public void layoutToCenter() {
float width = imageWidth * getScale();
float height = imageHeight * getScale(); float fill_width = WIDTH - width;
float fill_height = HEIGHT - height; float tran_width = 0f;
float tran_height = 0f; if (fill_width > )
tran_width = fill_width / ;
if (fill_height > )
tran_height = fill_height / ; float v[] = new float[];
Matrix m = getImageMatrix();
m.getValues(v);
float left = v[Matrix.MTRANS_X];
float top = v[Matrix.MTRANS_Y]; tran_width -= left;
tran_height -= top; postTranslate(tran_width, tran_height);
// setImageMatrix(getImageViewMatrix());
} protected float getValue(Matrix matrix, int whichValue) {
matrix.getValues(mMatrixValues);
// mMinZoom = (BrowsePictureActivity.screenWidth / 2f) / imageWidth;
mMinZoom = ;
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);
} // 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;
} static final float SCALE_RATE = 1.25F; // Sets the maximum zoom, which is a scale relative to the base matrix. It
// is calculated to show the image at 400% zoom regardless of screen or
// image orientation. If in the future we decode the full 3 megapixel image,
// rather than the current 1024x768, this should be changed down to 200%.
protected float maxZoom() {
if (image == null) {
return 1F;
} float fw = (float) image.getWidth() / (float) mThisWidth;
float fh = (float) image.getHeight() / (float) mThisHeight;
float max = Math.max(fw, fh) * ;
return max;
} protected void zoomTo(float scale, float centerX, float centerY) {
zoomTo(scale, centerX, centerY, true);
} protected void zoomTo(float scale, float centerX, float centerY,
boolean limit) {
if (limit) {
if (scale > mMaxZoom) {
scale = mMaxZoom;
} else if (scale < mMinZoom) {
scale = 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() + mMinZoom;
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 zoomBigToSmall(final float scale, final float centerX,
final float centerY, final float durationMs) {
final float incrementPerMs = (scale - getScale()) / durationMs;
final float oldScale = getScale() + mMaxZoom;
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.
} else if (getScale() <= mMinZoom) {
return;
}
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());
} private float mdy = 0.0f; protected void postTransVerticalDuration(final float dy,
final float durationMs) {
final float incrementPerMs = dy / durationMs;
final long startTime = System.currentTimeMillis();
mdy = 0.0f;
mHandler.post(new Runnable() {
public void run() {
long now = System.currentTimeMillis();
float currentMs = Math.min(durationMs, now - startTime); postTranslate(, incrementPerMs * currentMs - mdy);
mdy = incrementPerMs * currentMs; if (currentMs < durationMs) {
mHandler.post(this);
}
}
});
} private float mdx = 0.0f; protected void postTransHorizontalDuration(final float dx,
final float durationMs) {
final float incrementPerMs = dx / durationMs;
final long startTime = System.currentTimeMillis();
mdx = 0.0f;
mHandler.post(new Runnable() {
public void run() {
long now = System.currentTimeMillis();
float currentMs = Math.min(durationMs, now - startTime); postTranslate(incrementPerMs * currentMs - mdx, );
mdx = incrementPerMs * currentMs; if (currentMs < durationMs) {
mHandler.post(this);
}
}
});
} protected void panBy(float dx, float dy) {
postTranslate(dx, dy);
setImageMatrix(getImageViewMatrix());
} private float mDeltaX, mDeltaY; private float sx, sy;
private float width, height;
private float values[] = new float[];
private float baseValue;
private float originalScale; private final int NONE = ;
private final int DRAG = ;
private final int ZOOM = ;
private int mMod = ; @Override
public boolean onTouch(View v, MotionEvent event) {
if (mDetector != null) {
mDetector.onTouchEvent(event);
}
final ZoomImageView imageView = this;
final int sWidth = WIDTH;
final int sHeight = HEIGHT;
width = imageView.getScale() * imageView.getImageWidth();
height = imageView.getScale() * imageView.getImageHeight(); Matrix matrix = imageView.getImageMatrix();
matrix.getValues(values);
float x = event.getX();
float y = event.getY();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN: mMod = DRAG; baseValue = ;
originalScale = imageView.getScale(); mDeltaX = ;
mDeltaY = ;
sx = x;
sy = y;
break;
case MotionEvent.ACTION_POINTER_DOWN: mMod = ZOOM;
break;
case MotionEvent.ACTION_MOVE: if (event.getPointerCount() == ) {
mMod = ZOOM;
}
float deltaX = sx - x;
float deltaY = sy - y;
sx = x;
sy = y;
if (deltaX != 0.0f) {
mDeltaX = deltaX;
} if (deltaY != 0.0f) {
mDeltaY = deltaY;
} switch (mMod) {
case DRAG:
final float left = values[Matrix.MTRANS_X];
final float right = left + width; if(right < sWidth && Math.abs(right - sWidth) >= 0.0001f && right < width) {
v.getParent().requestDisallowInterceptTouchEvent(false);
}
else if(right > width) {
v.getParent().requestDisallowInterceptTouchEvent(false);
}
else {
v.getParent().requestDisallowInterceptTouchEvent(true);
} if (width < sWidth) {
deltaX = 0.0f;
mDeltaX = deltaX;
} if (height < sHeight) {
deltaY = 0.0f;
mDeltaY = deltaY;
} imageView.postTranslate(-deltaX, -deltaY);
break;
case ZOOM:
if (event.getPointerCount() < ) {
return true;
} float dx = event.getX() - event.getX();
float dy = event.getY() - event.getY();
float value = FloatMath.sqrt(dx * dx + dy * dy);
if (baseValue == ) {
baseValue = value;
} else {
float scale = value / baseValue;
// scale the image
imageView.zoomTo(originalScale * scale,
(event.getX() + event.getX()) / ,
(event.getY() + event.getY()) / , false);
}
break;
default:
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL: final float left = values[Matrix.MTRANS_X];
final float right = left + width;
final float top = values[Matrix.MTRANS_Y];
final float bottom = top + height; switch (mMod) {
case DRAG:
final float startX = mDeltaX;
final float startY = mDeltaY;
if (startX != ) {
if (right < sWidth) {
// ((ViewPager) v.getParent()).requestDisallowInterceptTouchEvent(false);
postTransHorizontalDuration(sWidth - right, 200.0f);
} if (left >= ) {
postTransHorizontalDuration(-left, 200.0f);
}
} if (startY != ) {
if (bottom < sHeight) {
postTransVerticalDuration(sHeight - bottom, 200.0f);
} if (top > ) {
postTransVerticalDuration(-top, 200.0f);
}
} break;
case ZOOM:
final float sr = getScaleRate();
final float scale = getScale();
mDoubleClick = false;
if (sr > scale) {
imageView.zoomTo(sr, sWidth / , sHeight / , 200.0f);
return true;
} else if (mMaxZoom < scale) {
imageView.zoomBigToSmall(sr, sWidth / , sHeight / ,
300.0f);
return true;
}
break;
} mMod = NONE;
break;
default:
} return true;
} private boolean mDoubleClick;
private GestureDetector mDetector;
private OnSimpleListener listener; public void doubleClick() {
float scale = getScale();
if (!mDoubleClick) {
zoomTo(1.0f);
mDoubleClick = true;
} else if (scale == 1.0f) {
zoomTo(2.0f);
} else if (scale == 2.0f) {
zoomTo(4.0f);
} else if (scale == 4.0f) {
zoomTo(1.0f);
}
} @Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
} @Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub } @Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
if(listener != null) {
listener.setOnSimpleClickListenr();
}
return true;
} @Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
return false;
} @Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub } @Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
return false;
} public interface OnSimpleListener {
void setOnSimpleClickListenr();
} public void setOnSimpleClickListenr(OnSimpleListener listener) {
this.listener = listener;
} }
为了不和ViewPager的滑动冲突 主要是在action_move里面加了一个判断:
final float left = values[Matrix.MTRANS_X];
final float right = left + width; if(right < sWidth && Math.abs(right - sWidth) >= 0.0001f && right < width) {
v.getParent().requestDisallowInterceptTouchEvent(false);
}
else if(right > width) {
v.getParent().requestDisallowInterceptTouchEvent(false);
}
else {
v.getParent().requestDisallowInterceptTouchEvent(true);
}
需要源码的M我~~~
Android-相册效果(图片缩放 自由滑动)的更多相关文章
- Android 简单的图片缩放方法
很简单的一个图片缩放方法,注意要比例设置正确否则可能会内存溢出 相关问题 java.lang.IllegalArgumentException: bitmap size exceeds 32bits ...
- Android实现本地图片选择及预览缩放效果仿春雨医生
在做项目时常常会遇到选择本地图片的需求.曾经都是懒得写直接调用系统方法来选择图片.可是这样并不能实现多选效果.近期又遇到了,所以还是写一个demo好了.以后也方便使用.还是首先来看看效果 显示的图片使 ...
- Android仿微信图片上传,可以选择多张图片,缩放预览,拍照上传等
仿照微信,朋友圈分享图片功能 .可以进行图片的多张选择,拍照添加图片,以及进行图片的预览,预览时可以进行缩放,并且可以删除选中状态的图片 .很不错的源码,大家有需要可以下载看看 . 微信 微信 微信 ...
- [Android实例教程] 教你如何拍照+相册选择图片+剪裁图片完整实现
[Android实例教程] 教你如何拍照+相册选择图片+剪裁图片完整实现 今天做Android项目的时候要用到图片选择,要实现拍照获取图片和从相册获取图片,并且要求在获取完之后可以裁剪,试了很多方法之 ...
- android多点触控自由对图片缩放
在系统的相册中,观看相片就可以用多个手指进行缩放. 要实现这个功能,只需要这几步: 1.新建项目,在项目中新建一个ZoomImage.java public class ZoomImageView e ...
- Android相机、相册获取图片显示并保存到SD卡
Android相机.相册获取图片显示并保存到SD卡 [复制链接] 电梯直达 楼主 发表于 2013-3-13 19:51:43 | 只看该作者 |只看大图 本帖最后由 happy小妖同学 ...
- android 照相或从相册获取图片并裁剪
照相或从相册获取图片并裁剪 在android应用中很多时候都要获取图片(例如获取用户的头像)就需要从用户手机上获取图片.可以直接照,也可以从用户SD卡上获取图片,但获取到的图片未必能达到要求.所以要对 ...
- Android调用相册截取图片遇到的问题
1.在Android中可以使用如下的方式来调用相册,选择图片进行裁剪使用,昨天在开发的时候遇到一个问题 private void cropBigImageUri(Uri uri, int output ...
- android关于图片缩放
网上有许多关于图片缩放的demo,本人都感觉不怎么好用,最近在github看到了 一个简单的支持多指缩放图片的Android View类 gesture-imageview (地址:https://g ...
随机推荐
- SQL Server(基本) 关键字的使用 一
一, 基础关键字 -- 使用介绍 1,select 的使用(select 结果集) SELECT 列名称 FROM 表名称 以及: (*)是选取所有列的快捷方式. SELECT * FROM 表名称 ...
- items 与iteritems
dict的items函数返回的是键值对的元组的列表,而iteritems使用的是键值对的generator. items当使用时会调用整个列表 iteritems当使用时只会调用值. >> ...
- C++例题练习(1)
环境:Dev-C++( Version:5.6.1) 一.求2个或3个正整数中的最大数,用带有默认参数的函数实现 代码实现: #include <iostream> using names ...
- 解决VS2012【加载......符号缓慢】的问题
http://blog.csdn.net/shi0090/article/details/19411777 最近在用VS2012调试时,经常出现"加载......符号缓慢的问题", ...
- 点击穿透问题(http://www.tuicool.com/articles/6NfaUnM)
一.click与300ms延迟 移动浏览器提供一个特殊的功能:双击(double tap)放大 300ms的延迟就来自这里,用户碰触页面之后,需要等待一段时间来判断是不是双击(double tap)动 ...
- 11_Servlet基础知识
[概念] Servlet通常被称为服务端小程序,是运行在服务端的程序,用于处理及相应客户端的请求. Servlet是用java语言开发网页动态资源的技术. [特点] 1.Servlet是个特殊的Jav ...
- 九度OJ 1056--最大公约数 1439--Least Common Multiple 【辗转相除法】
题目地址:http://ac.jobdu.com/problem.php?pid=1056 题目描述: 输入两个正整数,求其最大公约数. 输入: 测试数据有多组,每组输入两个正整数. 输出: 对于每组 ...
- OpenJudge / Poj 1928 The Peanuts C++
链接地址:http://bailian.openjudge.cn/practice/1928 题目: 总时间限制: 1000ms 内存限制: 65536kB 描述 Mr. Robinson and h ...
- c#个人记录常用方法(更新中)
1.日期毫秒转换为标准的C#日期格式 //使用时,先将秒Convert.ToInt64,返回格式2015-2-10 14:03:33 public DateTime JavaTimeToC(long ...
- sae-多个file_put_contents('saestor://public/text.txt',$data);只写第一次
多个file_put_contents('saestor://public/text.txt',$data); 只执行第一个文件的写入,永久存储也只需要一次写入 如果需要用户中间缓存文件,用tmpfs ...