• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How to fetch bitmap named from string?

How do i get a certain resource based on a name given in the string _bitmap?

Code:
public AnimatedObject(Context _context, Point _pos, int _rows, int _columns, String _bitmap)
{
    setUsesAnimation(true);
    pos = _pos;
    rowsInSheet = _rows;
    columnsInSheet = _columns;
    String bitmapstring = _bitmap;
    bitmap = BitmapFactory.decodeResource(_context.getResources(), R.drawable.bitmapstring);
    bitmapHeight = bitmap.getHeight() / rowsInSheet;
    bitmapWidth = bitmap.getWidth() / columnsInSheet;
}

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:
public AnimatedObject(Context _context, Point _pos, int _rows, int _columns)
{
    setUsesAnimation(true);
    pos = _pos;
    rowsInSheet = _rows;
    columnsInSheet = _columns;
    bitmap = BitmapFactory.decodeResource(_context.getResources(), R.drawable.fire);
    bitmapHeight = bitmap.getHeight() / rowsInSheet;
    bitmapWidth = bitmap.getWidth() / columnsInSheet;
}
 
Last edited by a moderator:
Is there any problem with using the ID of the resource as a parameter?

Code:
public AnimatedObject(Context _context, Point _pos, int _rows, int _columns, int _bitmapId)
{
    setUsesAnimation(true);
    pos = _pos;
    rowsInSheet = _rows;
    columnsInSheet = _columns;
    bitmap = BitmapFactory.decodeResource(_context.getResources(), _bitmapId);
    bitmapHeight = bitmap.getHeight() / rowsInSheet;
    bitmapWidth = bitmap.getWidth() / columnsInSheet;
}

And you construct your AnimatedObject like this:

Code:
AnimatedObject myObject = new AnimatedObject(......., R.drawable.fire)
 
Back
Top Bottom