ImageView with setBackgroundResource and setImageBitmap - android

I have Imageview which i am creating it dynamically,now i am setting
imageView.setImageBitmap(bitmap);
imageView.setBackgroundResource(R.drawable.a);
What is happening is the setBackgroundResource image is stretching,as the bitmap size increases.
Why is it so?Is there any workaround?

A Background resource is designed to fill up the entire View, which is why it stretched when the contents of the View are increased in size.
A possible workaround would be to use a 9-patch drawable, which only stretches are specified by you.
Another workaround would be to scale your background as your normal View Contents increase, and reset it to the new background.

Related

Imageview Background PNG Issue when set Wrap content

I have a LayoutView in a ScrollView and want to set a my glass background PNG image that should be scaled automatically to wrap content.
In these layout view, there is a text view and the contents of this text area is variable.
Usually without any PNG background the size of layout view is not extended if isn't necessary to view all the text set, but when I set PNG background the layout view automatically enlarged to the size of the PNG if I set wrap content on layout view.
How to set a custom background image scalable to fit the size of layout view that doesn't influence the wrap content behavior?
As per your requirement i would suggest you- take your background image of Height= approx 50 pixel width= any
- convert this image into nine patch follow this link- http://developer.android.com/reference/android/graphics/NinePatch.html
by using nine patch images your background will be re sized/adjust as per the dynamic data.
Hope it helps.
Thanks
In my opinion, you have to:
Set scaleType of Image view = "fitXY"
Set layout_height, layout_width of layout = "wrap_content"

Getting LayoutParams.WRAP_CONTENT to ignore a large background image

I'm trying to make a LinearLayout that has height = LayoutParams.WRAP_CONTENT.
I am receiving an arbitrarily sized bitmap from a server to set as the background image for the LinearLayout.
How do I set the background image of the LinearLayout without resizing the view if the received bitmap is larger than the contents of the linear layout?
It would be nice if the background image could maintain its aspect ratio, and scale to match the width of the screen.
I tried overriding onMeasure as a temporary solution, but that just wound up biting me.
how about using an imageView inside your layout , which also has its adjustViewBounds set to true in order to keep aspect ratio?

How to resize an image without stretching?

I have a bitmap of 128*128 size. I want to make it of size 256*256. But I don't want to stretch the image. Instead, my old image should be placed at the center of the new image and remaining pixels should be filled with a certain color, for example white.
How can I do this?
ImageView have setScaleType(ScaleType type) method.
When ScaleType is http://developer.android.com/reference/android/widget/ImageView.ScaleType.html
Try to play with it. Good luck!!

What is the difference between ImageView.setBackgroundResource and ImageView.setImageResource?

I have seen these different approaches in setting images but I don't get the difference.
Why there two methods?
setBackgroundResource is for setting the background of an ImageView.
setImageResource is for setting the src image of the ImageView.
Given:
ImageView iv = new ImageView(this);
Then:
iv.setBackgroundResource(R.drawable.imagedata);
Will fit the image for the entire background. That means it will stretch the image to fill that background entirely even if the image size is too small.
imageView.setImageResource(R.drawable.imagedata);
Will occupy only the size of the image in ImageView.
For that you want to also set
android:layout_width="wrap_content"
android:layout_height="wrap_content"
for your ImageView. If the size of the image is smaller than the ImageView the remaining border will be left blank and the background will be shown.
SetBackdroundResource is for a drawable or color you want to set at the background of the imageview and your setImageResource is like to display on it.
so setImageResource is for add any resource to your imageview's front side. try this example and look at the difference. Android Gallery, ImageView Example
. This is a two layer effect,backside (setBackgroundResource) and frontside (setImageResource).
The method setBackgroundResource() belongs to all Views. The method setImageResource() only belongs to ImageView. You can set them both:
imageView.setBackgroundResource(R.drawable.sky);
imageView.setImageResource(R.drawable.balloons);
The setBackgroundResource() method will cause the image's width and height will be stretched to fill the size of the view. The setImageResource() method will let its image keep its aspect ratio.
My fuller answer is here.
setBackgroundResource sets the background image of an ImageView. The XML attribute is: android:background
setImageResource sets the image displayed in an ImageView. The XML attribute is: android:src

Disabling background image shrinking in Android

It seems background images are automatically shrunk in Android, for example, I use setBackgroundDrawable to set background of a view:
Drawable background = getBackground();
myView.setBackgroundDrawable(background);
Instead of shrinking, I want the image to be cropped to fit the screen size. How to do it? Thanks.
Finally I fixed this problem. I use BitmapDrawable instead of Drawable, since BitmapDrawable has method setGravity(int) which can set the gravity to position/stretch the object.

Categories

Resources