android 如何利用手势放大和缩小一个图片?



   
  •                                                             
  •    
            
            
            
            
    1. import android.app.Activity;
    2. import android.graphics.Bitmap;
    3. import android.graphics.BitmapFactory;
    4. import android.graphics.Matrix;
    5. import android.graphics.PointF;
    6. import android.hardware.SensorManager;
    7. import android.os.Bundle;
    8. import android.util.FloatMath;
    9. import android.util.Log;
    10. import android.view.MotionEvent;
    11. import android.view.View;
    12. import android.view.View.OnClickListener;
    13. import android.view.View.OnTouchListener;
    14. import android.widget.Button;
    15. import android.widget.ImageView;
    16. public class Touch extends Activity implements OnTouchListener, OnClickListener {
    17.    private static final String TAG = "Touch" ;
    18.    // These matrices will be used to move and zoom image
    19.    Matrix matrix = new Matrix();
    20.    Matrix savedMatrix = new Matrix();
    21.    PointF start = new PointF();
    22.    PointF mid = new PointF();
    23.    float  oldDist;
    24.    private ImageView view;
    25.    private Button zoomIn, zoomOut;
    26.    //button zoom
    27.    private float scaleWidth = 1;
    28.    private float scaleHeight = 1;
    29.    private Bitmap bmp, zoomedBMP;
    30.    private int zoom_level = 0;
    31.    private static final double ZOOM_IN_SCALE = 1.25;//放大系数
    32.    private static final double ZOOM_OUT_SCALE = 0.8;//缩小系数
    33.    // We can be in one of these 3 states
    34.    static final int NONE = 0;
    35.    static final int DRAG = 1;
    36.    static final int ZOOM = 2;
    37.    int mode = NONE;
    38.    @Override
    39.    public void onCreate(Bundle savedInstanceState) {
    40.       super.onCreate(savedInstanceState);
    41.       setContentView(R.layout.main);
    42.       //放大按钮
    43.       zoomIn = (Button) findViewById(R.id.zoom_in);
    44.        //缩小按钮
    45.       zoomOut = (Button) findViewById(R.id.zoom_out);
    46.       zoomIn.setOnClickListener(this);
    47.       zoomOut.setOnClickListener(this);
    48.       view = (ImageView) findViewById(R.id.imageView);
    49.       view.setOnTouchListener(this);
    50.       //取得drawable中图片,放大,缩小,多点触摸的作用对象
    51.        bmp = BitmapFactory.decodeResource(Touch.this.getResources(), R.drawable.butterfly);
    52.               }
    53.    public boolean onTouch(View v, MotionEvent event) {
    54. // Handle touch events here...
    55. ImageView view = (ImageView) v;
    56. // Handle touch events here...
    57. switch (event.getAction() & MotionEvent.ACTION_MASK) {
    58. //设置拖拉模式
    59. case MotionEvent.ACTION_DOWN:
    60. savedMatrix.set(matrix);
    61. start.set(event.getX(), event.getY());
    62. Log.d(TAG, "mode=DRAG" );
    63. mode = DRAG;
    64. break;
    65. case MotionEvent.ACTION_UP:
    66. case MotionEvent.ACTION_POINTER_UP:
    67. mode = NONE;
    68. Log.d(TAG, "mode=NONE" );
    69. break;
    70. //设置多点触摸模式
    71. case MotionEvent.ACTION_POINTER_DOWN:
    72. oldDist = spacing(event);
    73. Log.d(TAG, "oldDist=" + oldDist);
    74. if (oldDist > 10f) {
    75. savedMatrix.set(matrix);
    76. midPoint(mid, event);
    77. mode = ZOOM;
    78. Log.d(TAG, "mode=ZOOM" );
    79. }
    80. break;
    81. //若为DRAG模式,则点击移动图片
    82. case MotionEvent.ACTION_MOVE:
    83. if (mode == DRAG) {
    84. matrix.set(savedMatrix);
    85. // 设置位移
    86. matrix.postTranslate(event.getX() - start.x,
    87. event.getX() - start.x);
    88. }
    89. //若为ZOOM模式,则多点触摸缩放
    90. else if (mode == ZOOM) {
    91. float newDist = spacing(event);
    92. Log.d(TAG, "newDist=" + newDist);
    93. if (newDist > 10f) {
    94. matrix.set(savedMatrix);
    95. float scale = newDist / oldDist;
    96. //设置缩放比例和图片中点位置
    97. matrix.postScale(scale, scale, mid.x, mid.y);
    98. }
    99. }
    100. break;
    101. }
    102. // Perform the transformation
    103. view.setImageMatrix(matrix);
    104. return true; // indicate event was handled
    105.    }
    106. //计算移动距离
    107.    private float spacing(MotionEvent event) {
    108. float x = event.getX(0) - event.getX(1);
    109. float y = event.getY(0) - event.getY(1);
    110. return FloatMath.sqrt(x * x + y * y);
    111.    }
    112. //计算中点位置
    113.    private void midPoint(PointF point, MotionEvent event) {
    114. float x = event.getX(0) + event.getX(1);
    115. float y = event.getY(0) + event.getY(1);
    116. point.set(x / 2, y / 2);
    117.    }
    118. //放大,缩小按钮点击事件
    119. @Override
    120. public void onClick(View v) {
    121.         if(v == zoomIn){
    122.                 enlarge();
    123.         }else if (v == zoomOut) {
    124.                 small();
    125.         }
    126. }
    127. //按钮点击缩小函数
    128. private void small() {
    129. int bmpWidth = bmp.getWidth();
    130. int bmpHeight = bmp.getHeight();
    131. scaleWidth = (float) (scaleWidth * ZOOM_OUT_SCALE);
    132. scaleHeight = (float) (scaleHeight * ZOOM_OUT_SCALE);
    133. Matrix matrix = new Matrix();
    134. matrix.postScale(scaleWidth, scaleHeight);
    135. zoomedBMP = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight, matrix,
    136. true);
    137. view.setImageBitmap(zoomedBMP);
    138. }
    139. //按钮点击放大函数
    140. private void enlarge() {
    141.         try {
    142.                 int bmpWidth = bmp.getWidth();
    143.                 int bmpHeight = bmp.getHeight();
    144.                 scaleWidth = (float) (scaleWidth * ZOOM_IN_SCALE);
    145.                 scaleHeight = (float) (scaleHeight * ZOOM_IN_SCALE);
    146.                 Matrix matrix = new Matrix();
    147.                 matrix.postScale(scaleWidth, scaleHeight);
    148.                 zoomedBMP = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight, matrix,
    149.                                 true);
    150.                 view.setImageBitmap(zoomedBMP);
    151.         } catch (Exception e) {
    152.                 //can't zoom because of memory issue, just ignore, no big deal
    153.         }
    154. }
    155. }

    你可能感兴趣的:(android 如何利用手势放大和缩小一个图片?)