android滑动基础篇 - 触屏显示信息

来源:互联网 发布:博德之门2增强版 mac 编辑:程序博客网 时间:2024/05/21 22:46

效果图:


代码部分:

 activity类代码:

package com.TouchView;/* * android滑动基础篇 * */import android.app.Activity;import android.os.Bundle;import android.view.MotionEvent;import android.view.View;import android.widget.TextView;public class TouchView extends Activity {        private TextView eventlable;  //触摸事件    private TextView histroy;  //历史数据    private TextView TouchView;  //触摸事件测试区        public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.main);          TouchView =(TextView)findViewById(R.id.touch_area);          histroy =(TextView)findViewById(R.id.history_label);          eventlable =(TextView)findViewById(R.id.event_label);                  /*设置触摸监听*/        TouchView.setOnTouchListener(new View.OnTouchListener() {                            public boolean onTouch(View v, MotionEvent event) {                  int action =event.getAction();                  switch(action){                  //当按下的时候                  case (MotionEvent.ACTION_DOWN):                      Display("ACTION_DOWN",event);                  break;                  //当按上的时候                  case(MotionEvent.ACTION_UP):                      int historysize=ProcessHistory(event);                      histroy.setText("历史数据: "+historysize);                      Display("ACTION_UP",event);                      break;                   //当触摸的时候                  case(MotionEvent.ACTION_MOVE):                      Display("ACTION_MOVE",event);                  }                  return true;              }          });      }         //显示事件类型以及事件信息(坐标,压力,尺寸,时间等)    public void Display(String eventType,MotionEvent event){          //触点相对坐标的信息          int x =(int) event.getX();          int y=(int)event.getY();          //表示触屏压力大小          float pressure =event.getPressure();          //表示触点尺寸          float size=event.getSize();          //获取绝对坐标信息          int RawX=(int)event.getRawX();          int RawY=(int)event.getRawY();                    String msg="触摸事件:\n";          msg+="事件类型"+eventType+"\n";          msg+="相对坐标"+String.valueOf(x)+","+String.valueOf(y)+"\n";          msg+="绝对坐标"+String.valueOf(RawX)+","+String.valueOf(RawY)+"\n";          msg+="触点压力"+String.valueOf(pressure)+" , ";          msg+="触点尺寸"+String.valueOf(size)+"\n";          eventlable.setText(msg);      }         /*可以通过调用getHistorySize来获得历史的大小值,它可以返回当前事件可用的运动位置的数目。     * 然后你可以通过使用一系列getHistorical*方法,并传递给它位置索引,来获得每一个历史事件的时间、压力、大小和位置*/    public int ProcessHistory(MotionEvent event){          int historysize =event.getHistorySize();  // 获取历史采样的集合大小         for(int i=0;i<historysize;i++){              long time=event.getHistoricalEventTime(i);  // 历史采样的时间点              float pressure=event.getHistoricalPressure(i);              float x=event.getHistoricalX(i) ;              float y=event.getHistoricalY(i);              float size=event.getHistoricalSize(i);            }         return historysize;      }  }  

MAIN.XML代码部分:

<?xml version="1.0" encoding="utf-8"?>  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:orientation="vertical"      android:layout_width="fill_parent"      android:layout_height="fill_parent"      >  <TextView        android:id="@+id/touch_area"      android:layout_width="fill_parent"       android:layout_height="360dip"       android:background="#0FF"      android:textColor="#FFFFFF"      android:text="触摸事件测试区" />           <TextView        android:id="@+id/history_label"      android:layout_width="fill_parent"       android:layout_height="wrap_content"       android:background="#006400"     android:text="历史数据" />          <TextView        android:id="@+id/event_label"        android:layout_width="fill_parent"        android:layout_height="match_parent"        android:background="#C0C0C0"        android:textColor="#000000"         android:text="触摸事件:" />  </LinearLayout>  








0 0