今天介绍一下Android中怎么实现ImageView的缩放和移动,自定义TouchImageView。

 public class TouchImageView extends ImageView {

     Matrix matrix;

     // We can be in one of these 3 states
static final int NONE = ;
static final int DRAG = ;
static final int ZOOM = ;
int mode = NONE; // Remember some things for zooming
PointF last = new PointF();
PointF start = new PointF();
float minScale = 1f;
float maxScale = 3f;
float[] m; int viewWidth, viewHeight;
static final int CLICK = ;
float saveScale = 1f;
protected float origWidth, origHeight;
int oldMeasuredWidth, oldMeasuredHeight; 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;
mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
matrix = new Matrix();
m = new float[];
setImageMatrix(matrix);
setScaleType(ScaleType.MATRIX); setOnTouchListener(new OnTouchListener() { @Override
public boolean onTouch(View v, MotionEvent event) {
mScaleDetector.onTouchEvent(event);
PointF curr = new PointF(event.getX(), event.getY()); switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
last.set(curr);
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 fixTransX = getFixDragTrans(deltaX, viewWidth, origWidth * saveScale);
float fixTransY = getFixDragTrans(deltaY, viewHeight, origHeight * saveScale);
matrix.postTranslate(fixTransX, fixTransY);
fixTrans();
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;
} setImageMatrix(matrix);
invalidate();
return true; // indicate event was handled
} });
} public void setMaxZoom(float x) {
maxScale = x;
} 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;
} if (origWidth * saveScale <= viewWidth || origHeight * saveScale <= viewHeight)
matrix.postScale(mScaleFactor, mScaleFactor, viewWidth / , viewHeight / );
else
matrix.postScale(mScaleFactor, mScaleFactor, detector.getFocusX(), detector.getFocusY()); fixTrans();
return true;
}
} void fixTrans() {
matrix.getValues(m);
float transX = m[Matrix.MTRANS_X];
float transY = m[Matrix.MTRANS_Y]; float fixTransX = getFixTrans(transX, viewWidth, origWidth * saveScale);
float fixTransY = getFixTrans(transY, viewHeight, origHeight * saveScale); if (fixTransX != || fixTransY != )
matrix.postTranslate(fixTransX, fixTransY);
} float getFixTrans(float trans, float viewSize, float contentSize) {
float minTrans, maxTrans; if (contentSize <= viewSize) {
minTrans = ;
maxTrans = viewSize - contentSize;
} else {
minTrans = viewSize - contentSize;
maxTrans = ;
} if (trans < minTrans)
return -trans + minTrans;
if (trans > maxTrans)
return -trans + maxTrans;
return ;
} float getFixDragTrans(float delta, float viewSize, float contentSize) {
if (contentSize <= viewSize) {
return ;
}
return delta;
} @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
viewWidth = MeasureSpec.getSize(widthMeasureSpec);
viewHeight = MeasureSpec.getSize(heightMeasureSpec); //
// Rescales image on rotation
//
if (oldMeasuredHeight == viewWidth && oldMeasuredHeight == viewHeight
|| viewWidth == || viewHeight == )
return;
oldMeasuredHeight = viewHeight;
oldMeasuredWidth = viewWidth; if (saveScale == ) {
//Fit to screen.
float scale; Drawable drawable = getDrawable();
if (drawable == null || drawable.getIntrinsicWidth() == || drawable.getIntrinsicHeight() == )
return;
int bmWidth = drawable.getIntrinsicWidth();
int bmHeight = drawable.getIntrinsicHeight(); Log.d("bmSize", "bmWidth: " + bmWidth + " bmHeight : " + bmHeight); float scaleX = (float) viewWidth / (float) bmWidth;
float scaleY = (float) viewHeight / (float) bmHeight;
scale = Math.min(scaleX, scaleY);
matrix.setScale(scale, scale); // Center the image
float redundantYSpace = (float) viewHeight - (scale * (float) bmHeight);
float redundantXSpace = (float) viewWidth - (scale * (float) bmWidth);
redundantYSpace /= (float) ;
redundantXSpace /= (float) ; matrix.postTranslate(redundantXSpace, redundantYSpace); origWidth = viewWidth - * redundantXSpace;
origHeight = viewHeight - * redundantYSpace;
setImageMatrix(matrix);
}
fixTrans();
}
}

Android 移动缩放的ImageView的更多相关文章

  1. android学习笔记之ImageView的scaleType属性

    我们知道,ImageView有一个属性叫做scaleType,它的取值一共有八种,分别是:matrix,fitXY,fitStart,fitCenter,fitEnd,center,centerCro ...

  2. Android开源项目发现---ImageView 篇(持续更新)

    1. PhotoView 支持双击或双指缩放的ImageView 在ViewPager等Scrolling view中正常使用,相比上面的AndroidTouchGallery,不仅支持ViewPag ...

  3. 一起学习android图像缩放资源 (27)

    效果图: 在平时载入图片时,我会使用SetImageBitmap.setImageResource.BitmapFactory.decodeResource来设置一张图 片通过以上方法来设置图片时.会 ...

  4. Android基础控件ImageView的使用

    1.相关属性 <!--src 设置内容--> <!--background 设置背景--> <!--alpha 设置透明度 --> <!--adjustVie ...

  5. Android控件之ImageView(显示图片的控件)

    一.ImageView属性: android:src = "@drawable/ic_launcher"——ImageView的内容图像(可以和android:background ...

  6. android ImageView的属性android:scaleType,即ImageView.setScaleType(ImageView.ScaleType)

    实例 <ImageView android:id="@+id/image" android:layout_width="fill_parent" andr ...

  7. ImageView的属性android:scaleType,即ImageView.setScaleType(ImageView.ScaleType)

    1 imageView.setScaleType(ImageView.ScaleType.FIT_XY ); 1 这里我们重点理解ImageView的属性android:scaleType,即Imag ...

  8. android中动态修改ImageView控件的宽高度

    本例实现了动态修改ImageView控件的宽高度,有两个按钮,一个按钮实现放大image,一个按钮实现缩小image activity_main.xml <?xml version=" ...

  9. android开发布局文件imageview 图片等比例缩放:

    ImageView的属性scaleType,如果等比缩放的话,就使用CenterInside,如果想固定大小的话,就CenterCrop <?xml version="1.0" ...

随机推荐

  1. D3.js 简介和安装

    一.What´s D3.js D3.js是一种数据操作类型的javascript库(也可视其为插件):结合HTML,SVG和CSS,D3可以图形化的,生动的展现数据. D3 的全称是(Data-Dri ...

  2. activiti jbpm相关资源

    Activiti 5.16 用户手册 http://www.mossle.com/docs/activiti/index.html jBPM 4.4开发指南 http://www.mossle.com ...

  3. CSS3学习笔记之属性值

    font-family 设置文本的字体名称. font-style 设置文本样式. 取值 normal不使用斜体. italic使用斜体. oblique使用倾斜体. inherit从父元素继承. f ...

  4. JavaScript高阶函数的应用

    定义 高阶函数是指至少满足下列条件之一的函数: 函数可以作为参数被传递: 函数可以作为返回值输出. JavaScript语言中的函数显然满足高阶函数的条件,在实际开发中,无论是将函数当作参数传递,还是 ...

  5. sql CHARINDEX函数

    CHARINDEX函数返回字符或者字符串在另一个字符串中的起始位置.CHARINDEX函数调用方法如下: CHARINDEX ( expression1 , expression2 [ , start ...

  6. 【CITE】利用鼠标绘图C#

    实例018 利用鼠标绘图 光盘位置:光盘\MR\01\018 在常用的画图软件中,用户一般都可以通过鼠标在其中绘图,那么该功能是如何实现的呢?本实例将讲解如何使用C#实现通过拖动鼠标在窗体上绘图的功能 ...

  7. 根据不同的屏幕宽度引入不同的css文件

    <link rel="stylesheet" href="css/jl_public.css?v=11"/> <link rel=" ...

  8. spring来了-01-概述

    思考 对象创建能否写死? 对象创建细节 对象数量 action       多个    [需要维护成员变量] service      一个    [不需要维护成员变量] dao           ...

  9. [saiku] 简化/汉化/设置默认页

    上一篇分析了schema文件 [ http://www.cnblogs.com/avivaye/p/4877832.html] 在安装完毕Saiku后,由于是社区版本,所以界面上存在很多升级为商业版的 ...

  10. python 第三方模块 转 https://github.com/masterpy/zwpy_lst

    Chardet,字符编码探测器,可以自动检测文本.网页.xml的编码. colorama,主要用来给文本添加各种颜色,并且非常简单易用. Prettytable,主要用于在终端或浏览器端构建格式化的输 ...