歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android的數據存儲與IO - 手勢

Android的數據存儲與IO - 手勢

日期:2017/3/1 10:14:59   编辑:Linux編程

Android的數據存儲與IO - 手勢

關於手勢的知識,我是第一次接觸,是Android提供的另類的IO

可以進行手勢檢測、通過指定手勢完成相應的動作、可以自行添加手勢到手勢庫,也可以識別手勢

下面是一個實例:通過手勢縮放圖片

創建項目:GestureZoom

運行項目效果如下:

Activity文件:GestureZoom

  1. package wwj.gesturezoom;
  2. import android.app.Activity;
  3. import android.graphics.Bitmap;
  4. import android.graphics.BitmapFactory;
  5. import android.graphics.Matrix;
  6. import android.graphics.drawable.BitmapDrawable;
  7. import android.os.Bundle;
  8. import android.view.GestureDetector;
  9. import android.view.GestureDetector.OnGestureListener;
  10. import android.view.MotionEvent;
  11. import android.widget.ImageView;
  12. public class GestureZoom extends Activity implements OnGestureListener {
  13. //定義手勢檢測器實例
  14. GestureDetector detector;
  15. ImageView imageView;
  16. //初始圖片資源
  17. Bitmap bitmap;
  18. //定義圖片的寬、高
  19. int width, height;
  20. //記錄當前的縮放比
  21. float currentScale = 1;
  22. //控制圖片縮放的Matrix對象
  23. Matrix matrix;
  24. @Override
  25. public void onCreate(Bundle savedInstanceState) {
  26. super.onCreate(savedInstanceState);
  27. setContentView(R.layout.main);
  28. //創建手勢檢測器
  29. detector = new GestureDetector(this);
  30. imageView = (ImageView)findViewById(R.id.show);
  31. matrix = new Matrix();
  32. //獲取被縮放的源圖片
  33. bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.flower);
  34. //獲得位圖寬
  35. width = bitmap.getWidth();
  36. //獲得位圖高
  37. height = bitmap.getHeight();
  38. //設置ImageView初始化時顯示的圖片
  39. imageView.setImageBitmap(BitmapFactory.decodeResource(this.getResources(), R.drawable.flower));
  40. }
  41. @Override
  42. public boolean onTouchEvent(MotionEvent event) {
  43. // TODO Auto-generated method stub
  44. //將該Acitivity上觸碰事件交給GestureDetector處理
  45. return detector.onTouchEvent(event);
  46. }
  47. public boolean onDown(MotionEvent e) {
  48. // TODO Auto-generated method stub
  49. return false;
  50. }
  51. public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
  52. float velocityY) {
  53. // TODO Auto-generated method stub
  54. velocityX = velocityX > 4000 ? 4000 : velocityX;
  55. velocityY = velocityY < -4000 ? -4000 : velocityY;
  56. //根據手勢的速度來計算縮放比,如果velocity > 0, 放大圖像,否則縮小圖像
  57. currentScale += currentScale * velocityX / 4000.0f;
  58. //保證currentScale不會等於0
  59. currentScale = currentScale > 0.01 ? currentScale : 0.01f;
  60. //重置Matrix
  61. matrix.reset();
  62. //縮放Matrix
  63. matrix.setScale(currentScale, currentScale, 160, 200);
  64. BitmapDrawable tmp = (BitmapDrawable) imageView.getDrawable();
  65. //如果圖片還未回收,先強制回收該圖片
  66. if(!tmp.getBitmap().isRecycled()){
  67. tmp.getBitmap().recycle();
  68. }
  69. //根據原始位圖和Matrix創建新圖片
  70. Bitmap bitmap2 = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
  71. //顯示新的位圖
  72. imageView.setImageBitmap(bitmap2);
  73. return true;
  74. }
  75. public void onLongPress(MotionEvent e) {
  76. // TODO Auto-generated method stub
  77. }
  78. public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
  79. float distanceY) {
  80. // TODO Auto-generated method stub
  81. return false;
  82. }
  83. public void onShowPress(MotionEvent e) {
  84. // TODO Auto-generated method stub
  85. }
  86. public boolean onSingleTapUp(MotionEvent e) {
  87. // TODO Auto-generated method stub
  88. return false;
  89. }
  90. }
更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11
Copyright © Linux教程網 All Rights Reserved