Android多点触摸交互处理-图片缩放

 

创建一个新工程LearnMultipointTouich一个空的Activity,方式一个ImageView和一个Checkbox控件

实现目标:如果Checkbox打勾这移动图片;否则缩放图片。

实现原理:1、根据event.getPointerCount获取手机上触摸点个数
2:两个触摸点之间的距离  利用勾股定理可以算出两个触摸点之间的距离
3:onTouch事件的返回时差异:默认是false,即处理第一个事件后,后续事件不处理,如果要触发后续事件应该return true
 Android多点触摸交互处理-图片缩放_第1张图片

MainActivity.java代码

public class MainActivity extends AppCompatActivity {
    private LinearLayout root;
    private CheckBox cbMove;
    ImageView iv;
    //两个点之间的x坐标差的平方+y坐标差的平方 然后求其平方根即时两个点之间的距离
    float lastCurrentDistance=-1;//两个触摸点之间的最后距离     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        cbMove= (CheckBox) findViewById(R.id.cbMove);
        iv= (ImageView) findViewById(R.id.imageView);
        root= (LinearLayout) findViewById(R.id.fmContainer);
        root.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){  //通过event判断此时的触摸类型
                    case MotionEvent.ACTION_DOWN://触摸按下事件                         break;
                    case MotionEvent.ACTION_MOVE://触摸移动事件                         if (cbMove.isChecked()==true){
                            goMoveImg(v,event);
                        }else{
                            goBlowUpShrinkImg(v, event);
                        }
                        break;
                    case MotionEvent.ACTION_UP://触摸弹起事件                     default:
                        break;
  
                }
                return true//默认是false,即处理第一个事件后,后续事件不处理,如果要触发后续事件应该return true
            }
        });
    }
  
    private void goBlowUpShrinkImg(View v, MotionEvent event) {
        float currentDistance;//两个触摸点之间的距离  利用勾股定理可以算出两个触摸点之间的距离
        if (event.getPointerCount()>=2){  ////获取触摸点的个数据说最多可以获取10个触摸点
            float offsetX=event.getX(0)-event.getX(1);
            float offsetY=event.getY(0)-event.getY(1);
            currentDistance= (float) Math.sqrt(offsetX*offsetX+offsetY*offsetY);
            if (lastCurrentDistance<0){
                lastCurrentDistance=currentDistance;
            } else{
                //由于android的手指触摸会存在偏差,我们取值为8进行判断放大或缩小操作
                if (currentDistance-lastCurrentDistance>8){
                    System.out.println("放大操作");
                    LinearLayout.LayoutParams lp= (LinearLayout.LayoutParams) iv.getLayoutParams();
                    lp.width= (int) (1.1f*iv.getWidth());
                    lp.height= (int) (1.1f*iv.getHeight());
                    iv.setLayoutParams(lp);
                    lastCurrentDistance=currentDistance;
                }else if (lastCurrentDistance-currentDistance>8){
                    System.out.println("缩小操作");
                    LinearLayout.LayoutParams lp= (LinearLayout.LayoutParams) iv.getLayoutParams();
                    lp.width= (int) (0.9f*iv.getWidth());
                    lp.height= (int) (0.9f*iv.getHeight());
                    iv.setLayoutParams(lp);
                    lastCurrentDistance=currentDistance;
                }
            }
        }
    }
  
    private void goMoveImg(View v, MotionEvent event) {
        //获取触摸点坐标         System.out.println(String.format("触摸点坐标x:%f,y:%f",event.getX(),event.getY()));
        LinearLayout.LayoutParams lp= (LinearLayout.LayoutParams) iv.getLayoutParams();
        lp.leftMargin= (int) event.getX();
        lp.topMargin= (int) event.getY();
        iv.setLayoutParams(lp);  //重新设置图片的左上角坐标为触摸坐标     }
  
  
}

 

你可能感兴趣的:(android,多点触摸,拖动,图片缩放)