Android开发图片的旋转和缩放

流浪的星星 发布于 2012/11/19 16:40
阅读 2K+
收藏 11

直接上代码:

第一步新建TouchImageView类

package com.yzlt.touchimage;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.FloatMath;
import android.view.MotionEvent;
import android.widget.ImageView;

public class TouchImageView extends ImageView {

 float x_down = 0;
 float y_down = 0;
 PointF start = new PointF();
 PointF mid = new PointF();
 float oldDist = 0.1f;
 float oldRotation = 0;
 Matrix matrix = new Matrix();
 Matrix matrix1 = new Matrix();
 Matrix savedMatrix = new Matrix();

 private static final int NONE = 0;
 private static final int DRAG = 1;
 private static final int ZOOM = 2;
 int mode = NONE;

 boolean matrixCheck = false;
 int widthScreen;
 int heightScreen;

 Bitmap gintama;

 public TouchImageView(TouchImageViewActivity activity) {
  super(activity);
  gintama = BitmapFactory.decodeResource(getResources(),
    R.drawable.picture);
  DisplayMetrics dm = new DisplayMetrics();
  activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
  widthScreen = dm.widthPixels;
  heightScreen = dm.heightPixels;

  matrix = new Matrix();

 }

 protected void onDraw(Canvas canvas) {
  canvas.save();
  canvas.drawBitmap(gintama, matrix, null);
  canvas.restore();
 }

 public boolean onTouchEvent(MotionEvent event) {
  switch (event.getAction() & MotionEvent.ACTION_MASK) {
  case MotionEvent.ACTION_DOWN:
   mode = DRAG;
   x_down = event.getX();
   y_down = event.getY();
   savedMatrix.set(matrix);
   break;
  case MotionEvent.ACTION_POINTER_DOWN:
   mode = ZOOM;
   oldDist = spacing(event);
   // oldRotation = rotation(event);
   savedMatrix.set(matrix);
   midPoint(mid, event);
   break;
  case MotionEvent.ACTION_MOVE:
   if (mode == ZOOM) {
    matrix1.set(savedMatrix);
    // float rotation = rotation(event) - oldRotation;
    float newDist = spacing(event);
    float scale = newDist / oldDist;
    matrix1.postScale(scale, scale, mid.x, mid.y);// 缩放
    // matrix1.postRotate(rotation(event), mid.x, mid.y);// 旋转
    matrixCheck = matrixCheck();
    if (matrixCheck == false) {
     matrix.set(matrix1);
     invalidate();
    }
   } else if (mode == DRAG) {
    matrix1.set(savedMatrix);
    matrix1.postTranslate(event.getX() - x_down, event.getY()
      - y_down);// 平移
    matrixCheck = matrixCheck();
    if (matrixCheck == false) {
     matrix.set(matrix1);
     invalidate();
    }
   }
   break;
  case MotionEvent.ACTION_UP:
  case MotionEvent.ACTION_POINTER_UP:
   mode = NONE;
   break;

  }
  return true;
 }

 private boolean matrixCheck() {
  float[] f = new float[9];
  matrix1.getValues(f);
  // 图片4个定点坐标
  float x1 = f[0] * 0 + f[1] * 0 + f[2];
  float y1 = f[3] * 0 + f[4] * 0 + f[5];
  float x2 = f[0] * gintama.getWidth() + f[1] * 0 + f[2];
  float y2 = f[3] * gintama.getWidth() + f[4] * 0 + f[5];
  float x3 = f[0] * 0 + f[1] * gintama.getHeight() + f[2];
  float y3 = f[3] * 0 + f[4] * gintama.getHeight() + f[5];
  float x4 = f[0] * gintama.getWidth() + f[1] * gintama.getHeight()
    + f[2];
  float y4 = f[3] * gintama.getWidth() + f[4] * gintama.getHeight()
    + f[5];
  // 图片现宽度
  double width = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
  // 缩放比率判断
  if (width < widthScreen / 3 || width > widthScreen * 3) {
   return true;
  }
  // 出界判断
  if ((x1 < widthScreen * 1 && x2 < widthScreen * 1
    && x3 < widthScreen * 1 && x4 < widthScreen * 1)
    || (x1 > widthScreen * 1 / 1000000
      && x2 > widthScreen * 1 / 1000000
      && x3 > widthScreen * 1 / 1000000 && x4 > widthScreen * 1 / 1000000)
    || (y1 < heightScreen * 1 && y2 < heightScreen * 1
      && y3 < heightScreen * 1 && y4 < heightScreen * 1)
    || (y1 > heightScreen * 1 / 1000000
      && y2 > heightScreen * 1 / 1000000
      && y3 > heightScreen * 1 / 1000000 && y4 > heightScreen * 1 / 1000000)) {
   return true;
  }
  return false;
 }

 // 取手势中心点
 private void midPoint(PointF point, MotionEvent event) {
  float x = event.getX(0) - event.getX(1);
  float y = event.getY(0) - event.getY(1);
  point.set(x / 2, y / 2);
  point.set(x, y);
 }

 // 取旋转角度
 private float rotation(MotionEvent event) {
  double delta_x = (event.getX(0) - event.getX(1));
  double delta_y = (event.getY(0) - event.getY(1));
  double radians = Math.atan2(delta_y, delta_x);
  return (float) Math.toDegrees(radians);
 }

 // 触碰两点间距离
 private float spacing(MotionEvent event) {
  float x = event.getX(0) - event.getX(1);
  float y = event.getY(0) - event.getY(1);
  return FloatMath.sqrt(x * x + y * y);
 }

 // 将移动,缩放以及旋转后的图层保存为新图片
 // 本例中没有用到该方法,需要保存图片的可以参考
 public Bitmap CreatNewPhoto() {
  Bitmap bitmap = Bitmap.createBitmap(widthScreen, heightScreen,
    Config.ARGB_8888);// 背景图片
  Canvas canvas = new Canvas(bitmap);// 新建画布
  canvas.drawBitmap(gintama, matrix, null);// 画图片
  canvas.save(Canvas.ALL_SAVE_FLAG);// 保存画布
  canvas.restore();
  return bitmap;
 }
}

第二步写Activity主类

package com.yzlt.touchimage;

import android.app.Activity;
import android.os.Bundle;

public class TouchImageViewActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TouchImageView img = new TouchImageView(this);
        setContentView(img);
    }
}

不需要编写Xml布局文件

 

加载中
0
莱恩
莱恩
虽然看不懂,但是好像很牛逼的样子。
0
cut
cut
比较好奇,为啥你的图片旋转也要翻转矩阵,OPENGL做的么,但OPENGL区域裁剪不用自己写代码的,FBO+glViewport就简单实现旋转,缩放,区域裁剪,保存图片像素等功能。
返回顶部
顶部