Android触屏测试实例代码
2023-06-13 09:15:40 时间
本文实例详细描述了Android触屏测试代码,可实现对触屏的点击、移动、离开等事件的处理,对于Android初学者有很好的借鉴价值。
具体功能代码如下:
packagecom.test; importandroid.app.Activity; importandroid.os.Bundle; importandroid.util.DisplayMetrics; importandroid.util.Log; importandroid.view.MotionEvent; importandroid.view.View; importandroid.widget.AbsoluteLayout; importandroid.widget.Button; importandroid.widget.ImageView; importandroid.widget.Toast; publicclassTouchActivityextendsActivity{ /*声明ImageView变量*/ privateImageViewmImageView01; /*声明相关变量作为存储图片宽高,位置使用*/ privateintintWidth,intHeight,intDefaultX,intDefaultY; privatefloatmX,mY; /*声明存储屏幕的分辨率变量*/ privateintintScreenX,intScreenY; publicvoidonCreate(BundlesavedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); /*取得屏幕对象*/ DisplayMetricsdm=newDisplayMetrics(); 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(newButton.OnClickListener() { @Override publicvoidonClick(Viewv) { RestoreButton(); } }); } /*覆盖触控事件*/ publicbooleanonTouchEvent(MotionEventevent) { /*取得手指触控屏幕的位置*/ floatx=event.getX(); floaty=event.getY(); try { /*触控事件的处理*/ switch(event.getAction()) { /*点击屏幕*/ caseMotionEvent.ACTION_DOWN: picMove(x,y); break; /*移动位置*/ caseMotionEvent.ACTION_MOVE: picMove(x,y); break; /*离开屏幕*/ caseMotionEvent.ACTION_UP: picMove(x,y); break; } }catch(Exceptione) { e.printStackTrace(); } returntrue; } /*移动图片的方法*/ privatevoidpicMove(floatx,floaty) { /*默认微调图片与指针的相对位置*/ mX=x-(intWidth/2); mY=y-(intHeight/2); /*防图片超过屏幕的相关处理*/ /*防止屏幕向右超过屏幕*/ if((mX+intWidth)>intScreenX) { mX=intScreenX-intWidth; } /*防止屏幕向左超过屏幕*/ elseif(mX<0) { mX=0; } /*防止屏幕向下超过屏幕*/ elseif((mY+intHeight)>intScreenY) { mY=intScreenY-intHeight; } /*防止屏幕向上超过屏幕*/ elseif(mY<0) { mY=0; } /*通过log来查看图片位置*/ Log.i("jay",Float.toString(mX)+","+Float.toString(mY)); /*以setLayoutParams方法,重新安排Layout上的位置*/ mImageView01.setLayoutParams ( newAbsoluteLayout.LayoutParams (intWidth,intHeight,(int)mX,(int)mY) ); } /*还原ImageView位置的事件处理*/ publicvoidRestoreButton() { intDefaultX=((intScreenX-intWidth)/2); intDefaultY=((intScreenY-intHeight)/2); /*Toast还原位置坐标*/ mMakeTextToast ( "("+ Integer.toString(intDefaultX)+ ","+ Integer.toString(intDefaultY)+")",true ); /*以setLayoutParams方法,重新安排Layout上的位置*/ mImageView01.setLayoutParams ( newAbsoluteLayout.LayoutParams (intWidth,intHeight,intDefaultX,intDefaultY) ); } /*自定义一发出信息的方法*/ publicvoidmMakeTextToast(Stringstr,booleanisLong) { if(isLong==true) { Toast.makeText(TouchActivity.this,str,Toast.LENGTH_LONG).show(); } else { Toast.makeText(TouchActivity.this,str,Toast.LENGTH_SHORT).show(); } } }
读者还可以在该实例的基础上完善各种事件的响应处理函数,使其功能更加丰富。
相关文章
- 74款android开机动画,修改Android系统开机动画
- strictmode android,(十三)Android 性能优化 StrictMode
- android activity singletask,Android Activity启动模式之singleTask实例详解
- android短信验证码代码,Android短信验证码自动填写实现代码
- Android resource linking failed_android sdk location should not
- Android studio更新后出现警告:Warning:The `android.dexOptions.incremental` property is deprecated and it has
- Yolov5 Android tf-lite方式集成
- 【Android 高性能音频】OboeTest 音频性能测试应用 ( 应用简介 | 测试内容 | 输出测试 | Oboe 缓冲区 与 工作负载修改 | 测试案例 )
- 【Android 插件化】Hook 插件化框架 ( Hook Activity 启动流程 | 主线程创建 Activity 实例之前使用插件 Activity 类替换占位的组件 )
- 【Android 逆向】代码调试器开发 ( 使用 NDK 中的 ndk-build + Android.mk 编译 Android 平台的代码调试器可执行应用 )
- 【Android Gradle 插件】Gradle 构建机制 ⑤ ( 在 Android Studio 中查看 Android Gradle 插件源码 )
- Android控制文字水平间距android:letterSpacing详解手机开发
- [android] 手机卫士保存密码时进行md5加密详解手机开发
- android手机SD卡读写操作(以txt文本为例)实现步骤
- Android文件操作方法
- Android获取手机型号/系统版本号/App版本号等信息实例讲解
- Android访问php取回json数据实例
- Android之RAS加密算法测试实例
- android自定义TabActivity的实例方法
- Android根据电话号码获得联系人头像实例代码