如何在不使用 android 矩阵的情况下缩放图像?
这是我正在使用的代码:
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
*/
}