[英]Android canvas.scale(-1,1)
So my aim is to flip an image horizontally then draw it on a canvas. 所以我的目标是水平翻转图像然后在画布上绘制。 Currently I'm using canvas.scale(-1,1) which effectively works and draws the image horizontally, however it also screws with the x axis values where before the scale the x position would be 150 and after I'd have to switch it to -150 to render in the same spot.
目前我正在使用canvas.scale(-1,1),它可以有效地工作并水平绘制图像,但是它也会固定x轴值,在x刻度之前,x位置为150,之后我必须切换它到-150在同一个地方渲染。
My question is, how can I make it so the x value is 150 in both cases without having to adjust the x position after the scale? 我的问题是,如何在两种情况下都能使x值为150,而无需在比例后调整x位置? Is there a more effective way to do this without taking a hit on performance?
有没有一种更有效的方法来做到这一点而不会影响性能?
I know this question is old, but I happened to bump into the same problem. 我知道这个问题很老,但我碰巧遇到了同样的问题。 In my situation, I had to flip the canvas when drawing on a class extending an ImageButton.
在我的情况下,我必须在绘制扩展ImageButton的类时翻转画布。 Fortunately, the solution for this specific case was more elegant than I thought.
幸运的是,这个特定案例的解决方案比我想象的更优雅。 Simply override the
onDraw(Canvas)
method as follows: 只需覆盖
onDraw(Canvas)
方法,如下所示:
@Override
protected void onDraw(final Canvas canvas) {
// Scale the canvas, offset by its center.
canvas.scale(-1f, 1f,
super.getWidth() * 0.5f, super.getHeight() * 0.5f);
// Draw the button!
super.onDraw(canvas);
}
I've fixed this by applying the transformation to the bitmap prior to ever using it like this: 我已经通过在使用它之前将变换应用到位图来解决这个问题,如下所示:
public void applyMatrix(Matrix matrix) {
mBitmap = Bitmap.createBitmap(mBitmap, 0, 0,
mBitmap.getWidth(), mBitmap.getHeight(), matrix, true);
}
...
Matrix matrix = new Matrix();
matrix.preScale(-1, 1);
mSprite.applyMatrix(matrix);
Did you try repeating the canvas.scale(-1, 1)
? 你尝试过重复
canvas.scale(-1, 1)
吗? It will effectively remove the transformation, since two negatives make a positive. 它将有效地消除转变,因为两个消极因素是积极的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.