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);
Related
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.
I'm trying to set a color as an image to an ImageView that exists inside a custom notification. I can give it predefined colors by writing this line:
remoteViews.setImageViewResource(R.id.imageView, R.color.Green);
But i want to be able to give it hex color values, and set that as the color. I tried setting the color as the background of the ImageView, but i couldn't get access to it, and the only options i have to access the ImageView are these:
remoteViews
.setImageViewResource()
.setImageViewBitmap()
.setImageViewIcon()
.setImageViewUri()
Is there a way to pass in a color by parsing the hex value to any of these?
I think if I was able to create a bitmap and set the bitmap to the image view, it could be done, so i wrote the code bellow to create the bitmap:
int[] colors = {Color.parseColor("#64DD17"), Color.parseColor("#D32F2F")};
Bitmap bitmap = Bitmap.createBitmap(colors, 10, 10, Bitmap.Config.RGB_565);
, but i get a java.lang.ArrayIndexOutOfBoundsException. I tried with one element in the colors array too, but same error.
What is my error here?
The int[] is your image; so you create an image 2px wide, since that's the length of your array. However, you call createBitmap and specify that the image is in fact 10pxx10px; this is why the ArrayIndexOutOfBoundsException is thrown, as Android starts looking for pixels that aren't there.
Using Bitmap bitmap = Bitmap.createBitmap(colors, 2, 1, Bitmap.Config.RGB_565); would solve this problem, albeit generate a very tiny image!
Simple solution: generate png images(or use svg) to put inside the image view, instead of the color you want.
but, if u want to use that approach, you can create a colored bitmap using something like that:
Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
image.eraseColor(android.graphics.Color.GREEN);
In my project a bitmap gets filled with text and another bitmap, the method to add the text to the bitmap returns a BitmapDrawable.
I want to save this BitmapDrawable as a pdf file, so I can email the original bitmap, with text and other image(s) added.
For this, I'll need a regular Bitmap instead of a BitmapDrawable.
I can't seem to find any answers on this, because I don't have a reference to a drawable in a drawable folder, I just have a BitmapDrawable to work with.
Any ideas on this?
BitmapDrawables support a getter for the bitmap.
Bitmap bitmap = bitmapDrawable.getBitmap();
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
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.