• 图片的拖动与释放


    原理图:

    1)


    2)



    代码如下:

    1、在drawable目录下导入要演示的图片


    2、main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
        
        <ImageView 
            android:id="@+id/iv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="matrix"
            android:src="@drawable/a"
            />
    
    </LinearLayout>
    


    3、MainActivity

    package com.njupt.drag_scale1;
    
    import android.app.Activity;
    import android.graphics.Matrix;
    import android.graphics.PointF;
    import android.os.Bundle;
    import android.util.FloatMath;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.View.OnTouchListener;
    import android.widget.ImageView;
    
    public class MainActivity extends Activity {
        /** Called when the activity is first created. */
    	
    	private ImageView iv;
    	
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            
            iv = (ImageView) findViewById(R.id.iv);
            
            iv.setOnTouchListener(new MyOnTouchListener());
        }
        
        private final class MyOnTouchListener implements OnTouchListener{
        	
        	private PointF startPoint = new PointF();//初始点
        	private Matrix mCurrentMatrix = new Matrix();//图片初始的matrix值
        	private Matrix mMatrix = new Matrix();//来一个临时的matrix
        	
        	private PointF midPointF;//中心点
        	private float midDistance;//两点间的距离
        	
        	private int type = 0;
        	private final static int DRAG = 1;
        	private final static int SCALE = 2;
            
    		public boolean onTouch(View v, MotionEvent event) {
    			// TODO Auto-generated method stub
    			int action = event.getAction()&MotionEvent.ACTION_MASK;
    			switch (action) {
    			case MotionEvent.ACTION_DOWN://手指按下
    				float x = event.getX();
    				float y = event.getY();
    				startPoint.set(x, y);
    				mCurrentMatrix.set(iv.getImageMatrix());//图片没有移动的位置
    				type = DRAG;
    				break;
    			case MotionEvent.ACTION_POINTER_1_DOWN://屏幕上有一根手指,再按下一根手指
    				mCurrentMatrix.set(iv.getImageMatrix());//图片没有缩放的大小
    				midPointF = getMidPointF(event);
    				midDistance = getMidDistance(event);
    				type = SCALE;
    				break;
    			case MotionEvent.ACTION_MOVE://手指移动
    				if(type == DRAG){
    					mMatrix.set(mCurrentMatrix);
    					float dx = event.getX() - startPoint.x;
    					float dy = event.getY() - startPoint.y;
    					mMatrix.postTranslate(dx, dy);
    				}else if(type == SCALE){
    					mMatrix.set(mCurrentMatrix);
    					float distance = getMidDistance(event);//得到当前两个手指间的距离
    					float sx = distance/midDistance;//得到缩放的倍数
    					mMatrix.postScale(sx, sx, midPointF.x, midPointF.y);
    				}
    				break;
    			case MotionEvent.ACTION_UP://手指弹起
    			case MotionEvent.ACTION_POINTER_1_UP://弹起一根手指,屏幕上还有一根手指
    				type = 0;
    				break;
    
    			default:
    				break;
    			}
    			
    			iv.setImageMatrix(mMatrix);
    			return true;
    		}
    		
    
        	
        }
        
        //得到中心点
    	private PointF getMidPointF(MotionEvent event){
    		float x = (event.getX(1) + event.getX(0))/2;
    		float y = (event.getY(1) + event.getY(0))/2;
    		return new PointF(x, y);
    	}
    	
    	//计算两点间的距离
    	private float getMidDistance(MotionEvent event){
    		float dx = event.getX(1) - event.getX(0);
    		float dy = event.getY(1) - event.getY(0);
    		return FloatMath.sqrt(dx*dx + dy*dy);
    	}
    }


  • 相关阅读:
    Spring事务内方法调用自身事务 增强的三种方式
    SpringBoot优化内嵌的Tomcat
    Tomcat 8.0的并发优化
    Swift搭建本地http服务器,实现外部视频即时播放
    更新ruby:Error running 'requirements_osx_brew_update_system ruby-2.4.1报错解决
    iOS关于沙盒文件拷贝manager.copyItem的一个坑
    Swift udp实现根据端口号监听广播数据(利用GCDAsyncUdpSocket实现)
    iOS刻度尺换算之1mm等于多少像素理解
    Swift下的基于UIView的位置属性扩展
    iOS Main Thread Checker: UI API called on a background thread的解释
  • 原文地址:https://www.cnblogs.com/suncoolcat/p/3331237.html
Copyright © 2020-2023  润新知