Best way to animate programatically created bitmaps in android - android

I have a board game app that creates a lot (hundreds) of bitmaps, and constantly changes them as the game progresses. These bitmaps are created, not loaded from a resource... so they have no R.id to refer to.
I would like to animate some of them, e.g. a bitmap moving from one loction to another when a player taps to move it. What is the best way to do this?
Note, this is 2.1 and the bitmaps are drawn on a canvas via a matrix translate.

Make this function to get image from drawable folder
private Bitmap getImageBitmap(){
int resID = getResources().getIdentifier(uri, "drawable",
"a.b.c");
Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), resID);
return mBitmap;
}
For set the image , where do you you want>
ImageView imageview01.setImageBitmap(getImageBitmap("name of bitmap");
if you want more about bitmap. then you may try this links.
http://developer.android.com/reference/android/widget/ImageView.html
drawable getResources().getIdentifier problem
setImageURI / setimagebitmap to fetch image from net and display in image view

Related

LRUCache BitmapDrawable

I'm using an LruCache to cache a lot of small BitmapDrawables used throughout my app. The problem is that the sizes differ for the different places I use the images.
I set the bounds when I retrieve the drawable from the cache before setting it to the ImageView to have the correct size.
When I set the bounds, the drawable resizes on the other places as well.
How can I get around this issue without using drawable.getConstantState().newDrawable()? Creating a new drawable from the cached drawable is very slow when scrolling a listview.
The same images are used in DynamicDrawableSpans where I can't set the bounds on the span itself, only on the drawable directly.
Will it be wise to have the same drawable image cached for the different contexts in seperate Caches?
I got it to work correctly.
I changed the LruCache type from BitmapDrawable to Bitmap. This way the cache only stores the Bitmap image and not the drawable bounds as well.
When I want to set the bitmap I convert it to BitmapDrawable
Drawable d = new BitmapDrawable(getResources(), bitmap);
Then I set the bounds for the newly created drawable instance
d.setBounds(0, 0, sizex, sizey);
This way each instance of the Bitmap is a seperate Drawable with its own Bounds.

Create a bitmap from drawable resource filepath

I would like to create an image chooser from my drawable resources. I think I have found an "easy" way to do this. I was going to create a listview that displays the drawables , and the user would be able to pick one. I would then pass back , the path to this drawable resource, the paths would be hard-coded in. I then would set a bitmap to this drawable path. I was trying to do a simple test of this with just hard coded values, (no listview yet) but I do not get a visible bitmap back. I think it "works" , but maybe the bitmap is to small or not set to visible?
bm2 = BitmapFactory.decodeFile("/gds2/res/drawable-hdpi/feel_like_a_sir.png");
foreground_imageview.setImageBitmap(bm2);
Try to load your drawable in this way
bm2 = BitmapFactory.decodeResource(getResources(), R.drawable.feel_like_a_sir);
foreground_imageview.setImageBitmap(bm2);

Android BitmapDrawable recycling/re-use

In the code below I've got two bitmaps (I omitted the code for creating them as that's not relevant to my question) and I also have an ImageView in my layout. I make the ImageView display the first bitmap as a drawable, and then make it display the second bitmap, again as a drawable.
I'm aware that bitmaps can be recycled, my question is related to the "new BitmapDrawable" part, as I can't figure out exactly what a BitmapDrawable is. Is it just a reference or does it use up memory each time one is created? In other words, does the BitmapDrawable I create for bitmap1 need to be deleted/recycled before I create another BitmapDrawable for bitmap2?
Thanks.
Bitmap bitmap1,bitmap2;
...assume bitmap1 and bitmap2 contain valid bitmaps...
// get imageview
ImageView iv = (ImageView)findViewById(R.id.my_imageview);
// make the imageview display bitmap1
iv.setImageDrawable(new BitmapDrawable(getResources(),bitmap1));
// now make the imageview display bitmap2
iv.setImageDrawable(new BitmapDrawable(getResources(),bitmap2));
You should keep bitmaps as long as you need them. It is costly to create new bitmaps. GC will fire if enough memory not available and your app will stall for that time.
See this for efficiently displaying bitmaps
http://developer.android.com/training/displaying-bitmaps/index.html

Android: how to simply draw a bitmap on another bitmap

I'm new to Android dev and I'm having a hard time trying to do something which seems obvious to me: drawing little images on top of a bigger image.
Let's say that I have a 500x500 image and I want to draw icons at different locations. Icons are png files that I load with:
Bitmap img =
BitmapFactory.decodeResource(getResources(),
R.drawable.idIcon1)
My "background image" is a LayerDrawable.
Then, I am totally lost... Do I have to create a canvas ? How to draw on my "background image" my icons at different positions?
int positionLeft=0;
int positionTop=0;
Bitmap newBitmap =Bitmap.createBitmap(backgroundBitmap.getWidth(),bitmap.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(newBitmap);
canvas.drawBitmap(backgroundBitmap, positionLeft, positionTop,null);
positionLeft=100;
positionTop=100;
canvas.drawBitmap(iconBitmap,positionLeft,positionTop,null);
imageView.setImageBitmap(newBitmap);
You're making simple things difficult. Just use a layout with android:background attribute, and then add ImageViews dynamically with the necessary bitmaps inside.

multiple images in a one single image

I want to create a bitmap / image which has many images like "Collage" which has more then one images in a single picture.
I have stored all my images in a grid view but now i want to create a single image from all those images. And even i want to make few images click able
so what can be the road map to do this ? any sort of help / example will be helpful.
reference image
Bitmap pic1 = BitmapFactory.decodeResource(getResources(), R.drawable.pic1);
Bitmap pic2 = BitmapFactory.decodeResource(getResources(), R.drawable.pic2);
Bitmap bg= BitmapFactory.decodeResource(getResources(), R.drawable.background);
Bitmap out1 = Bitmap.createBitmap(bg) ;
Canvas comboImage = new Canvas(out1);
comboImage.drawBitmap(pic1, 10f, 20f, null);
comboImage.drawBitmap(pic2, 30f, 40f, null);
out1 will have pic1 & pic2, with a background image bg.
To create a single image from multiple images look at using Canvas. You can put bitmaps (and drawables) on a canvas. You can commit the changes and then push then to a single bitmap that you can then use. As far as making certain sections clickable after making it one single image, I will leave this up to someone else to explain, I am not worked directly with the ontouch() functions.

Categories

Resources