今天介绍一下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. Sqlserver_自定义函数操作

    use Test go if exists( SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'gettime') AND type in ...

  2. Gitweb 安装与配置

    gitweb 可以通过搭建git服务器将代码保存在git服务器上,多个开发者可以从服务器上clone代码,也可以各自维护一份本地代码,在本地更新之后可以提交到git服务器上,提高开发效率.     g ...

  3. MySQL之聚合

    EP:巧妙地过滤掉重复的日期 example:使用bit_or() bit_count()巧妙地过滤掉重复的日期: CREATE TABLE t1 ( year YEAR(4), month INT( ...

  4. visual studio 自带单元测试demo

    0) 创建类,编写方法类1) 在方法点击鼠标右键,在运行测试(T)和调试测试(D)之间会有一个 <创建单元测试>选项.有则进入2,没有则看1.01.0) 菜单栏 工具-->自定义-- ...

  5. Jsp-Servlet 那一大堆事儿--1

    为毛全局变量声明时初始化在try内不能用? import javax.servlet.http .*; import java.io.*; import javax.servlet.*; import ...

  6. 2014---多校训练2(ZCC Loves Codefires)

    ZCC Loves Codefires Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  7. ARM 汇编的一些规范

    A.5.1  文件格式        ARM 源程序文件(即源文件)为文件格式,可以使用任一文本编辑器编写程序代码.         在一个项目中,至少要有一个汇编源文件或C 程序文件,可以有多个汇编 ...

  8. (转载)Android理解:显式和隐式Intent

    Intent分两种:显式(Explicit intent)和隐式(Implicit intent). 一.显式(设置Component) 显式,即直接指定需要打开的activity对应的类. 以下多种 ...

  9. SQL Server数据库(高级查询)

    高级查询 1.连接查询 有外键关系的两张表,通过关系一次查询两张表 (1)select * from 表名,表名 --- 直接使用出现笛卡尔积,两个表数据一一对应,查询出的结果数目是两个表的行的乘积, ...

  10. OpenStack/Gnocchi简介——时间序列数据聚合操作提前计算并存储起来,先算后取的理念

    先看下 http://www.cnblogs.com/bonelee/p/6236962.html 这里对于环形数据库的介绍,便于理解归档这个操作! 转自:http://blog.sina.com.c ...