Android怎样通过手势实现缩放处理
3个回答
展开全部
android开发是目前很热门的课程,我们要了解通过手势实现的缩放处理 的基础才可以更好的应用Android系统。import android.app.Activity;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.os.Bundle;import android.util.DisplayMetrics;import android.view.MotionEvent;import android.view.View;/*** ...** @author vlinux**/public class MultiTouchTestActivity extends Activity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// setContentView(R.layout.main);View view = new MultiTouchView(this);setContentView(view);}class MultiTouchView extends View {private float x1;private float y1;private float x2;private float y2;public MultiTouchView(Context context) {super(context);// TODO Auto-generated constructor stub}@Overridepublic boolean onTouchEvent(MotionEvent event) {// TODO Auto-generated method stubfloat size = event.getSize();int szi = (int) size;int dxi = szi >> 12;int dyit = ((1 << 12) - 1);int dyi = szi & dyit;DisplayMetrics metrics = getResources().getDisplayMetrics();float dx = metrics.widthPixels * dxi / (float) dyit;float dy = metrics.heightPixels * dyi / (float) dyit;x1 = event.getX();y1 = event.getY();x2 = x1 + dx;y2 = y1 + dy;invalidate();return true;}@Overrideprotected void onDraw(Canvas canvas) {// TODO Auto-generated method stubsuper.onDraw(canvas);float r = (float) Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2)* (y1 - y2)) / 2;r = 50 >= r ? 50 : r;Paint paint = new Paint();paint.setColor(Color.BLUE);canvas.drawCircle(x1, y1, r, paint);}天行健,君子以自强不息。勤奋是上天赐予我们的最大的财富,希望每一个人都能把握住自己的人生,用勤奋来获取多彩的人生。
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
1. [代码][Java]代码 跳至 [1] [全屏预览]
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
/**
* ...
*
* @author vlinux
*
*/
public class MultiTouchTestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
View view = new MultiTouchView(this);
setContentView(view);
}
class MultiTouchView extends View {
private float x1;
private float y1;
private float x2;
private float y2;
public MultiTouchView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
float size = event.getSize();
int szi = (int) size;
int dxi = szi >> 12;
int dyit = ((1 << 12) - 1);
int dyi = szi & dyit;
DisplayMetrics metrics = getResources().getDisplayMetrics();
float dx = metrics.widthPixels * dxi / (float) dyit;
float dy = metrics.heightPixels * dyi / (float) dyit;
x1 = event.getX();
y1 = event.getY();
x2 = x1 + dx;
y2 = y1 + dy;
invalidate();
return true;
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
float r = (float) Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2)
* (y1 - y2)) / 2;
r = 50 >= r ? 50 : r;
Paint paint = new Paint();
paint.setColor(Color.BLUE);
canvas.drawCircle(x1, y1, r, paint);
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
/**
* ...
*
* @author vlinux
*
*/
public class MultiTouchTestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
View view = new MultiTouchView(this);
setContentView(view);
}
class MultiTouchView extends View {
private float x1;
private float y1;
private float x2;
private float y2;
public MultiTouchView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
float size = event.getSize();
int szi = (int) size;
int dxi = szi >> 12;
int dyit = ((1 << 12) - 1);
int dyi = szi & dyit;
DisplayMetrics metrics = getResources().getDisplayMetrics();
float dx = metrics.widthPixels * dxi / (float) dyit;
float dy = metrics.heightPixels * dyi / (float) dyit;
x1 = event.getX();
y1 = event.getY();
x2 = x1 + dx;
y2 = y1 + dy;
invalidate();
return true;
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
float r = (float) Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2)
* (y1 - y2)) / 2;
r = 50 >= r ? 50 : r;
Paint paint = new Paint();
paint.setColor(Color.BLUE);
canvas.drawCircle(x1, y1, r, paint);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
两指合并在一起缩放
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询