zoukankan      html  css  js  c++  java
  • 图片的拖动与释放

    原理图:

    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);
    	}
    }


  • 相关阅读:
    compiere简易介绍及个人看法
    js数字金额转换为英文金额(数字的中英文转化) 修正版
    eval和document.getElementById
    关于netsuite编辑单据页面默认打开某tab的办法
    js 抛出异常 throw
    Training Agenda netsuite
    js 多维数组 应用
    AJAX提交数据时 中文处理 以及js url 中文处理
    监视所有 HTTP 请求和响应的工具 Fiddler工具介绍 (转载)
    关于netsuite创建组合按钮的方法,合并按钮,netsuite API
  • 原文地址:https://www.cnblogs.com/suncoolcat/p/3331237.html
Copyright © 2011-2022 走看看