Android 如何在不使用矩阵的情况下放大位图?

Android 如何在不使用矩阵的情况下放大位图?,android,Android,在android中,如果不使用matrix,如何缩放图像 以下是我正在使用的代码: public class SimpImageView extends BaseView{ private Bitmap image = null; private Paint borderPaint = null; PointF start = new PointF(); PointF lastPosition = new PointF(); Matrix savedMatrix =

在android中,如果不使用matrix,如何缩放图像

以下是我正在使用的代码:

public class SimpImageView extends BaseView{

  private Bitmap image = null;

  private Paint borderPaint = null;

  PointF start = new PointF();
  PointF lastPosition = new PointF();

  Matrix savedMatrix = new Matrix();

  private Paint imagePaint = null;

  private Matrix matrix = null;

  public Bitmap getImage() {
    return image;
  }

  public void setImage(Bitmap image) {
    this.image = image;
  }

  public SimpImageView(Bitmap image,int x,int y,int width,int height){
    super(x, y, width, height);
    this.image = image;
    borderPaint = new Paint();
    borderPaint.setARGB(255, 255, 128, 128);
    borderPaint.setStyle(Paint.Style.STROKE);
    borderPaint.setStrokeWidth(2);
    imagePaint = new Paint();
    imagePaint.setARGB(32, 255, 255, 255);
    imagePaint.setStyle(Paint.Style.FILL);

  }

  /* (non-Javadoc)
   * @see com.simplogics.surfaceview.Views.BaseView#onDraw(android.graphics.Canvas)
   */
  @Override
  public void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    canvas.drawBitmap(image, getBounds().left, getBounds().top, null);

    //        canvas.drawBitmap(image, matrix, new Paint());

    //  canvas.drawBitmap(image, matrix, borderPaint);

    //  canvas.drawRect(getBounds() , borderPaint);
  }

  /* (non-Javadoc)
   * @see com.simplogics.surfaceview.Views.BaseView#onTouchEvent(android.view.MotionEvent)
   */


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


  @Override

  public boolean onTouchEvent(MotionEvent event) {
    boolean handled = false;

    Log.d("SimpEditText", "(int)event.getX() " + (int)event.getX());
    Log.d("SimpEditText", "(int)event.getY() " + (int)event.getY());

    if(isTouchedInBounds((int)event.getX(), (int)event.getY())){
      Log.d("SimpEditText", "Touched in Bounds");
      handled = true;
      switch (event.getAction() & MotionEvent.ACTION_MASK) {
      case MotionEvent.ACTION_DOWN:
        start.set(event.getX(), event.getY());
        lastPosition.x = getBounds().left;
        lastPosition.y = getBounds().top;

        Log.d("VerticalLabelView", "mode=DRAG");
        mode = DRAG;
        break;
      case MotionEvent.ACTION_POINTER_DOWN:
        mode = ZOOM;
        Log.d("VerticalLabelView", "mode=ZOOM");
        break;
      case MotionEvent.ACTION_UP:
        mode = NONE;
        break;
      case MotionEvent.ACTION_POINTER_UP:
        mode = NONE;
        Log.d("VerticalLabelView", "mode=NONE");
        break;
      case MotionEvent.ACTION_MOVE:
        if (mode == DRAG) {
          int x = (int)(event.getX() - start.x);
          int y = (int)(event.getY() - start.y);
          setPosition((int)lastPosition.x + x, (int)lastPosition.y + y);
          mode = DRAG;
          break;
        }else if(mode == ZOOM){
          Log.d("SimpImageView", "MODE=ZOOM");
          Log.d("SimpImageView", "getImage().getWidth()="+getImage().getWidth());
          Log.d("SimpImageView"," getImage().getHeight()="+getImage().getHeight());

        }
        break;
      }
    }

    return handled;
  }
}
baseView.java

public abstract class BaseView {

  private int id = -1;

  private Object tag = null;

  private Rect bounds = null;

  public Rect getBounds() {
    return bounds;
  }

  public BaseView() {
    bounds = new Rect();
  }

  public BaseView(int x, int y, int width, int height) {
    bounds = new Rect(x, y, x + width, y + height);
  }

  public abstract void onDraw(Canvas canvas);

  public abstract  boolean onTouchEvent(MotionEvent event);

  public void setBounds(int x, int y, int width, int height){
    bounds.set(x, y, x + width, y + height);
  }

  public void setPosition(int x, int y){
    if(x <= 0 || y <= 0){
      Log.e("SimpEditText1", "----------------------------------------------------------");
      Log.e("SimpEditText1", "-------------0000000000000000000000000000000000------------");
      Log.e("SimpEditText1", "----------------------------------------------------------");
    }
    bounds.set(x, y, x + bounds.width(), y + bounds.height());
  }

  public void setScale(int x,int y,int width,int height){
    bounds.set(x, y, x + bounds.width()+width, y + bounds.height()+height);
  }

  protected boolean isTouchedInBounds(int xPos, int yPos){
    return bounds.contains(xPos, yPos);
  }

  public Object getTag() {
    return tag;
  }

  public void setTag(Object tag) {
    this.tag = tag;
  }

  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }

  /**
   * @param w
   * @param h
   * @param oldw
   * @param oldh
   */
}
公共抽象类BaseView{
私有int id=-1;
私有对象标记=null;
private Rect bounds=null;
公共Rect getBounds(){
返回边界;
}
公共BaseView(){
边界=新的Rect();
}
公共基本视图(整数x、整数y、整数宽度、整数高度){
边界=新矩形(x,y,x+宽度,y+高度);
}
公开摘要(画布);
公共抽象布尔onTouchEvent(MotionEvent);
公共空心收进边界(整数x、整数y、整数宽度、整数高度){
设置(x,y,x+宽度,y+高度);
}
公共无效设置位置(整数x,整数y){

如果(x)为什么没有矩阵?有什么原因吗?您的BaseView需要扩展一个视图类。此外,我想不出任何不使用矩阵的原因。当然,您可以在zoomlevel更改时使用Bitmap.createScaledBitmap()创建一个新位图。但此解决方案存在内存和性能问题。