一个小程序:Instrumentation的使用
来源:互联网 发布:网络打印机主机ip地址 编辑:程序博客网 时间:2024/06/15 13:22
本来是想练习Matrix的,没想到写了一个自定义View,监听它的ASWD键后,不知道该如何按下ASWD(手机上一般都没实体按键了)。于是:
一个自定义View:
public class MyView extends View { private final Bitmap bitmap; private int width; private int height; private Matrix matrix = new Matrix(); private boolean isScale = false; //设置倾斜度 private float sx = 0.0f; //缩放比例 private float scale = 1.0f; public MyView(Context context, AttributeSet attrs) { super(context, attrs); //获得位图 bitmap = ((BitmapDrawable) context.getResources().getDrawable(R.drawable.a)).getBitmap(); //获得位图宽 width = bitmap.getWidth(); height = bitmap.getHeight(); //当前视图获得焦点 this.setFocusable(true); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //重置Matrix matrix.reset(); if (!isScale) { //旋转Matrix matrix.setSkew(sx, 0); } else { matrix.setScale(scale, scale); } //根据原始位图和Matrix创建新图片 Bitmap bitmap1 = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true); //绘制新位图 canvas.drawBitmap(bitmap1, matrix, null); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { switch (keyCode) { //向左倾斜 case KeyEvent.KEYCODE_A: isScale = false; sx += 0.1; postInvalidate(); break; //向右倾斜 case KeyEvent.KEYCODE_D: isScale = false; sx -= 0.1; postInvalidate(); break; //放大 case KeyEvent.KEYCODE_W: isScale = true; if (scale < 2.0) scale += 0.1; postInvalidate(); break; //缩小 case KeyEvent.KEYCODE_S: isScale = true; if (scale > 0.5) scale -= 0.1; postInvalidate(); break; } return super.onKeyDown(keyCode, event); }}
然后是Activity里的实现(把按钮的点击事件传递给WASD):
public class MatrixActivity extends Activity { private Button left; private Button right; private Button top; private Button bottom; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_matrix); left = (Button) findViewById(R.id.left); right = (Button) findViewById(R.id.right); top = (Button) findViewById(R.id.top); bottom = (Button) findViewById(R.id.bottom); left.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { sendKeyEvent(29); } }); right.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { sendKeyEvent(32); } }); top.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { sendKeyEvent(51); } }); bottom.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { sendKeyEvent(47); } }); } public static void sendKeyEvent(final int KeyCode) { new Thread() { //不可在主线程中调用 public void run() { try { Instrumentation inst = new Instrumentation(); inst.sendKeyDownUpSync(KeyCode); } catch (Exception e) { e.printStackTrace(); } } }.start(); }}
0 0
- 一个小程序:Instrumentation的使用
- SDK Instrumentation创建一个Note的实例
- android开发笔记之Instrumentation一个小应用
- 使用CREST测试 前对多文件程序的 Instrumentation 方法
- 使用Tkinter编写一个简单的提醒小程序
- C--使用指针做的一个小程序
- 关于android instrumentation的理解、使用
- 一个XmlHttpRequest的小程序
- jsf的一个小程序
- jetty的一个小程序
- 一个无聊的小程序
- 一个小的jdbc程序
- ipython的一个小程序
- java的一个小程序
- Java的一个小程序
- 数据库的一个小程序
- JSP的一个小程序
- 一个小程序的小BUG
- 贝塞尔曲线
- 卷积神经网络(Convolutional Neural Networks)概述
- 8大策略让你对抗机器学习数据集里的不均衡数据
- C语言二维数组与指针
- JS中的DOM方法大全
- 一个小程序:Instrumentation的使用
- 红外与可见光图像配准算法--MATLAB版
- php生成图片验证码
- PHP无限分类-左右值实现
- python json
- redhat6.4下添加一个用户并赋管理员权限
- appium环境搭建步骤记录(二)-appium环境配置及sdkmanager
- Linux下找不到so文件的解决办法
- 判断两个链表是否相交并找出交点