android——单点触控移动,多点触控放大缩小

xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/imgage"
        android:src="@drawable/test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
       />

</RelativeLayout>

java

package com.example.touchdemo;

import android.os.Bundle;
import android.app.Activity;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MainActivity extends Activity {
    private ImageView image;
    float lastDistance = -1;
    float x0 = 0;
    float y0 = 0;
     private int screenWidth; 
       private int screenHeight; 
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        image = (ImageView) findViewById(R.id.imgage);
        //获得屏幕宽高 不让出界
         DisplayMetrics dm = getResources().getDisplayMetrics(); 
            screenWidth = dm.widthPixels; 
            screenHeight = dm.heightPixels-100; 
image.setOnTouchListener(
new OnTouchListener() { private float x; private float y; @Override public boolean onTouch(View v,final MotionEvent event) { // TODO Auto-generated method stub int action = event.getAction(); Log.i("TAG","触摸开始......"); switch (action) { case MotionEvent.ACTION_DOWN: //getX()是表示Widget相对于自身左上角的x坐标,而getRawX()是表示相对于屏幕左上角的x坐标值(注意:这个屏幕左上角是手机屏幕左上角,不管activity是否有titleBar或是否全屏幕) x = event.getRawX(); y = event.getRawX(); break; case MotionEvent.ACTION_MOVE: //得到触摸点的个数 int count = event.getPointerCount(); Log.i("TAG","触摸点的个数:"+count); if(count > 1){ Log.i("TAG", event.getX(0)+","+event.getY(0)+"|"+event.getX(1)+","+event.getY(1)); //获得两点的坐标差 float distanceX = event.getX(0) - event.getX(1); float distanceY = event.getY(0) - event.getY(1); //获得两点之间的距离 float betweenDistance = (float) Math.sqrt(distanceX*distanceX + distanceY*distanceY); Log.i("TAG","当前两点之间的新距离:"+betweenDistance); if(betweenDistance < 1){ lastDistance = betweenDistance; }else if((betweenDistance - lastDistance) > 5){ //放大 lastDistance = betweenDistance; //获得布局参数 RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams) image.getLayoutParams(); params.width = (int) (image.getWidth()*1.1f); params.height = (int) (image.getHeight()*1.1f); image.setLayoutParams(params); }else if((lastDistance - betweenDistance) > 5){ lastDistance = betweenDistance; //获得布局参数 RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams) image.getLayoutParams(); params.width = (int) (image.getWidth()*0.9f); params.height = (int) (image.getHeight()*0.9f); image.setLayoutParams(params); } }else if(count == 1){ //移动距离 float rawX = event.getRawX()-x; float rawy = event.getRawY()-y; //定义新的 int left = (int) (image.getLeft()+rawX); int top = (int) (image.getTop()+rawy); int right = (int) (image.getRight()+rawX); int bottom = (int) (image.getBottom()+rawy); // 设置不能出界 if (left < 0) { left = 0; right = left + v.getWidth(); } if (right > screenWidth) { right = screenWidth; left = right - v.getWidth(); } if (top < 0) { top = 0; bottom = top + v.getHeight(); } if (bottom > screenHeight) { bottom = screenHeight; top = bottom - v.getHeight(); } //赋值 image.layout(left, top, right, bottom); //改成新的按下作标 x=event.getRawX(); y=event.getRawY(); /*return false;*/ } break; } return true; } }); } //全局移动 @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub /*image.setX(event.getX()); image.setY(event.getY()); */ return true; } }

 

你可能感兴趣的:(android——单点触控移动,多点触控放大缩小)