1. Download our Official Android App: Forums for Android!

Apps How to fetch bitmap named from string?

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

  1. BringBackJiraiyaSensei

    Thread Starter
    Rank:
    None
    Points:
    16
    Posts:
    18
    Joined:
    Apr 3, 2017

    Apr 3, 2017
    18
    5
    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
    Rank:
     #21
    Points:
    1,238
    Posts:
    4,828
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    4,828
    6,306
    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
    Rank:
    None
    Points:
    16
    Posts:
    18
    Joined:
    Apr 3, 2017

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

Share This Page

Loading...