简体   繁体   English

在Android上围绕中心旋转位图

[英]Rotating a bitmap around center on android

Good day, my first question here after wasting 3+ hours. 美好的一天,我浪费3个多小时后在这里提出的第一个问题。 I have reviewed dozens of different posts with similar questions and a small wide of answers. 我已经审查了数十个不同的帖子,这些帖子有类似的问题和少量答案。

I'm currently designing a function that takes a bitmap and an angle, and returns a rotated version, for my spaceship android game 我目前正在为我的太空飞船android游戏设计一个函数,该函数需要位图和角度,并返回旋转的版本

public static Bitmap RotateBitmap(Bitmap source, float angle){ 


    Matrix matrix = new Matrix();

    matrix.setRotate(angle, source.getWidth()/2,  source.getHeight()/2);

    return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);

}

i have tried several different options , mixing translate, post and pre rotate methods, but all proven unneffective. 我尝试了几种不同的方法,混合了平移,后旋转和预旋转方法,但都被证明无效。 I have to admit I have limited knowledge on matrix but android developer sources seem crystal clear to me, yet any method I tried does anything regarding the pivot point. 我必须承认我对矩阵的知识有限,但是android开发人员的资料对我来说似乎很清楚,但是我尝试过的任何方法都不会对枢轴点产生任何影响。 The rotation always happens but effectively displacing my ship to other hexes 旋转总是发生,但有效地将我的船转移到其他位置

Thanks in advance ^^ 在此先感谢^^

Couldn't manage with this function so I ended up making another with same effect. 无法使用此功能进行管理,因此我最终制作了另一个具有相同效果的产品。

public void rotate(Bitmap paramBitmap, float angle, Canvas canvas, int x, int y)
{
    canvas.save();
    canvas.translate(x, y);
    canvas.rotate(angle);
    canvas.translate(-paramBitmap.getWidth() / 2, -paramBitmap.getHeight() / 2);
    canvas.drawBitmap(paramBitmap, 0, 0, new Paint());
    canvas.restore();

}

It rotates the canvas instead 而是旋转画布

I've arrived at the same point you were at when you posted this question. 当您发布此问题时,我已经到达了您的观点。 My bitmap is rotated, but also offset. 我的位图旋转了,但也偏移了。 The offset isn't very noticeable until you place another image under or over it, and it then becomes VERY evident. 直到您在其下方或上方放置另一个图像,该偏移量才非常明显,然后变得非常明显。
To clarify, was your workaround to rotate the entire display around your spaceship image, keeping your bitmap image static (ie always pointing in the same direction)? 澄清一下,您是否要解决的问题是围绕着飞船图像旋转整个显示器,使位图图像保持静态(即始终指向同一方向)? I'm considering moving to a different tool other than Canvas to do this, and will report back if I find something more effective. 我正在考虑使用除Canvas以外的其他工具来执行此操作,如果我发现更有效的方法,则会进行报告。

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

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