服务器之家

服务器之家 > 正文

Android触屏测试实例代码

时间:2021-03-05 15:49     来源/作者:Android开发网

本文实例详细描述了Android触屏测试代码,可实现对触屏的点击、移动、离开等事件的处理,对于Android初学者有很好的借鉴价值。

具体功能代码如下:

?
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package com.test;
import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class TouchActivity extends Activity {
  /*声明ImageView变量*/
  private ImageView mImageView01;
  /*声明相关变量作为存储图片宽高,位置使用*/
  private int intWidth, intHeight, intDefaultX, intDefaultY;
  private float mX, mY;
  /*声明存储屏幕的分辨率变量 */
  private int intScreenX, intScreenY;
  public void onCreate(Bundle savedInstanceState)
  {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);
   
   /* 取得屏幕对象 */
   DisplayMetrics dm = new DisplayMetrics();
   getWindowManager().getDefaultDisplay().getMetrics(dm);
   
   /* 取得屏幕解析像素 */
   intScreenX = dm.widthPixels;
   intScreenY = dm.heightPixels;
   
   /* 设置图片的宽高 */
   intWidth = 100;
   intHeight = 100;
   /*通过findViewById构造器创建ImageView对象*/
   mImageView01 =(ImageView) findViewById(R.id.myImageView1);
   /*将图片从Drawable赋值给ImageView来呈现*/
   mImageView01.setImageResource(R.drawable.baby);
   
   /* 初始化按钮位置居中 */
   RestoreButton();
   
   /* 当点击ImageView,还原初始位置 */
   mImageView01.setOnClickListener(new Button.OnClickListener()
   {
    @Override
    public void onClick(View v)
    {
     RestoreButton();
    }
   });
  }
  
  /*覆盖触控事件*/
  public boolean onTouchEvent(MotionEvent event)
  {
   /*取得手指触控屏幕的位置*/
   float x = event.getX();
   float y = event.getY();
   
   try
   {
    /*触控事件的处理*/
    switch (event.getAction())
    {
     /*点击屏幕*/
     case MotionEvent.ACTION_DOWN:
      picMove(x, y);
       break;
     /*移动位置*/
     case MotionEvent.ACTION_MOVE:
      picMove(x, y);
       break;
     /*离开屏幕*/
     case MotionEvent.ACTION_UP:
      picMove(x, y);
       break;
    }
   }catch(Exception e)
    {
     e.printStackTrace();
    }
   return true;
  }
  /*移动图片的方法*/
  private void picMove(float x, float y)
  {
   /*默认微调图片与指针的相对位置*/
   mX=x-(intWidth/2);
   mY=y-(intHeight/2);
   
   /*防图片超过屏幕的相关处理*/
   /*防止屏幕向右超过屏幕*/
   if((mX+intWidth)>intScreenX)
   {
    mX = intScreenX-intWidth;
   }
   /*防止屏幕向左超过屏幕*/
   else if(mX<0)
   {
    mX = 0;
   }
   /*防止屏幕向下超过屏幕*/
   else if ((mY+intHeight)>intScreenY)
   {
    mY=intScreenY-intHeight;
   }
   /*防止屏幕向上超过屏幕*/
   else if (mY<0)
   {
    mY = 0;
   }
   /*通过log 来查看图片位置*/
   Log.i("jay", Float.toString(mX)+","+Float.toString(mY));
   /* 以setLayoutParams方法,重新安排Layout上的位置 */
   mImageView01.setLayoutParams
   (
    new AbsoluteLayout.LayoutParams
    (intWidth,intHeight,(int) mX,(int)mY)
   );
  }
  
  /* 还原ImageView位置的事件处理 */
  public void RestoreButton()
  {
   intDefaultX = ((intScreenX-intWidth)/2);
   intDefaultY = ((intScreenY-intHeight)/2);
   /*Toast还原位置坐标*/
   mMakeTextToast
   (
    "("+
    Integer.toString(intDefaultX)+
    ","+
    Integer.toString(intDefaultY)+")",true
   );
   
   /* 以setLayoutParams方法,重新安排Layout上的位置 */
   mImageView01.setLayoutParams
   (
    new AbsoluteLayout.LayoutParams
    (intWidth,intHeight,intDefaultX,intDefaultY)
   );
  }
  
  /*自定义一发出信息的方法*/
  public void mMakeTextToast(String str, boolean isLong)
  {
   if(isLong==true)
   {
    Toast.makeText(TouchActivity.this, str, Toast.LENGTH_LONG).show();
   }
   else
   {
    Toast.makeText(TouchActivity.this, str, Toast.LENGTH_SHORT).show();
   }
  }
}

读者还可以在该实例的基础上完善各种事件的响应处理函数,使其功能更加丰富。

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部