关闭

图片缩放移动

117人阅读 评论(0) 收藏 举报

创建MyImageView.java代码extends SurfaceView

package com.example.picture;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

public class MyImageView extends SurfaceView implements Callback {

    private SurfaceHolder holder = null;
    private Bitmap bitmap = null;
    private Matrix matrix = new Matrix();

    // 一根手指在触碰
    private int ONE_TOUCH = 1;
    // 多根手指在触碰
    private int MORE_TOUCH = 2;
    // 当前触碰的手指模拟
    private int currentTouch = ONE_TOUCH;

    // 单根手指触碰的开始点
    private Point startPoint = new Point();

    // 多根手指滑动的前后距离
    private float dis1;
    private float dis2;
    // 多根手指滑动的中心点
    private Point centerPoint = new Point();
    private Matrix savedMatrix = new Matrix();



    public MyImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public MyImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyImageView(Context context) {
        super(context);
        init();
    }

    /**
     * 初始化
     */
    private void init() {
        holder = this.getHolder();
        holder.addCallback(this);

        // 得到一个Bitmap对象
        bitmap = ((BitmapDrawable) getResources().getDrawable(
                R.drawable.android_book)).getBitmap();

    }


    /**
     * 绘画
     */
    private void draw() {
        // 锁定画布
        Canvas canvas = holder.lockCanvas();
        if (canvas == null) {
            return;
        }

        // 清除画布
        canvas.drawColor(Color.BLACK);

        // 画图片
        canvas.drawBitmap(bitmap, matrix, null);

        // 解锁画布并提交
        holder.unlockCanvasAndPost(canvas);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        /** 通过与运算保留最后八位 MotionEvent.ACTION_MASK = 255 */
        // 能接受单根和多根手指的触碰
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
            // 设置模式为单根手指
            currentTouch = ONE_TOUCH;
            // 得到单根手指一开始接触的坐标
            startPoint.set((int) event.getX(), (int) event.getY());
            savedMatrix.set(matrix);

            break;
        // 当屏幕上已经有触点(手指),再有一个触点压下屏幕
        case MotionEvent.ACTION_POINTER_DOWN:
            // 设置模式为多根手指触碰
            currentTouch = MORE_TOUCH;
            // 得到两根手指一开始触碰的距离
            dis1 = getDistance(event);

            if (dis1 > 10f) {
                // 得到这两根手指的中心点
                getCenter(event);
                // 记录当前图片的缩放倍数
                savedMatrix.set(matrix);

            }

            break;
        case MotionEvent.ACTION_MOVE:
            if (currentTouch == ONE_TOUCH) {
                // 如果是一根手指,则设置平移,平移的距离是从开始点到现在的移动点
                // 当前位置上移动
                matrix.set(savedMatrix);
                matrix.postTranslate((event.getX() - startPoint.x),
                        (event.getY() - startPoint.y));

            } else if (currentTouch == MORE_TOUCH) {
                // 如果是多根手指模式,则设置缩放,缩放的比例是刚接触时两根手指之间的距离与移动后两根手指之间的距离比.
                // 缩放的中心点是刚接触时两根手指的中心点
                dis2 = getDistance(event);
                if (dis2 > 10f) {
                    float scale = dis2 / dis1;

                    matrix.set(savedMatrix);
                    matrix.postScale(scale, scale, centerPoint.x, centerPoint.y);
                }
            }
            break;
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_POINTER_UP:
            currentTouch = 0;
            break;
        default:
            break;
        }

        // 重新画图像
        draw();

        return true;
    }

    /**
     * 得到两根手指之间的距离
     * 
     * @param event
     * @return
     */
    private float getDistance(MotionEvent event) {
        float a = event.getX(1) - event.getX(0);
        float b = event.getY(1) - event.getY(0);
        return (float) Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
    }

    /**
     * 得到两根手指之间的中心点
     * 
     * @param event
     */
    private void getCenter(MotionEvent event) {
        float a = event.getX(1) + event.getX(0);
        float b = event.getY(1) + event.getY(0);
        centerPoint.set((int) a / 2, (int) b / 2);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // 让这个View在创建的时候就开始画画
        draw();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }

}

MainActivity.java代码

package com.example.picture;


import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.SurfaceHolder;

public class MainActivity extends Activity{

    private SurfaceHolder holder = null ;
    private int screenWidth;
    private int screenHeight;
    private Bitmap bitmap;
    private int imageWidth;
    private int imageHeight;
    private int imageX;
    private int imageY;
    private MyImageView myImageView;
    private double Scale = 1.0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


//      this.screenWidth = super.getWindowManager().getDefaultDisplay().getWidth();
//      this.screenHeight = super.getWindowManager().getDefaultDisplay().getHeight();
//      this.bitmap = BitmapFactory.decodeResource(super.getResources(), R.drawable.android_book);
//      
//      this.imageWidth = this.bitmap.getWidth();
//      this.imageHeight = this.bitmap.getHeight();
//      
//      this.imageX = (this.screenWidth - this.imageWidth) / 2 ;
//      this.imageY = (this.screenHeight - this.imageHeight) / 2 ;

        myImageView = new MyImageView(this);


        super.setContentView(myImageView);
    }



}
2
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:4564次
    • 积分:321
    • 等级:
    • 排名:千里之外
    • 原创:29篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条