zoukankan      html  css  js  c++  java
  • ImageView 缩放

     <ImageView
                    android:id="@+id/imageview"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"                
                    android:src="@drawable/zixun"
                    android:scaleType="matrix"
                    />
    
    
    关键点:缩放的内容不能是图片的背景,一定要是图片的内容
    复制代码
    复制代码
    public class HelpActivity extends Activity
    {
    
        private static final int NONE = 0;
        private static final int DRAG = 1;
        private static final int ZOOM = 2;
    
        private int mode = NONE;
        private float oldDist;
        private Matrix matrix = new Matrix();
        private Matrix savedMatrix = new Matrix();
        private PointF start = new PointF();
        private PointF mid = new PointF();
    
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.help);
            ImageView view = (ImageView) findViewById(R.id.imageview);
    //        view.setImageResource(R.drawable.icon);
            view.setOnTouchListener(mOnTouchListener);
        }
    
        OnTouchListener mOnTouchListener = new OnTouchListener()
        {
            public boolean onTouch(View v, MotionEvent event)
            {
                Trace.Log("------------------"+event.toString());
                ImageView view = (ImageView) v;
                switch (event.getAction() & MotionEvent.ACTION_MASK)
                {
                case MotionEvent.ACTION_DOWN:
                    savedMatrix.set(matrix);
                    start.set(event.getX(), event.getY());
                    mode = DRAG;
                    break;
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_POINTER_UP:
                    mode = NONE;
                    break;
                // 多点触控
                case MotionEvent.ACTION_POINTER_DOWN:
                    oldDist = spacing(event);
                    if (oldDist > 10f)
                    {
                        savedMatrix.set(matrix);
                        midPoint(mid, event);
                        mode = ZOOM;
                    }
                    break;
                case MotionEvent.ACTION_MOVE:
                    if (mode == DRAG)
                    {
                        matrix.set(savedMatrix);
                        matrix.postTranslate(event.getX() - start.x, event.getY() - start.y);
                    }
                    else if (mode == ZOOM)
                    {
                        float newDist = spacing(event);
                        if (newDist > 10f)
                        {
                            matrix.set(savedMatrix);
                            float scale = newDist / oldDist;
                            matrix.postScale(scale, scale, mid.x, mid.y);
                            Trace.Log("------------------"+newDist);
                        }
                    }
                    break;
                }
    
                view.setImageMatrix(matrix);
    //            view.invalidate();
                return 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);
        }
    
        public void onClickBack(View view)
        {
            backToCardSelect();
        }    
    }

    ImageVIew 需要在最上面一层,否则可能不会生效
    复制代码
  • 相关阅读:
    MySQL数据库优化详解(收藏)
    怎么设置Linux swap分区?方法教程
    js获取IP地址多种方法实例教程
    JQuery设置获取下拉菜单选项的值 多实例
    JQuery中serialize()、serializeArray()和param()用法举例
    javascript 获取函数形参个数
    mysql SQLyog导入csv数据失败怎么办?
    今天离职了!
    Asp.Net Core 使用Quartz基于界面画接口管理做定时任务
    Asp.Net Core中使用Swagger,你不得不踩的坑
  • 原文地址:https://www.cnblogs.com/exmyth/p/4490826.html
Copyright © 2011-2022 走看看