Android的數據存儲與IO - 手勢
關於手勢的知識,我是第一次接觸,是Android提供的另類的IO
可以進行手勢檢測、通過指定手勢完成相應的動作、可以自行添加手勢到手勢庫,也可以識別手勢
下面是一個實例:通過手勢縮放圖片
創建項目:GestureZoom
運行項目效果如下:
Activity文件:GestureZoom
- package wwj.gesturezoom;
-
- import android.app.Activity;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Matrix;
- import android.graphics.drawable.BitmapDrawable;
- import android.os.Bundle;
- import android.view.GestureDetector;
- import android.view.GestureDetector.OnGestureListener;
- import android.view.MotionEvent;
- import android.widget.ImageView;
-
- public class GestureZoom extends Activity implements OnGestureListener {
-
- //定義手勢檢測器實例
- GestureDetector detector;
- ImageView imageView;
- //初始圖片資源
- Bitmap bitmap;
- //定義圖片的寬、高
- int width, height;
- //記錄當前的縮放比
- float currentScale = 1;
- //控制圖片縮放的Matrix對象
- Matrix matrix;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //創建手勢檢測器
- detector = new GestureDetector(this);
- imageView = (ImageView)findViewById(R.id.show);
- matrix = new Matrix();
- //獲取被縮放的源圖片
- bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.flower);
- //獲得位圖寬
- width = bitmap.getWidth();
- //獲得位圖高
- height = bitmap.getHeight();
- //設置ImageView初始化時顯示的圖片
- imageView.setImageBitmap(BitmapFactory.decodeResource(this.getResources(), R.drawable.flower));
- }
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- // TODO Auto-generated method stub
- //將該Acitivity上觸碰事件交給GestureDetector處理
- return detector.onTouchEvent(event);
- }
- public boolean onDown(MotionEvent e) {
- // TODO Auto-generated method stub
- return false;
- }
- public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
- float velocityY) {
- // TODO Auto-generated method stub
- velocityX = velocityX > 4000 ? 4000 : velocityX;
- velocityY = velocityY < -4000 ? -4000 : velocityY;
- //根據手勢的速度來計算縮放比,如果velocity > 0, 放大圖像,否則縮小圖像
- currentScale += currentScale * velocityX / 4000.0f;
- //保證currentScale不會等於0
- currentScale = currentScale > 0.01 ? currentScale : 0.01f;
- //重置Matrix
- matrix.reset();
- //縮放Matrix
- matrix.setScale(currentScale, currentScale, 160, 200);
- BitmapDrawable tmp = (BitmapDrawable) imageView.getDrawable();
- //如果圖片還未回收,先強制回收該圖片
- if(!tmp.getBitmap().isRecycled()){
- tmp.getBitmap().recycle();
- }
- //根據原始位圖和Matrix創建新圖片
- Bitmap bitmap2 = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
- //顯示新的位圖
- imageView.setImageBitmap(bitmap2);
- return true;
- }
- public void onLongPress(MotionEvent e) {
- // TODO Auto-generated method stub
-
- }
- public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
- float distanceY) {
- // TODO Auto-generated method stub
- return false;
- }
- public void onShowPress(MotionEvent e) {
- // TODO Auto-generated method stub
-
- }
- public boolean onSingleTapUp(MotionEvent e) {
- // TODO Auto-generated method stub
- return false;
- }
-
- }
更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11