10_多点触摸交互处理

  多点触摸,对图像进行缩放的实例。

10_多点触摸交互处理

 1 package com.example.multitouch;

 2 

 3 import android.app.Activity;

 4 import android.os.Bundle;

 5 import android.view.MotionEvent;

 6 import android.view.View;

 7 import android.view.WindowManager;

 8 import android.widget.FrameLayout;

 9 import android.widget.FrameLayout.LayoutParams;

10 import android.widget.ImageView;

11 

12 public class MainActivity extends Activity {

13 

14     private FrameLayout root;

15     private ImageView iv;

16 

17     @Override

18     protected void onCreate(Bundle savedInstanceState) {

19         super.onCreate(savedInstanceState);

20         setContentView(R.layout.activity_main);

21 

22         root = (FrameLayout) findViewById(R.id.container);

23         iv = (ImageView) findViewById(R.id.iv);

24 

25         root.setOnTouchListener(new View.OnTouchListener() {

26 

27             float currentDistance;

28             float lastDistance = -1;

29 

30             FrameLayout.LayoutParams lp = (LayoutParams) iv.getLayoutParams();

31 

32             @Override

33             public boolean onTouch(View v, MotionEvent event) {

34 

35                 switch (event.getAction()) {

36                 case MotionEvent.ACTION_DOWN:

37                     System.out.println("action_down");

38                     break;

39                 case MotionEvent.ACTION_MOVE:

40                     System.out.println("action_move");

41 

42                     if (event.getPointerCount() >= 2) {

43 

44                         float offsetX = event.getX(0) - event.getX(1);

45                         float offsetY = event.getY(0) - event.getY(1);

46                         currentDistance = (float) Math.sqrt(offsetX * offsetX

47                                 + offsetY * offsetY);

48 

49                         if (lastDistance < 0) {

50                             lastDistance = currentDistance;

51                         } else {

52                             if (currentDistance - lastDistance > 5) {

53                                 System.out.println("放大");

54 

55                                 lp.width = (int) (1.1f * iv.getWidth());

56                                 lp.height = (int) (1.1f * iv.getHeight());

57 

58                                 iv.setLayoutParams(lp);

59 

60                                 lastDistance = currentDistance;

61                             } else if (lastDistance - currentDistance > 5) {

62                                 System.out.println("缩小");

63                                 lp.width = (int) (0.9f * iv.getWidth());

64                                 lp.height = (int) (0.9f * iv.getHeight());

65                                 iv.setLayoutParams(lp);

66                                 lastDistance = currentDistance;

67                             }

68                         }

69                     }

70 

71                     // 获取到多个触摸点

72                     // System.out.println("pointer count : " +

73                     // event.getPointerCount() );

74                     // System.out.println(String.format("x1:%f y1:%f",

75                     // event.getX(0),event.getY(0)));

76 

77                     // FrameLayout.LayoutParams lp = (LayoutParams)

78                     // iv.getLayoutParams();

79                     // lp.leftMargin = (int) event.getX();

80                     // lp.topMargin = (int) event.getY();

81                     //

82                     // iv.setLayoutParams(lp);

83                     //

84                     // System.out.println(String.format("x:%f,y:%f",

85                     // event.getX(), event.getY() ));

86                     break;

87                 case MotionEvent.ACTION_UP:

88                     System.out.println("action_up");

89                     break;

90                 }

91 

92                 // 返回true的话,事件才会继续被触发。

93                 return true;

94             }

95         });

96     }

97 }
MainActivity
 1 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

 2     xmlns:tools="http://schemas.android.com/tools"

 3     android:id="@+id/container"

 4     android:layout_width="match_parent"

 5     android:layout_height="match_parent"

 6     tools:context="com.example.multitouch.MainActivity"

 7     tools:ignore="MergeRootFrame" >

 8 

 9     <ImageView

10         android:id="@+id/iv"

11         android:layout_width="wrap_content"

12         android:layout_height="wrap_content"

13         android:src="@drawable/ic_launcher" />

14 

15 </FrameLayout>
activity_main.xml
 1 <?xml version="1.0" encoding="utf-8"?>

 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"

 3     package="com.example.multitouch"

 4     android:versionCode="1"

 5     android:versionName="1.0" >

 6 

 7     <uses-sdk

 8         android:minSdkVersion="14"

 9         android:targetSdkVersion="19" />

10 

11     <application

12         android:allowBackup="true"

13         android:icon="@drawable/ic_launcher"

14         android:label="@string/app_name"

15         android:theme="@style/AppTheme" >

16         <activity

17             android:name="com.example.multitouch.MainActivity"

18             android:label="@string/app_name" >

19             <intent-filter>

20                 <action android:name="android.intent.action.MAIN" />

21 

22                 <category android:name="android.intent.category.LAUNCHER" />

23             </intent-filter>

24         </activity>

25     </application>

26 

27 </manifest>
AndroidManifest.xml

 

你可能感兴趣的:(处理)