zoukankan      html  css  js  c++  java
  • android滑动基础篇 TouchView

    效果图:

    代码部分:

    activity类代码:

    1. package com.TouchView;
    2.   
    3. import android.app.Activity;  
    4. import android.os.Bundle;  
    5. import android.view.MotionEvent;  
    6. import android.view.View;  
    7. import android.widget.TextView;  
    8.   
    9. public class TouchView extends Activity {  
    10.   
    11.     private TextView eventlable;  
    12.     private TextView histroy;  
    13.     private TextView TouchView;  
    14.     @Override  
    15.     public void onCreate(Bundle savedInstanceState) {  
    16.         super.onCreate(savedInstanceState);  
    17.         setContentView(R.layout.main);  
    18.         TouchView =(TextView)findViewById(R.id.touch_area);  
    19.         histroy =(TextView)findViewById(R.id.history_label);  
    20.         eventlable =(TextView)findViewById(R.id.event_label);  
    21.           
    22.         TouchView.setOnTouchListener(new View.OnTouchListener() {  
    23.               
    24.             @Override  
    25.             public boolean onTouch(View v, MotionEvent event) {  
    26.                 int action =event.getAction();  
    27.                 switch(action){  
    28.                 //当按下的时候  
    29.                 case (MotionEvent.ACTION_DOWN):  
    30.                     Display("ACTION_DOWN",event);  
    31.                 break;  
    32.                 //当按上的时候  
    33.                 case(MotionEvent.ACTION_UP):  
    34.                     int historysize=ProcessHistory(event);  
    35.                     histroy.setText("历史数据"+historysize);  
    36.                     Display("ACTION_UP",event);  
    37.                     break;  
    38.                  //当触摸的时候  
    39.                 case(MotionEvent.ACTION_MOVE):  
    40.                     Display("ACTION_MOVE",event);  
    41.                 }  
    42.                 return true;  
    43.             }  
    44.         });  
    45.     }  
    46.     public void Display(String eventType,MotionEvent event){  
    47.         //触点相对坐标的信息  
    48.         int x =(int) event.getX();  
    49.         int y=(int)event.getY();  
    50.         //表示触屏压力大小  
    51.         float pressure =event.getPressure();  
    52.         //表示触点尺寸  
    53.         float size=event.getSize();  
    54.         //获取绝对坐标信息  
    55.         int RawX=(int)event.getRawX();  
    56.         int RawY=(int)event.getRawY();  
    57.           
    58.         String msg="";  
    59.           
    60.         msg+="事件类型"+eventType+" ";  
    61.         msg+="相对坐标"+String.valueOf(x)+","+String.valueOf(y)+" ";  
    62.         msg+="绝对坐标"+String.valueOf(RawX)+","+String.valueOf(RawY)+" ";  
    63.         msg+="触点压力"+String.valueOf(pressure)+",";  
    64.         msg+="触点尺寸"+String.valueOf(size)+" ";  
    65.         eventlable.setText(msg);  
    66.     }  
    67.     public int ProcessHistory(MotionEvent event){  
    68.         int history =event.getHistorySize();  
    69.         for(int i=0;i<history;i++){  
    70.             long time=event.getHistoricalEventTime(i);  
    71.             float pressure=event.getHistoricalPressure(i);  
    72.             float x=event.getHistoricalX(i) ;  
    73.             float y=event.getHistoricalY(i);  
    74.             float size=event.getHistoricalSize(i);    
    75.         }  
    76.           
    77.         return history;  
    78.           
    79.     }  
    80.       
    81. }  

    MAIN.XML代码部分:

    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:orientation="vertical"  
    4.     android:layout_width="fill_parent"  
    5.     android:layout_height="fill_parent"  
    6.     >  
    7. <TextView    
    8.     android:id="@+id/touch_area"  
    9.     android:layout_width="fill_parent"   
    10.     android:layout_height="300dip"   
    11.     android:background="#0FF"  
    12.     android:textColor="#FFFFFF"  
    13.     android:text="触摸事件测试区"  
    14.    
    15.     />  
    16.     <TextView    
    17.     android:id="@+id/history_label"  
    18.     android:layout_width="fill_parent"   
    19.     android:layout_height="wrap_content"   
    20.     android:text="历史数据"  
    21.     
    22.     />  
    23.     <TextView    
    24.     android:id="@+id/event_label"  
    25.     android:layout_width="fill_parent"   
    26.     android:layout_height="wrap_content"   
    27.     android:text="触摸事件:"  
    28.     
    29.     />  
    30. </LinearLayout>  

  • 相关阅读:
    数据库版本管理工具flyway
    spring webapp的配置文件放置在项目外的方法
    logback
    linux as4 bind9 设置进程中的一些小效果
    设置/勾销Debian的屏保
    Linux内存:内存管理的天禀
    用YUM晋级CentOS体系中PHP和MySQL
    solaris的故事
    Solaris 的防火墙ipfilter设置
    mysql安置设置文件的成绩
  • 原文地址:https://www.cnblogs.com/anyuan9/p/6171630.html
Copyright © 2011-2022 走看看