版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/talk_8/article/details/86499847
各位看官们,大家好,上一回中咱们说的是Android中View之触摸事件的例子,这一回咱们继续说该例子。闲话休提,言归正转。让我们一起Talk Android吧!
看官们,我们在上一章回中详细介绍了触摸事件的内容,这些内容都是理论方面的知识,为了加深大家对触摸事件的理解,这一回中我们通过文本结合代码的方式来给大家做演示。
- 1.创建一个Activity并且包含布局文件;
- 2.在布局文件中创建一个Button,把它当作View;
- 3.在布局文件中创建一个TextView,它用来显示View坐标的结果;
- 4.在布局文件中创建另外一个TextView,它用来显示触摸事件坐标的结果;
- 5.在Activity的onCreate方法中为Button设置触摸事件监听器,在监听器中获取Button和触摸事件的坐标,并且通过TextView显示出来;
下面是具体的代码,请大家参考:
<?xml version="1.0" encoding="utf-8"?>
//对应步骤1.使用LinearLayout布局,并且把它当作View(这里使用Button当作View)所在的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:background="@color/colorGreen"
android:layout_marginLeft="30dp"
android:layout_marginTop="30dp"
android:layout_marginRight="30dp"
android:layout_marginBottom="30dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activity2">
/对应步骤2,把它当作View来演示,为了便于观察,给它指定了固定的长度和宽度,背景色设置为纸黄色,而且把它的位置调整到了屏幕中间
<Button
android:id="@+id/btn_of_activity2"
android:layout_width="200dp"
android:layout_height="60dp"
android:layout_marginLeft="60dp"
android:layout_marginTop="180dp"
android:background="@color/colorYellow"
android:gravity="center"
android:text="Button(a kinds of View)"
android:textAllCaps="false"
android:textSize="16sp" />
//对应步骤3,用来显示View坐标的结果
<TextView
android:id="@+id/tva_of_activity2"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
//对应步骤4,用来显示触摸事件坐标的结果
<TextView
android:id="@+id/tvb_of_activity2"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
public class Activity2 extends AppCompatActivity {
private Button mButton;
private TextView mTextViewA;
private TextView mTextViewB;
private String result;
int getTopValue = 0;
int getBottonValue = 0;
int getLeftValue = 0;
int getRightValue = 0;
int highOfButton = 0;
int widthOfButton = 0;
float x,y= 0;
float rawX,rawY = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
mTextViewA = (TextView)findViewById(R.id.tva_of_activity2) ;
mTextViewB = (TextView)findViewById(R.id.tvb_of_activity2) ;
mButton = (Button)findViewById(R.id.btn_of_activity2);
mButton.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
getTopValue = mButton.getTop();
getBottonValue = mButton.getBottom();
getLeftValue = mButton.getLeft();
getRightValue = mButton.getRight();
highOfButton = mButton.getHeight();
widthOfButton = mButton.getWidth();
result = "Location of View: \n"
+ "getTop: " + getTopValue
+ " getBottom: " + getBottonValue + "\n"
+ " getLeft: " + getLeftValue
+ " getRight: " + getRightValue + "\n"
+ " hight: " + highOfButton
+ " width: " + widthOfButton;
mTextViewA.setText(result);
x = event.getX();
y = event.getY();
rawX = event.getRawX();
rawY = event.getRawY();
result = "Location of event: \n"
+" X: "+x+" Y: "+y+"\n"
+ " RawX: "+rawX+" RawY: "+rawY;
mTextViewB.setText(result);
Toast.makeText(getApplicationContext(),"Key Down",Toast.LENGTH_SHORT).show();
break;
case MotionEvent.ACTION_UP:
Toast.makeText(getApplicationContext(),"Key Up",Toast.LENGTH_SHORT).show();
break;
case MotionEvent.ACTION_MOVE:
Toast.makeText(getApplicationContext(),"Key Move",Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(getApplicationContext(),"Key Nothing",Toast.LENGTH_SHORT).show();
break;
}
return false;
}
});
}
}
下面是程序的界面效果,为了方便理解各个函数的功能,我把它们放到了程序界面中,大家可以从图中直观地看到各个函数的功能。在图中我们使用一个小绿点来作为触摸事件,红色的箭头和文字表示获取触摸事件位置坐标的方法(getY方法箭头太短,显示不出文字来)。另外大家注意一下代表getX()
方法的箭头位置,它指向了Button
边缘,因为此时触摸事件位于按钮中,因此它的父布局是Button
而不是绿色背景的布局。还需要注意一下代表getRawY()
方法的箭头位置,它指向了手机屏幕边缘,因为这表示绝对坐标位置。
下面是程序的运行结果,请大家参考:
在该运行结果中大家可以看到触摸事件返回的坐标值为float
类型,而View
返回的坐标为int
类型。我们接着看一下Buttion
占用了大小为600*180
的矩形区域,该区域的中间位置坐标应该是(300,90)
,如果我们触摸该Button
的中间位置时应该会得到这个结果,我们按照这个假设去运行程序,从图片中可以看到运行结果中触摸事件的坐标值为(301,90)
(这里忽略掉了小数点后面的数值),忽略掉小的数据偏差后,可以发现这完全符合我们刚才的假设。
各位看官,关于Androd中View之触摸事件的例子咱们就介绍到这里,欲知后面还有什么例子,且听下回分解!