Android 手势放大与缩小

来源:互联网 发布:数据库的完整性是指 编辑:程序博客网 时间:2024/06/07 03:31
public class MainActivity extends Activity {    FrameLayout frameLayout;    private ImageView imgview;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.faceactivity);        frameLayout=(FrameLayout) findViewById(R.id.mathtiem);        imgview=(ImageView) findViewById(R.id.imgview);        frameLayout.setOnTouchListener(new OnTouchListener() {            float currendistance;            float lastdistance=-1;            @Override            public boolean onTouch(View v, MotionEvent event) {                // TODO Auto-generated method stub                switch (event.getAction()) {                case MotionEvent.ACTION_DOWN:                    System.out.println("ACTION_DOWN");                    break;                    //手指一动                case MotionEvent.ACTION_MOVE:                    if(event.getPointerCount()>=2){                    float offsetx=event.getX(0)-event.getX(1);                    float offsety=event.getY(0)-event.getY(1);                    currendistance=(float) Math.sqrt(offsetx*offsetx+offsety*offsety);                    if(lastdistance<0){                        lastdistance=currendistance;                    }else{                        if(currendistance-lastdistance<5){                            FrameLayout.LayoutParams lp=(LayoutParams) imgview.getLayoutParams();                            lp.width=(int) (1.1f*imgview.getWidth());                            lp.height=(int) (1.1f*imgview.getHeight());                            imgview.setLayoutParams(lp);                            System.out.println("放大");                            lastdistance=currendistance;                        }else if(lastdistance-currendistance>5){                            FrameLayout.LayoutParams lp=(LayoutParams) imgview.getLayoutParams();                            lp.width=(int) (0.9f*imgview.getWidth());                            lp.height=(int) (0.9f*imgview.getHeight());                            imgview.setLayoutParams(lp);                            System.out.println("缩小");                            lastdistance=currendistance;                        }                    }                    }//                  System.out.println("countet"+event.getPointerCount());//                  System.out.println(String.format("x1:%f y1:%f   x2:%f y2:%f", event.getX(0),event.getY(0),event.getX(1),event.getY(1)));//                  System.out.println("ACTION_MOVE");//                  FrameLayout.LayoutParams lp=(LayoutParams) imgview.getLayoutParams();//                  lp.leftMargin=(int) event.getX();//                  lp.topMargin=(int) event.getY();//                  imgview.setLayoutParams(lp);//                  System.out.println(">>>>>>>>>>"+String.format("x:%f,y:%f", event.getX(),event.getY()));                    break;                case MotionEvent.ACTION_UP:                    System.out.println("ACTION_UP");                    break;                default:                    break;                }                return true;            }        });    }///布局文件<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:layout_width="match_parent"    android:layout_height="match_parent"    android:id="@+id/mathtiem"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin">    <ImageView         android:id="@+id/imgview"        android:layout_gravity="center"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/ic_launcher"/></FrameLayout>
0 0