详细解决方案
Android透过手势实现的缩放处理
热度:29 发布时间:2016-05-01 19:00:42.0
Android通过手势实现的缩放处理
01 import android.app.Activity; |
02 | import android.content.Context; |
03 | import android.graphics.Canvas; |
04 | import android.graphics.Color; |
05 | import android.graphics.Paint; |
06 | import android.os.Bundle; |
07 | import android.util.DisplayMetrics; |
08 | import android.view.MotionEvent; |
09 | import android.view.View; |
17 | public class MultiTouchTestActivity extends Activity { |
18 | ???? /** Called when the activity is first created. */ |
20 | ???? public void onCreate(Bundle savedInstanceState) { |
21 | ???????? super .onCreate(savedInstanceState); |
23 | ???????? View view = new MultiTouchView( this ); |
24 | ???????? setContentView(view); |
27 | ???? class MultiTouchView extends View { |
29 | ???????? private float x1; |
30 | ???????? private float y1; |
31 | ???????? private float x2; |
32 | ???????? private float y2; |
34 | ???????? public MultiTouchView(Context context) { |
35 | ???????????? super (context); |
40 | ???????? public boolean onTouchEvent(MotionEvent event) { |
43 | ???????????? float size = event.getSize(); |
45 | ???????????? int szi = ( int ) size; |
46 | ???????????? int dxi = szi >> 12 ; |
47 | ???????????? int dyit = (( 1 << 12 ) - 1 ); |
48 | ???????????? int dyi = szi & dyit; |
50 | ???????????? DisplayMetrics metrics = getResources().getDisplayMetrics(); |
51 | ???????????? float dx = metrics.widthPixels * dxi / ( float ) dyit; |
52 | ???????????? float dy = metrics.heightPixels * dyi / ( float ) dyit; |
54 | ???????????? x1 = event.getX(); |
55 | ???????????? y1 = event.getY(); |
57 | ???????????? x2 = x1 + dx; |
58 | ???????????? y2 = y1 + dy; |
60 | ???????????? invalidate(); |
62 | ???????????? return true ; |
66 | ???????? protected void onDraw(Canvas canvas) { |
68 | ???????????? super .onDraw(canvas); |
70 | ???????????? float r = ( float ) Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) |
71 | ???????????????????? * (y1 - y2)) / 2 ; |
72 | ???????????? r = 50 >= r ? 50 : r; |
74 | ???????????? Paint paint = new Paint(); |
75 | ???????????? paint.setColor(Color.BLUE); |
76 | ???????????? canvas.drawCircle(x1, y1, r, paint); |