gpt4 book ai didi

安卓: Canvas 不缩放

转载 作者:太空宇宙 更新时间:2023-11-03 13:30:08 24 4
gpt4 key购买 nike

我有一个自定义 View ,它在 onDraw() 中在 Canvas 上绘制一些位图。绘图正在工作。查看代码 fragment :

@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.save();

if(appInitialized) {

hsSide.draw(canvas);
scaleA.draw(canvas);
scaleB.draw(canvas);

}
canvas.scale(mScaleFactor, mScaleFactor, pivotX, pivotY);
canvas.restore();
}

我已经实现了一个 ScaleGestureListener,它可以在屏幕上应用缩放缩放时缩放 Canvas 。问题是 Canvas 没有缩放。如果我将 canvas.scale(mScaleFactor, mScaleFactor, pivotX, pivotY); 放在 if(appInitialized) { 之前,它会缩放 Canvas ,但在它之后是绘图,即 hsSide .draw(canvas); 不缩放。它采用正常比例。以下是绘制方法:

hsDide.draw:

@Override
public void draw(Canvas canvas) {
// TODO Auto-generated method stub
canvas.drawBitmap(mBitmap, canvas.getWidth()/2 - mBitmap.getWidth()/2, 0, null);
}

scaleA.draw:

@Override
public void draw(Canvas canvas) {
// TODO Auto-generated method stub
canvas.drawBitmap(mBitmap, canvas.getWidth()/2 - mBitmap.getWidth()/2, canvas.getHeight()/2 - mBitmap.getHeight()/2, null);
}

scaleB.draw:

@Override
public void draw(Canvas canvas) {
// TODO Auto-generated method stub

canvas.save();
mMatrix.setRotate(currentAngle, canvas.getWidth()/2, canvas.getHeight()/2);
canvas.setMatrix(mMatrix);
canvas.drawBitmap(mBitmap, canvas.getWidth()/2 - mBitmap.getWidth()/2, canvas.getHeight()/2 - mBitmap.getHeight()/2, null);
canvas.restore();
}

知道我哪里出错了...???

最佳答案

事实上,查看您的代码,您应该在绘制之前缩放 Canvas ,然后在绘制后恢复它。因此,按如下方式移动 Canvas 比例:

@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.save();

canvas.scale(mScaleFactor, mScaleFactor, pivotX, pivotY); // MOVED HERE.

if(appInitialized) {

hsSide.draw(canvas);
scaleA.draw(canvas);
scaleB.draw(canvas);

}

canvas.restore();
}

然后您将根据 Canvas 尺寸绘制尺寸。因此,当您缩放 Canvas 时,您仍然会将其绘制到整个尺寸,然后再调整回正常尺寸。因此没有任何效果。

您需要做的是在缩放之前 获取 Canvas 大小,然后将其也传递给您的绘制方法。然后使用这些尺寸在 Canvas 上绘图。

关于安卓: Canvas 不缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14441778/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com