/**
* MyImageView.java
* @version 1.0
* @author Haven
* @createTime 2011-12-9 下午03:12:30
* 此类代码是根据android系统自带的ImageViewTouchBase代码修改
*/
package com.havenliu.demo;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.RectF;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.widget.ImageView;
public class MyImageView extends ImageView {
@SuppressWarnings("unused")
private static final String TAG = "ImageViewTouchBase";
// 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 remains 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[9];
// The current bitmap being displayed.
// protected final RotateBitmap mBitmapDisplayed = new RotateBitmap(null);
protected Bitmap image = null;
int mThisWidth = -1, mThisHeight = -1;
float mMaxZoom = 2.0f;// 最大缩放比例
float mMinZoom ;// 最小缩放比例
private int imageWidth;// 图片的原始宽度
private int imageHeight;// 图片的原始高度
private float scaleRate;// 图片适应屏幕的缩放比例
public MyImageView(Context context, int imageWidth, int imageHeight) {
super(context);
this.imageHeight = imageHeight;
this.imageWidth = imageWidth;
init();
}
public MyImageView(Context context, AttributeSet attrs, int imageWidth, int imageHeight) {
super(context, attrs);
this.imageHeight = imageHeight;
this.imageWidth = imageWidth;
init();
}
/**
* 计算图片要适应屏幕需要缩放的比例
*/
private void arithScaleRate() {
float scaleWidth = Main.screenWidth / (float) imageWidth;
float scaleHeight = Main.screenHeight / (float) imageHeight;
scaleRate = Math.min(scaleWidth, scaleHeight);
}
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;
}
@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() > 1.0f) {
// If we're zoomed in, pressing Back jumps out to show the
// entire image, otherwise Back returns the user to the gallery.
zoomTo(1.0f);
return true;
}
}
return super.onKeyUp(keyCode, event);
}
protected Handler mHandler = new Handler();
@Override
public void setImageBitmap(Bitmap bitmap) {
super.setImageBitmap(bitmap);
image = bitmap;
// 计算适应屏幕的比例
arithScaleRate();
//缩放到屏幕大小
zoomTo(scaleRate,Main.screenWidth / 2f, Main.screenHeight / 2f);
//居中
layoutToCenter();
// imageView.zoomTo(scaleRate, Main.screenWidth / 2, Main.screenHeight / 2
// center(true, 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 (mBitmapDisplayed.getBitmap() == null) {
// return;
// }
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);
}
/**
* 设置图片居中显示
*/
public void layoutToCenter()
{
//正在显示的图片实际宽高
float width = imageWidth*getScale();
float height = imageHeight*getScale();
//空白区域宽高
float fill_width = Main.screenWidth - width;
float fill_height = Main.screenHeight - height;
//需要移动的距离
float tran_width = 0f;
float tran_height = 0f;
if(fill_width>0)
tran_width = fill_width/2;
if(fill_height>0)
tran_height = fill_height/2;
postTranslate(tran_width, tran_height);
setImageMatrix(getImageViewMatrix());
}
protected float getValue(Matrix matrix, int whichValue) {
matrix.getValues(mMatrixValues);
mMinZoom =( Main.screenWidth/2f)/imageWidth;
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) * 4;
return max;
}
protected void zoomTo(float scale, float centerX, float centerY) {
if (scale > mMaxZoom) {
scale = mMaxZoom;
} else if (scale < mMinZoom) {
scale = mMinZoom;
}
float oldScale = getScale();
float deltaScale = scale / oldScale;
mSuppMatrix.postScale(deltaScale, deltaScale, centerX, centerY);
没有合适的资源?快使用搜索试试~ 我知道了~
Android界面] 如何实现滑动图片界面,双击图片放大缩小,多点缩放
共37个文件
class:15个
java:5个
jpg:4个
4星 · 超过85%的资源 需积分: 10 166 下载量 194 浏览量
2015-05-18
09:39:48
上传
评论
收藏 8.91MB ZIP 举报
温馨提示
免积分,Android界面] 如何实现滑动图片界面,双击图片放大缩小,多点缩放
资源推荐
资源详情
资源评论
收起资源包目录
GalleryDemo.zip (37个子文件)
GalleryDemo
.project 847B
proguard.cfg 1KB
src
com
havenliu
demo
GalleryAdapter.java 1KB
MyImageView.java 11KB
Main.java 5KB
MyGallery.java 6KB
AndroidManifest.xml 676B
res
drawable-ldpi
icon.png 2KB
drawable-hdpi
icon.png 4KB
drawable
chrysanthemum.jpg 859KB
desert.jpg 826KB
hydrangeas.jpg 581KB
penguins.jpg 760KB
drawable-mdpi
icon.png 3KB
layout
main.xml 424B
values
strings.xml 162B
assets
gen
com
havenliu
demo
R.java 956B
default.properties 362B
.classpath 280B
bin
resources.ap_ 2.97MB
GalleryDemo.apk 2.98MB
com
havenliu
demo
R$layout.class 391B
R$drawable.class 530B
R$attr.class 337B
Main.class 3KB
MyGallery.class 4KB
MyGallery$1.class 2KB
MyImageView$2.class 1KB
GalleryAdapter.class 2KB
MyImageView$1.class 1KB
MyGallery$MySimpleGesture.class 2KB
MyImageView.class 8KB
R$id.class 382B
R.class 524B
R$string.class 424B
Main$GalleryChangeListener.class 1KB
classes.dex 17KB
共 37 条
- 1
资源评论
- a乐呵呵2015-06-07很好用的资源
- waiting10162015-12-28听不错 用来交作业
- 爱吃豆芽菜的Helen2015-07-17很实用,很实用的资源啊
- 我才是点心2016-05-08一般般,还可以
- Lingli13905202015-09-07位置会跑偏,总体感觉还不错。
qq_18832749
- 粉丝: 1
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功