1. Check out our companion app, Forums for Android! Download from Google Play

How to fetch bitmap named from string?

Discussion in 'Application Development' started by BringBackJiraiyaSensei, Apr 21, 2017.

  1. BringBackJiraiyaSensei

    Thread Starter
    16

    Apr 3, 2017
    18
    2
    16
    Male
    How do i get a certain resource based on a name given in the string _bitmap?

    Code (Text):
    1.  
    2. public AnimatedObject(Context _context, Point _pos, int _rows, int _columns, String _bitmap)
    3. {
    4.     setUsesAnimation(true);
    5.     pos = _pos;
    6.     rowsInSheet = _rows;
    7.     columnsInSheet = _columns;
    8.     String bitmapstring = _bitmap;
    9.     bitmap = BitmapFactory.decodeResource(_context.getResources(), R.drawable.bitmapstring);
    10.     bitmapHeight = bitmap.getHeight() / rowsInSheet;
    11.     bitmapWidth = bitmap.getWidth() / columnsInSheet;
    12. }
    13.  
    it works when i do like this, but i would much rather that i could set the name of the resource in the constructor so i can reuse the class for other animations.

    Code (Text):
    1.  
    2. public AnimatedObject(Context _context, Point _pos, int _rows, int _columns)
    3. {
    4.     setUsesAnimation(true);
    5.     pos = _pos;
    6.     rowsInSheet = _rows;
    7.     columnsInSheet = _columns;
    8.     bitmap = BitmapFactory.decodeResource(_context.getResources(), R.drawable.fire);
    9.     bitmapHeight = bitmap.getHeight() / rowsInSheet;
    10.     bitmapWidth = bitmap.getWidth() / columnsInSheet;
    11. }
    12.  
     

    Advertisement

    #1 BringBackJiraiyaSensei, Apr 21, 2017
    Last edited by a moderator: Apr 21, 2017
  2. LV426

    LV426 Illegitimi non carborundum
    Moderator
    1,238

    Oct 16, 2015
    4,242
    5,460
    1,238
    Male
    Software developer
    South West of England
    Is there any problem with using the ID of the resource as a parameter?

    Code (Text):
    1.  
    2. public AnimatedObject(Context _context, Point _pos, int _rows, int _columns, int _bitmapId)
    3. {
    4.     setUsesAnimation(true);
    5.     pos = _pos;
    6.     rowsInSheet = _rows;
    7.     columnsInSheet = _columns;
    8.     bitmap = BitmapFactory.decodeResource(_context.getResources(), _bitmapId);
    9.     bitmapHeight = bitmap.getHeight() / rowsInSheet;
    10.     bitmapWidth = bitmap.getWidth() / columnsInSheet;
    11. }
    12.  
    And you construct your AnimatedObject like this:

    Code (Text):
    1.  
    2. AnimatedObject myObject = new AnimatedObject(......., R.drawable.fire)
    3.  
     
  3. BringBackJiraiyaSensei

    Thread Starter
    16

    Apr 3, 2017
    18
    2
    16
    Male
    Thanks this was exactly what i was looking for! :D
     
    LV426 likes this.

Share This Page

Loading...