今天介绍一下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. hiho_100_八数码

    题目大意 8数码问题,在3x3的矩阵中填入0-8九个数字,0可以和它相邻的数字进行交换.从初始状态到达状态F(3x3的方格从上到下,从左到右,形成的数字串为123456780)所需要最少移动的次数. ...

  2. iOS开发 UIPanGestureRecognizer手势抽象类

    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@sel ...

  3. foreach 相关

    20 Nov 08 深入理解PHP原理之foreach 作者: Laruence(   ) 本文地址: http://www.laruence.com/2008/11/20/630.html 转载请注 ...

  4. Rest-Assured

    Rest-Assured完整的测试例子 http://blog.csdn.net/win7system/article/details/52468078 使用 Rest-assured 测试 Rest ...

  5. ListView 搭配SimpleAdapter

    这是SimplerAdapter的构造函数 public SimpleAdapter(Context context, List<? extends Map<String, ?>&g ...

  6. entity refenrece 在views中的运用

    在一个content type中有一个field是entity reference, 那么这个字段的设置过程中会指定一个entity type和content type和一个具体内容的选择器, 然后到 ...

  7. assert的用处

    ASSERT函数是用于调试中,也就是说在你的代码中当是Debug的时候它完成对参数的判断,如果是TRUE则什么都不做,如果是FALSE则弹出一个程序中断对话框提示程序出现错误.在Release版本中它 ...

  8. 和为S的两个数VS和为S的连续正数序列

    其实这个题目如果没有限制时间复杂度的话,那么就很简单了,一遍一遍地扫描吧.时间复杂度肯定就是 O(n2)啰.但是这题目肯定不会这么简单,否则就是小学生的水平了嘛. 其实我刚到这题的时候想到的是用二叉查 ...

  9. 如何在腾讯云快速构建一个Wordpress个人站点

    版权声明:本文由贺嘉原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/175 来源:腾云阁 https://www.qclou ...

  10. java 集合(Collection 和 Array)

    Collection(是一个单列集合的根接口) Collections(操作集合对象的一个工具类)只要了解部分常用的方法就好