android 旋转图像

发布于 2024-08-28 09:36:29 字数 4433 浏览 11 评论 0原文

我正在尝试创建两个旋转轮,就像滑轮一样,因此每次连接的绳子移动时,两个滑轮都会旋转。我尝试了两种方法:

1)在View类的onDraw()方法中使用Matrix.postRotate,该方法调用以下内容:

private void drawSpinningWheel(Canvas canvas)
{
    try
    {
        canvas.save();

            Bitmap bitmapOrg = null;

        int iDrawable = R.drawable.spinning_button;

        bitmapOrg = BitmapFactory.decodeResource(getResources(), iDrawable);

        if(bitmapOrg != null)
        {
            int width = bitmapOrg.getWidth(); 
            int height = bitmapOrg.getHeight(); 
            int newWidth = 24; 
            int newHeight = 24; 

            // calculate the scale - in this case = 0.4f 
            float scaleWidth = ((float) newWidth) / width; 
            float scaleHeight = ((float) newHeight) / height; 

            // createa matrix for the manipulation 
            Matrix matrix = new Matrix(); 
            // resize the bit map 
            matrix.postScale(scaleWidth, scaleHeight); 
            matrix.postRotate((float) mDegrees++);

            Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, 
                    width, height, matrix, true); 

            canvas.drawBitmap(resizedBitmap, matrix, null);

        }

        canvas.restore();
    }
    catch(Exception e)
    {
        Log.e(TAG + "drawSpinningWheel", e.getMessage());
    }

}

但看起来图像不仅旋转而且绕另一个轴旋转

2)使用SurfaceView和单独的线程,在 run() 中调用:

    private void doDraw(Canvas canvas) {
        // Draw the background image. Operations on the Canvas accumulate
        // so this is like clearing the screen.
        canvas.drawBitmap(mBackgroundImage, 0, 0, null);

        int yTop = mCanvasHeight - ((int) mY + mSpinningWheelImageHeight / 2);
        int xLeft = (int) mX - mSpinningWheelImageWidth / 2;

...

        // Draw the ship with its current rotation
        canvas.save();
        canvas.rotate((float) mHeading++, (float) mX, mCanvasHeight
                - (float) mY);

            mSpinningWheelImage.setBounds(xLeft, yTop, xLeft + mSpinningWheelImageWidth, yTop
                    + mSpinningWheelImageHeight);
            mSpinningWheelImage.draw(canvas);


        canvas.restore();
    }

我让旋转工作,但我无法添加另一个旋转轮。我什至尝试为第二个纺车创建另一个线程,但只有一个出现。有人能指出我正确的方向吗?谢谢。

3)好吧,现在我也尝试了RotateAnimation:

public class GraphicsView extends View { 私有静态最终字符串QUOTE = “没有人再使用 Java。这是一个重量级的大球和链条。”;

private Animation anim;
private Animation anim2;

private Bitmap jobs;
private Bitmap wheel;

private int jobsXOffset;        
private int jobsYOffset;

private int wheelXOffset;        
private int wheelYOffset;

public GraphicsView(Context context) 
{
    super(context);
    jobs = BitmapFactory
    .decodeResource(getResources(), R.drawable.spinning_button);

    jobsXOffset = jobs.getWidth() / 2;
    jobsYOffset = jobs.getHeight() / 2;

    wheel = BitmapFactory
    .decodeResource(getResources(), R.drawable.wheel);

    wheelXOffset = jobs.getWidth() / 2;
    wheelYOffset = jobs.getHeight() / 2;


}

private void createAnim(Canvas canvas) 
{
    anim = new RotateAnimation(0, 360, canvas.getWidth() / 2, canvas
            .getHeight() / 2);
    anim.setRepeatMode(Animation.INFINITE);
    anim.setRepeatCount(Animation.INFINITE);
    anim.setDuration(10000L);
    anim.setInterpolator(new AccelerateDecelerateInterpolator());

    startAnimation(anim);
}

private void createAnim2(Canvas canvas) 
{
    anim2 = new RotateAnimation(0, 360, canvas.getWidth() / 2+30, canvas
            .getHeight() / 2);
    anim2.setRepeatMode(Animation.INFINITE);
    anim2.setRepeatCount(Animation.INFINITE);
    anim2.setDuration(10000L);
    anim2.setInterpolator(new AccelerateDecelerateInterpolator());

    startAnimation(anim);
}

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

    // creates the animation the first time
    if (anim == null) {
        createAnim(canvas);
    }

    int centerX = canvas.getWidth() / 2;
    int centerY = canvas.getHeight() / 2;

    canvas.drawBitmap(jobs, centerX - jobsXOffset,
            centerY - jobsYOffset, null);

    canvas.drawBitmap(wheel, centerX - wheelXOffset + 30,
            centerY - wheelYOffset, null);

}

结果与 Canvas.Rotate 类似,我能够使第一个图像很好地旋转,但第二个图像将简单地围绕第一个图像旋转,尽管我注意到第二个图像实际上也在旋转。看起来我需要一个单独的视图来显示第二张图像,我希望避免这种情况,因为我不确定如何使轮子旋转,但附加的绳子不旋转。

i am trying to create two spinning wheels, as in pulleys, so everytime the attached rope moves, the two pulleys will rotate. i have tried two approaches:

1) use Matrix.postRotate within the onDraw() method of the View class, which calls the following:

private void drawSpinningWheel(Canvas canvas)
{
    try
    {
        canvas.save();

            Bitmap bitmapOrg = null;

        int iDrawable = R.drawable.spinning_button;

        bitmapOrg = BitmapFactory.decodeResource(getResources(), iDrawable);

        if(bitmapOrg != null)
        {
            int width = bitmapOrg.getWidth(); 
            int height = bitmapOrg.getHeight(); 
            int newWidth = 24; 
            int newHeight = 24; 

            // calculate the scale - in this case = 0.4f 
            float scaleWidth = ((float) newWidth) / width; 
            float scaleHeight = ((float) newHeight) / height; 

            // createa matrix for the manipulation 
            Matrix matrix = new Matrix(); 
            // resize the bit map 
            matrix.postScale(scaleWidth, scaleHeight); 
            matrix.postRotate((float) mDegrees++);

            Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, 
                    width, height, matrix, true); 

            canvas.drawBitmap(resizedBitmap, matrix, null);

        }

        canvas.restore();
    }
    catch(Exception e)
    {
        Log.e(TAG + "drawSpinningWheel", e.getMessage());
    }

}

but it seems like the image not only spins but rotates around another axis

2) use SurfaceView and a separate thread, in the run() call this:

    private void doDraw(Canvas canvas) {
        // Draw the background image. Operations on the Canvas accumulate
        // so this is like clearing the screen.
        canvas.drawBitmap(mBackgroundImage, 0, 0, null);

        int yTop = mCanvasHeight - ((int) mY + mSpinningWheelImageHeight / 2);
        int xLeft = (int) mX - mSpinningWheelImageWidth / 2;

...

        // Draw the ship with its current rotation
        canvas.save();
        canvas.rotate((float) mHeading++, (float) mX, mCanvasHeight
                - (float) mY);

            mSpinningWheelImage.setBounds(xLeft, yTop, xLeft + mSpinningWheelImageWidth, yTop
                    + mSpinningWheelImageHeight);
            mSpinningWheelImage.draw(canvas);


        canvas.restore();
    }

i get the spinning to work but i can't add another spinning wheel. i even tried to create another thread for the second spinning wheel, only one shows up. can someone point me in the right direction? thanks.

3) ok now i also tried RotateAnimation:

public class GraphicsView extends View
{
private static final String QUOTE =
"Nobody uses Java anymore. It's this big heavyweight ball and chain.";

private Animation anim;
private Animation anim2;

private Bitmap jobs;
private Bitmap wheel;

private int jobsXOffset;        
private int jobsYOffset;

private int wheelXOffset;        
private int wheelYOffset;

public GraphicsView(Context context) 
{
    super(context);
    jobs = BitmapFactory
    .decodeResource(getResources(), R.drawable.spinning_button);

    jobsXOffset = jobs.getWidth() / 2;
    jobsYOffset = jobs.getHeight() / 2;

    wheel = BitmapFactory
    .decodeResource(getResources(), R.drawable.wheel);

    wheelXOffset = jobs.getWidth() / 2;
    wheelYOffset = jobs.getHeight() / 2;


}

private void createAnim(Canvas canvas) 
{
    anim = new RotateAnimation(0, 360, canvas.getWidth() / 2, canvas
            .getHeight() / 2);
    anim.setRepeatMode(Animation.INFINITE);
    anim.setRepeatCount(Animation.INFINITE);
    anim.setDuration(10000L);
    anim.setInterpolator(new AccelerateDecelerateInterpolator());

    startAnimation(anim);
}

private void createAnim2(Canvas canvas) 
{
    anim2 = new RotateAnimation(0, 360, canvas.getWidth() / 2+30, canvas
            .getHeight() / 2);
    anim2.setRepeatMode(Animation.INFINITE);
    anim2.setRepeatCount(Animation.INFINITE);
    anim2.setDuration(10000L);
    anim2.setInterpolator(new AccelerateDecelerateInterpolator());

    startAnimation(anim);
}

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

    // creates the animation the first time
    if (anim == null) {
        createAnim(canvas);
    }

    int centerX = canvas.getWidth() / 2;
    int centerY = canvas.getHeight() / 2;

    canvas.drawBitmap(jobs, centerX - jobsXOffset,
            centerY - jobsYOffset, null);

    canvas.drawBitmap(wheel, centerX - wheelXOffset + 30,
            centerY - wheelYOffset, null);

}

}

but the result is similar to Canvas.Rotate, i was able to get the first image to rotate fine, but the second image will simply revolve around the first image, although i notice the second image is actually rotating too. looks like i will need a separate view for the second image, i was hoping to avoid this, because i am not sure how to make the wheel spin but the attached rope to not spin.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

查看我的问题及其答案,我认为这就是您(和我)正在寻找的......
如何在其中心点旋转 Android 图标?< /a>

我放置了三个动画,我的徽标文本和我的徽标图标。然后我对文本进行负旋转,对图标进行正旋转。它很简单但很酷。我的问题的答案有代码清单。

Check out my question and its answer, I think it is what you (and I) are looking for ...
How to spin an android icon on its center point?

I put three animations, the text of my logo and my logo icon. Then I put a negative rotation on the text and positive on the icon. It is simple but cool. The answer on my question has the code listing.

无名指的心愿 2024-09-04 09:36:29

您尝试过RotateAnimation吗?

Have you tried RotateAnimation?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文