2013-06-05 74 views
0

我需要縮放圖像。但問題是,如果我用偏移x縮放secunde時間,y指向圖像向左或向右跳。我需要在點x,y - > canvas.scale(scale,scale,x,y)上縮放;不在0,0點 - > canvas.scale(scale,scale);canvas.scale(scale,scale,x,y)

public class Img extends View { 
private Paint paint; 

private Rect imgRect; 
private Bitmap img; 

private int h, w; 
private int x, y; 
private float scale = 1f; 

private int scaleX, scaleY; 
private int aScaleX, aScaleY; 

public img(Context context) { 
    super(context); 

    paint = new Paint(); 

    img = BitmapFactory.decodeResource(getResources(), R.drawable.uebersicht); 
    w = 1330; 
    h = 846;  
    imgRect = new Rect(0, 0, w, h);  
} 

@Override 
protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas);    

    canvas.scale(scale, scale, scaleX, scaleY); 

    paint.setColor(Color.WHITE); 
    canvas.drawBitmap(img, null, imgRect, paint);  

    canvas.restore(); 

    paint.setColor(Color.RED); 
    paint.setTextSize(30); 
    canvas.drawText(String.valueOf(scale), 10, 25, paint); 
} 

public void reDrawPos(int rX, int rY) { 
    x -= rX; 
    y -= rY; 

    calcImg();  
    invalidate();  
} 

public void reDrawScale(float s) { 
    scale = s; 

    calcImg();  
    invalidate(); 
} 

private void calcImg() { 
    mapRect.set(x, y, x+w, y+h); 
} 

public float scaleSet(int sX, int sY) { 
    scaleX = sX; 
    scaleY = sY; 

    return scale; 
}} 

任何人都可以幫助我嗎?

thanx的幫助Bobert

回答

1

有一個叫Matrix.setRectToRect(RectF,RectF,ScaleToFit),以幫助你在這裏方便的方法。

Matrix imageMatrix = imageView.getImageMatrix(); 
RectF drawableRect = new RectF(0, 0, imageWidth, imageHeight); 
RectF viewRect = new RectF(0, 0, imageView.getWidth(), imageView.getHeight()); 
imageMatrix.setRectToRect(drawableRect, viewRect, Matrix.ScaleToFit.CENTER); 
imageView.setImageMatrix(m); 

這應該設置矩陣imageMatrix有比例的組合和翻譯所需要展示中心的繪製和ImageView的小部件內擬合值。

@Override 
public void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 
    canvas.save(); 
    canvas.setMatrix(imageMatrix); 
    ((BitmapDrawable)mIcon).draw(canvas); 
    canvas.restore(); 
} 
+0

Thanx,但我不知道你的意思。我不使用drawable,在位圖中是沒有getImageMatrix()。 – Bobert