Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7666535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:51:26+00:00 2026-05-31T14:51:26+00:00

Moving objects in a 2D game is very easy in an Android 2D game,

  • 0

Moving objects in a 2D game is very easy in an Android 2D game, we know. Just use a SurfaceView and draw the single bitmaps with Canvas.drawBitmap().

But when it comes to a rolling object, e.g. a ball, how can I achieve this? Using transformation matrices results in very poor quality of the rendered bitmap, doesn’t it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T14:51:27+00:00Added an answer on May 31, 2026 at 2:51 pm

    If you want to rotate a bitmap using Canvas based drawing, you don’t have much choice but to use transformation matrices, either through a Matrix you pass to Canvas.drawBitmap() or through transformation operations invoked directly on Canvas. And if you enable bitmap filtering with Paint.setFilterBitmap(boolean) the quality will be fine, at least by my standards.

    Here is a complete example (Matrix-based) you can play around with, in particular looking at what difference it makes to have bitmap filtering on and off (or just look at my screenshots below). It doesn’t use SurfaceView though, just a normal custom View, but it should be easy to port to SurfaceView:

    CustomView.java:

    package com.example.android;
    
    import android.content.Context;
    import android.content.res.Resources;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Canvas;
    import android.graphics.Matrix;
    import android.graphics.Paint;
    import android.util.TypedValue;
    import android.view.View;
    
    public class CustomView extends View {
        private static final float DP_PER_SECONDS = 10;
    
        private final float mBallCirfumference;
        private final float mBallRadius;
        private final Bitmap mBallBitmap;
        private final Paint mBallBitmapPaint;
        private final Matrix mBallTransformMatrix = new Matrix();
        private final float mPxPerSecond;
    
        private long mStartTime = -1;
    
        public CustomView(Context context) {
            super(context);
            final Resources res = getResources();
    
            // Load the ball bitmap. You probably want to use a better bitmap ;)
            mBallBitmap = BitmapFactory.decodeResource(res, R.drawable.icon);
    
            // We need the radius and circumference of the ball for our calculations
            // later
            mBallRadius = mBallBitmap.getHeight() / 2;
            mBallCirfumference = mBallRadius * 2 * (float)Math.PI;
    
            // Create ourself a paint object so we can adjust the quality of the
            // bitmap drawing
            mBallBitmapPaint = new Paint();
    
            // Significantly improves quality when drawing transformed bitmaps. Compare
            // with when you disable this, which is the default
            mBallBitmapPaint.setFilterBitmap(true);
    
            // Calculate speed of ball in pixels
            mPxPerSecond = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, DP_PER_SECONDS,
                    res.getDisplayMetrics());
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
    
            // Calculate how far into the animation we are
            if (mStartTime == -1) {
                mStartTime = getDrawingTime();
            }
            long currentTime = getDrawingTime();
            float secondsPassed = (currentTime - mStartTime) / 1000.0f;
    
            // Calculate how far the ball has moved and how many degrees it has been
            // rotated as a consequence of the movement
            float movedDistance = secondsPassed * mPxPerSecond;
            float fullRotationsMade = movedDistance / mBallCirfumference;
            float rotationInDegrees = fullRotationsMade * 360;
    
            // Setup the transformation matrix to simulate a rolling ball
            mBallTransformMatrix.reset();
            mBallTransformMatrix.postRotate(rotationInDegrees, mBallRadius, mBallRadius);
            mBallTransformMatrix.postTranslate(movedDistance, 0);
            canvas.drawBitmap(mBallBitmap, mBallTransformMatrix, mBallBitmapPaint);
    
            // Force redraw so we get an animation
            invalidate();
        }
    }
    

    ExampleActivity.java:

    package com.example.android;
    
    import android.app.Activity;
    import android.os.Bundle;
    
    public class ExampleActivity extends Activity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(new CustomView(this));
        }
    }
    

    Sample screenshot with bitmap filtering enabled. If you run the example, the bitmap will roll like a ball (excuse my poor choice of ball graphics):

    enter image description here

    Sample screenshot with bitmap filtering disabled.

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I work on a 2D shooter game with lots of moving objects on the
we are doing a game with moving objects around frame-by-frame and also using accelerometer.
I'm making a game in HTML5's Canvas + JavaScript. My CPU use for the
I have just started to develop a 2D Android game, something like squash a
I'm making an iPhone game and using UIView objects to draw sprites. Most of
I am making a 2d space game with many moving objects. I have already
I have one triangle ad one circle moving objects on plane.How will i know
I'm trying to make my first android game, just a pong clone really, I
I'm developing a game, obviously there are moving objects... when I move them they
I have multiple objects moving about in a 3D space and am looking for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.