How to set an image in the background of custom view - android

How can one set image from uri to the background of custom view without causing the stackoverflow error. As I couldn't find any appropriate answers, I hope to find it here.

Get that image via Picasso / Glide libraries as a Bitmap
Convert Bitmap to drawable
Drawable drawable = new BitmapDrawable(getResources(), bitmap);
Set drawable to your custom view
yourCustomView.setBackground(drawable);

Related

Android avoid image scalling in Imageview for Image Mapping like Html

Hi Am trying to implement image mapping as like html in android. For that i referred this project. Its work good when i access the image from drawable-nodpi folder. But when i access from other drawable folder the image get scaled as per the android concepts. Here i used drawable folder for checking purpose only. Actually i get image from back end service.
I also tried with bitmap by setting the no density.
Bitmap bm;
bm=BitmapFactory.decodeResource(getResources(), R.drawable.blueprint);
bm.setDensity(Bitmap.DENSITY_NONE);
mImageMap.setImageBitmap(bm); //mImageMap is customized view which extends Imageview
But it's not working. Am also tried as per this post. Its not working.Is there any way to load image to image view without scaling?
you can use Bitmap.createScaledBitmap with the orignal mesaures of the picture..
int width = orignal bitmap width
int height = orignal bitmap height
Bitmap bm;
bm = getBitmap... from network
bm = Bitmap.createScaledBitmap(bm,width,height,true);
this will create a image scaled properly and just place it in the ImageView.

Converting bitmap to drawable in Android

I'm downloading the image from server and storing it in bitmap object. I want to set this image as background for button. But the button doesn't have the property setImageBitmap. So is there anyway I can set the background of button with the downloaded bitmap image? Like by converting the bitmap to drawable? Sorry, I'm new to Android, please bear with my mistakes (if any).
P.S : I want to use button control only. Because I want some text at the bottom of each buttons and I'm creating these buttons dynamically.
The best way to convert a Bitmap to drawable in android is as follows,
Drawable drawable = new BitmapDrawable(getResources(), bitmap);
where bitmap is the name the Bitmap. Then set the drawable as your Button background as follows,
btn.setBackground(drawable);
N.B: Without specifying getResources() as the first argument, you may experience inconsistent image sizing across different screen densities.
for more info refer this
http://developer.android.com/reference/android/graphics/drawable/BitmapDrawable.html
Simply use BitmapDrawable.
Drawable drawable=new BitmapDrawable(contact_pic);
Convert Bitmap to BitmapDrawable like this
Drawable drawable = new BitmapDrawable(getResources(),your_bitmap);
and then set it as background to button as ,
button.setBackgroundDrawable(drawable);
P.S. You can also do the same inline as ,
button.setBackgroundDrawable(new BitmapDrawable(getResources(),your_bitmap));
This is how to do it, I've used several times:
Drawable drawable = (Drawable)new BitmapDrawable(Bitmap);
Button button= new Button(this);
button.setBackground(drawable);

set bitmap to background of ImageView with imageView.setImageBitmap method

I have an ImageView. I am using imageView.setImageBitmap to set my image as background to ImageView. But it sets my image to ImageView's source (i.e. android:src) , but it doesn't set my image to ImageView's background (i.e. android:background).
When I use imageView.setImageBitmap, it looks like as if I used imageView.setImageResource not imageView.setBackgroundResource.
How can I handle this if I want to set my image to background using imageView.setImageBitmap. I know by I can do this by making custom ImageView. But is it possible without custom ImageView? If its possible, please let me know how to do it.
I have tested and it's done
I think you are getting Bitmap
So you have to convert Bitmap to BitmapDrawable
like
BitmapDrawable ob = new BitmapDrawable(getResources(), bitmap)
then just set bitmap with below function
imageView.setBackground(ob);
by this way you can do it..
Try following code to set your own bitmap as background.
Bitmap bitmap = ...;
ImageView imageView = ...;
Drawable drawable = new BitmapDrawable(getResources(), bitmap);
imageView.setBackground(drawable);
try this..
Create drawable using bitmap & use setBackgroundDrawable(Drawable d) method for imageview.
Drawable d = new BitmapDrawable(getResources(),bitmap);
imageview.setBackgroundDrawable(d);
Please Use the following line to set image background.
imageview.setBackground(getResources().getDrawable(R.drawable.uporder));
Here uporder is an image resource present in your drawablefolder.
I prefer to use this because is not deprecated and it works in the lowers versions of android.
ImageView imageView;
Bitmap bitmap;
Drawable drawable = new BitmapDrawable(getResources(),bitmap);
imageView.setImageDrawable(drawable);
call setBackgroundDrawable withe BitmapDrawable param

How to destroy a drawable inside an ImageView if we don't need it?

This question is related with Do we have to explicitly recycle the bitmap if we don't need it?.
There is an ImageView have a drawable, when user clicks a button, it will assign a new drawable to the ImageView.
Do we have to destroy the old drawable belongs to the ImageView, and how?
Drawable oriDrawable = imageView.getDrawable()
// set callback to null
oriDrawable.setCallback(null);
// get the bitmap and recycle it
((BitmapDrawable)oriDrawable).getBitmap().recycle();
Is the code above correct? What's the best solution?
You could try using something like:
Drawable drawable = imageView.getDrawable();
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
Bitmap bitmap = bitmapDrawable.getBitmap();
bitmap.recycle();
}
Where imageView is your ImageView.
Original answer here.
Is this just a general question or are you running out of memory? I would not make this optimisation until you really have a problem.
In general if you are loading bitmaps from drawable folders that are not large (large as in megabytes) then you should not really run into a problem.
First you need to make sure the assets you are loading are optimal for where you display them, for example there is no point in setting an ImageView to an image thats 1024 x 1024 in size if the area you display the image is a size of 64x64.
Breaking the bitmap budget is usually caused by loading in images of an unknown size or just simply getting the image sizes wrong as described above, swapping an ImageView frequently will generally not give you an issue with optimal sized images.
There is a great article in Android Training that covers loading bitmaps optimally http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
Hope that helps

How to prevent an image from being resized in Android

I am writing an application on android. I am downloading an image from the server and trying to set it as an ImageView background using setBackgroundDrawable.
Drawable drawable = Drawable.createFromPath(path)
imageView.setBackgroundDrawable(drawable);
It works but the image is resized (it is getting smaller). Does anyone have an idea about how to set an image view background without changing its size?
Edit:
I solved the problem by using:
File imgFile = new File(path);
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
imageView.setImageBitmap(myBitmap);
imageView.setScaleType (ImageView.ScaleType.MATRIX)
Set image scale type to Matrix it will scale using the image matrix when drawing.
Use these two properties:
android:scaleType
android:adjustViewBounds
Study about them in : ImageView

Categories

Resources