基于Android 实现图片平移、缩放、旋转同时进行

2019-12-10 19:53:20于海丽
  • }  /** 
  • * 图片居中显示、判断旋转角度 小于(90 * x + 45)度图片旋转(90 * x)度 大于则旋转(90 * (x+1))  */ 
  • private void centerAndRotate() {  RectF rect = new RectF(0, 0, imgWidth, imgHeight); 
  • matrix.mapRect(rect);  float width = rect.width(); 
  • float height = rect.height();  float dx = 0; 
  • float dy = 0;  if (width 2 - width / 2 - rect.left; 
  • } else if (rect.left > 0) {  dx = -rect.left; 
  • } else if (rect.right if (height 2 - height / 2 - rect.top;  } else if (rect.top > 0) { 
  • dy = -rect.top;  } else if (rect.bottom if (rotation != 0) { 
  • int rotationNum = (int) (rotation / 90);  float rotationAvai = new BigDecimal(rotation % 90).setScale(1, BigDecimal.ROUND_HALF_UP).floatValue(); 
  • float realRotation = 0;  if (rotation > 0) { 
  • realRotation = rotationAvai > 45 ? (rotationNum + 1) * 90 : rotationNum * 90;  } else if (rotation 0) { 
  • realRotation = rotationAvai 45 ? (rotationNum - 1) * 90 : rotationNum * 90;  } 
  • System.out.println("realRotation: " + realRotation);  matrix.postRotate(realRotation, displayWidth / 2, displayHeight / 2); 
  • rotation = 0;  } 
  • }  /** 
  • * 显示网络图片时使用  */ 
  • private class MyTask extends AsyncTaskFile, File, Bitmap> {  Bitmap bitmap; 
  • String path;  int scale = 1; 
  • long size;  @Override 
  • protected Bitmap doInBackground(File... params) {  // TODO Auto-generated method stub 
  • try {  size = params[0].length();