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 8163997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:12:15+00:00 2026-06-06T19:12:15+00:00

Can any1 tell me why I cant add a bitmap onto my surfaceView like

  • 0

Can any1 tell me why I cant add a bitmap onto my surfaceView like this:

    steering = new Steering(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher), getWidth()-50,getHeight()-50);

if I use integers instead of the “getHeight()”-method, the bitmap is added just fine. But since I want this game to run on more then 1 phone without looking weird I want to add it using those 2 methods.

can any1 help?

thanks!

  • 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-06-06T19:12:17+00:00Added an answer on June 6, 2026 at 7:12 pm

    where exactly you are adding that line? if its on your onCreate then it wouldnt display you the image since the method getWidth() and getHeight() would return 0. So to paint it you have to wait until the system have actually created the view.
    To test that you are actually receiving a value try changing the code you actually have for something like this:

    final int width = getWidth();
    final int height = getHeight();
    final Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
    steering = new Steering(bitmap, width-50,height-50);
    

    and add a breakpoint to the steering line and debugg it. If you are getting 0 in the width and height then you will have to wait for the view to draw.

    Edit:
    on your Activity/Fragment you can add a tree observer like this:

     myView.getViewTreeObserver().addOnGlobalLayoutListener( new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        //Do something here since now you have the width and height of your view
                    }
                });
    

    Here is a small example on how you would do it in your classes:

    My Steering Class:

    public class Steering {
    
        private Bitmap mBitmap;
        private int mWidth;
        private int mHeight;
    
        public Steering(Bitmap bitmap, int width, int height) {
            this.mBitmap = bitmap;
            this.mWidth = width;
            this.mHeight = height;
        }
    
        public Bitmap getBitmap() {
            //reescaling from anddev.org/resize_and_rotate_image_-_example-t621
            final int imageWidth = mBitmap.getWidth();
            final int imageHeight = mBitmap.getHeight();
            // calculate the scale -
            float scaleWidth = ((float) mWidth) / imageWidth;
            float scaleHeight = ((float) mHeight) / imageHeight;
            // createa matrix for the manipulation
            Matrix matrix = new Matrix();
            // resize the bit map
            matrix.postScale(scaleWidth, scaleHeight);
    
            Bitmap resizedBitmap = Bitmap.createBitmap(mBitmap, 0, 0, imageWidth, imageHeight, matrix, true);
            return resizedBitmap;
        }
    
    }
    

    My Activity

    public class MainActivity extends Activity {
    
        MyView mView;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mView = (MyView) findViewById(R.id.viewid);
            OnGlobalLayoutListener listener = new OnGlobalLayoutListener() {
    
                @Override
                public void onGlobalLayout() {
                    final int width = mView.getWidth();
                    final int height = mView.getHeight();
                    final Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.android);
                    //image from anddev
                    final Steering steering = new Steering(bitmap, width-50,height-50);
                    mView.setObject(steering);
                }
            };
            mView.getViewTreeObserver().addOnGlobalLayoutListener(listener);
        }
    
    }
    

    and my View Class

    public class MyView extends View{
    
        Steering steering = null;
    
        public MyView(Context context) {
            super(context);
        }
    
        public MyView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public void setObject(Steering steering){
            this.steering = steering;
        }
    
        final Paint paint = new Paint();
    
        @Override
        protected void onDraw(Canvas canvas) {
            canvas.save();
            if(steering!=null){
                canvas.drawBitmap(steering.getBitmap(), 0, 0, paint);
            }
            canvas.restore();
        }
    
    }
    

    You can use this for a normal view or a surfaceView, either way works.
    Sorry if the answer is a bit too long 😛

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

Sidebar

Related Questions

Can someone please explain why this doesn't work? MyClass myClass1 = new MyClass(); object
Can any one tell, how to get the result of LINQ query contains group
Want to play a file that on path like this /var/spool/sound/dog.wav present on server.
I would like to see if I can avoid a lengthy switch or if
me new to android developments, tell me how to handle screen orientation in android.
I have used this : var watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default); Where Hgh means GPS
i am newbie to Ext Js..i am working on ruby on rails... can any1
still fairly new to matlab, picked up this data analysis code from someone and
Can anyone let me know how can we change the value of kendo combobox
Can I order my users in the database, so I don't have to say

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.