热门标签 | HotTags
当前位置:  开发笔记 > Android > 正文

android图片操作(缩放移动)实例代码

android图片操作(缩放移动)实例代码,需要的朋友可以参考一下

view_show.xml

代码如下:


  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_
  android:layout_>
   

GalleryViewTouch.java:

代码如下:

package com.kotei.lbs.Anthurium.LawCases;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.graphics.RectF;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.FloatMath;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

/**
 * 查看图片的操作
 * @author hongj
 */
public class GalleryViewTouch extends Activity  {
    private ImageView iv;
     private Bitmap bitmap=null;
        Matrix matrix = new Matrix();
        Matrix savedMatrix = new Matrix();
        DisplayMetrics dm;
        float minScaleR;// 最小缩放比例
        static final float MAX_SCALE = 4f;// 最大缩放比例
        static final int NOnE= 0;// 初始状态
        static final int DRAG = 1;// 拖动
        static final int ZOOM = 2;// 缩放
        int mode = NONE;
        PointF prev = new PointF();
        PointF mid = new PointF();
        float dist = 1f;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.view_show);
        dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);// 获取分辨率
        bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.bottom_layout_background);
        iv=(ImageView)findViewById(R.id.view_img);
        iv.setImageBitmap(bitmap);
        iv.setOnTouchListener(new ImageTouch(bitmap,dm.widthPixels,dm.heightPixels,iv));

    }

    class ImageTouch implements OnTouchListener {
        private ImageView iv;
         private Bitmap bitmap=null;
            Matrix matrix = new Matrix();
            Matrix savedMatrix = new Matrix();
            int dmwidth,dmheight;
            float minScaleR;// 最小缩放比例
            static final float MAX_SCALE = 4f;// 最大缩放比例
            static final int NOnE= 0;// 初始状态
            static final int DRAG = 1;// 拖动
            static final int ZOOM = 2;// 缩放
            int mode = NONE;
            PointF prev = new PointF();
            PointF mid = new PointF();
            float dist = 1f;
            ImageTouch(Bitmap bitmap,int width,int height,ImageView iv){
                this.bitmap= bitmap;
                this.dm| dmheight
                    minZoom();
                    center();
                    iv.setImageMatrix(matrix);
                } else {
                    iv.setScaleType(ScaleType.CENTER);
                }
            }
        @Override

        public boolean onTouch(View v, MotionEvent event) {
             switch (event.getAction() & MotionEvent.ACTION_MASK) {
                // 主点按下
                case MotionEvent.ACTION_DOWN:
                    Log.d("System.out", "ACTION_DOWN");
                    savedMatrix.set(matrix);
                    prev.set(event.getX(), event.getY());
                    mode = DRAG;
                    break; // 副点按下
                case MotionEvent.ACTION_POINTER_DOWN:
                    Log.d("System.out", "ACTION_POINTER_DOWN");
                    dist = spacing(event); // 如果连续两点距离大于10,则判定为多点模式
                    if (spacing(event) > 10f) {
                        savedMatrix.set(matrix);
                        midPoint(mid, event);
                        mode = ZOOM;
                    }
                    break;
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_POINTER_UP:
                    mode = NONE;
                    break;
                case MotionEvent.ACTION_MOVE:
                    if (mode == DRAG) {
                        matrix.set(savedMatrix);
                        matrix.postTranslate(event.getX() - prev.x, event.getY()
                                - prev.y);

                    } else if (mode == ZOOM) {
                        float newDist = spacing(event);
                        if (newDist > 10f) {
                            matrix.set(savedMatrix);
                            float tScale = newDist / dist;
                            matrix.postScale(tScale, tScale, mid.x, mid.y);
                        }
                    }
                    break;
                }

                iv.setImageMatrix(matrix);
                if(mode!=NONE)
               CheckView();

                return true;
        }
         private void CheckView() {
                float p[] = new float[9];
                matrix.getValues(p);
                if (mode == ZOOM) {
                    if (p[0]                         matrix.setScale(minScaleR, minScaleR);
                    }
                    if (p[0] > MAX_SCALE) {
                        matrix.set(savedMatrix);
                    }
                }
                center();
            }

         public void minZoom() {
                Log.i("test", bitmap.getWidth()+"");
                Log.i("test", bitmap.getHeight()+"");
                minScaleR = Math.min(
                        (float) dmwidth / (float) bitmap.getWidth(),
                        (float) dmheight / (float) bitmap.getHeight());
                if (minScaleR <1.0) {
                    matrix.postScale(minScaleR, minScaleR);
                }
            }
          protected void center(boolean horizontal, boolean vertical) {
                Matrix m = new Matrix();
                m.set(matrix);
                RectF rect = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight());
                m.mapRect(rect);
                float height = rect.height();
                float width = rect.width();
                Log.d("System.out",rect.top+"************"+ rect.bottom);
                float deltaX = 0, deltaY = 0;
                if (vertical) { // 图片小于屏幕大小,则居中显示。大于屏幕,上方留空则往上移,下方留空则往下移

                    int screenHeight = dmheight;
                    if (height                         deltaY = (screenHeight - height) / 2 - rect.top;
                    } else if (rect.top > 0) {
                        deltaY = -rect.top;
                    } else if (rect.bottom                         deltaY = iv.getHeight() - rect.bottom;
                    }
                    //deltaY-=50;
                }
                if (horizontal) {
                    int screenWidth = dmwidth;
                    if (width                         deltaX = (screenWidth - width) / 2 - rect.left;
                    } else if (rect.left > 0) {
                        deltaX = -rect.left;
                    } else if (rect.right                         deltaX = screenWidth - rect.right;
                    }
                }
                matrix.postTranslate(deltaX, deltaY);
            }
         public void center() {
                center(true, true);
            }
         private float spacing(MotionEvent event) {
                float x = event.getX(0) - event.getX(1);
                float y = event.getY(0) - event.getY(1);
                return FloatMath.sqrt(x * x + y * y);
            }

            /** * 两点的中点 */
            private void midPoint(PointF point, MotionEvent event) {
                float x = event.getX(0) + event.getX(1);
                float y = event.getY(0) + event.getY(1);
                point.set(x / 2, y / 2);
            }
    }
}


推荐阅读
author-avatar
林yong珊_450
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有