简体   繁体   English

canvas.scale(scale,scale,x,y)

[英]canvas.scale(scale, scale, x, y)

i need to scale a image. 我需要缩放图像。 But the problem is that if i scale secunde time with diverent x,y point the image jump left or right. 但是问题是,如果我用不同的x,y点缩放第二时间,则图像会向左或向右跳转。

I need to scale at point x,y -> canvas.scale(scale, scale, x, y); 我需要在点x,y-> canvas.scale(scale,scale,x,y); not at point 0,0 -> canvas.scale(scale, scale); 不在点0,0-> canvas.scale(scale,scale);

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;
}}

Can any one help me? 谁能帮我?

Thanx for Help Bobert 感谢帮助博伯特

There's a convenient method called Matrix.setRectToRect(RectF, RectF, ScaleToFit) to help you here. 有一个方便的方法叫做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);

That should set the matrix imageMatrix to have combo of scaling and translate values that is needed to show the drawable centered and fit within the ImageView widget. 这应该将矩阵imageMatrix设置为具有比例缩放和转换值的组合,以显示可绘制的居中图像并适合ImageView小部件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM