Android触屏测试实例代码_Android

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

具体功能代码如下:

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();
   }
  }
}

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

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索android
, 测试
触屏
android 触摸屏测试、android 触摸测试、android studio 实例、android创意实例详解、android界面布局实例,以便于您获取更多的相关知识。

时间: 2024-09-17 04:15:57

Android触屏测试实例代码_Android的相关文章

Android触屏测试实例代码

本文实例详细描述了Android触屏测试代码,可实现对触屏的点击.移动.离开等事件的处理,对于Android初学者有很好的借鉴价值. 具体功能代码如下: 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 从底部弹出Dialog(横向满屏)的实例代码_Android

项目中经常需要底部弹出框,这里我整理一下其中我用的比较顺手的一个方式(底部弹出一个横向满屏的dialog). 效果图如下所示(只显示关键部分): 步骤如下所示: 1.定义一个dialog的布局(lay_share.xml) <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/androi

Android 画一个太极图实例代码_Android

今天练手一下,一起来画个太极图吧~ 最终效果如下: 最终效果 一般都是先讲原理,我就反其道而行,先讲实现吧. 1.继承实现初始化方法 继承View,实现基本的构造函数: public TestView(Context context) { this(context, null); } public TestView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public TestView(Context c

Android字段验证的实例代码_Android

先给大家展示效果图: package com.example.walkerlogin1; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widge

Webview实现android简单的浏览器实例代码_Android

WebView是Android中一个非常实用的组件,它和Safai.Chrome一样都是基于Webkit网页渲染引擎,可以通过加载HTML数据的方式便捷地展现软件的界面,下面通过本文给大家介绍Webview实现android简单的浏览器实例代码. 实现了浏览器的返回 前进 主页 退出 输入网址的功能 注释的很清楚啦 就不多说了 首先是布局文件 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&qu

Android横竖屏切换实例总结_Android

本文实例总结了Android横竖屏切换相关技巧.分享给大家供大家参考,具体如下: 一.禁止横竖屏切换 Android横竖屏切换在手机开发中比较常见,很多软件在开发过程中为了避免横竖屏切换时引发不必要的麻烦,通常禁止掉横竖屏的切换,即通过在AndroidManifest.xml中设置activity中的android:screenOrientation属性值来实现. 该android:screenOrientation属性,他有以下几个参数: "unspecified":默认值 由系统来

android截屏功能实现代码_Android

android开发中通过View的getDrawingCache方法可以达到截屏的目的,只是缺少状态栏! 原始界面 截屏得到的图片 代码实现 1. 添加权限(AndroidManifest.xml文件里) 复制代码 代码如下: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 2. 添加1个Button(activity_main.xml文件) <RelativeL

Android ListView万能适配器实例代码_Android

ListView是开发中最常用的控件了,但是总是会写重复的代码,浪费时间又没有意义. 最近参考一些资料,发现一个万能ListView适配器,代码量少,节省时间,总结一下分享给大家. 首先有一个自定义的Adapter继承于BaseAdapter,下面是自定义的Adapter,精华在getView()方法中 package com.example.mylistview.util; import java.util.List; import android.content.Context; impor

Android Timer使用的实例代码_Android

1:服务端使用PHP 复制代码 代码如下: <?php    echo date('Y-m-d H:i:s');?> 2:activity_main.xml 复制代码 代码如下: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android: