Im working on a initials drawable, sort of like gmail has.
I want this drawable to scale automatically to image view size (which will be hardcoded in xml, but multiple variants), therefore I dont want the initials drawable to have hardcoded size as well, which is to me seems is what getIntrinsicWidth() does.
Is there a way to do this? Is there way then for drawable to get the image view size, to do its calculations for rendering?
Thanks
Vector drawables can scale automatically by default. Setting android:width="24dp" for example in your drawable xml doesn't force it to be this size, it's just a default value.
Set the ImageView to the size you want, and set your drawable as the image resource. It will fill the available space.
In layout xml:
<ImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:src="#drawable/your_drawable"/>
Or in java:
ImageView iv = findViewById(R.id.your_image_view);
iv.setImageResource(R.drawable.your_drawable);
Mess with android:scaleType if you want different crop behavior.
Related
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
I will be displaying an image in an ImageView that will always be much higher and wider than the size of the screen. Is there a simple way to scale the image down so that it fills the ImageView exactly?
Try setting up the scale type in the xml, i believe it scaleXY is the one you are looking for scale types
<ImageView
android:scaleType="scaleXY"
android:src="#drawable/my_image"/>
If you're loading the view in XML, the android:scaleType parameter should do the trick.
http://developer.android.com/reference/android/widget/ImageView.html#attr_android:scaleType
I like to use a customized (Radio)Button and I know I can use whatever image for the background with the following code inside the xml:
<RadioButton
...
android:button="#drawable/myDrawable"
../>
However: I want to use three radiobuttons, that fill the whole row. But the drawable that I use for the button, doesnt get resized depending on the sreensize, so if the drawable is too big only parts of it will show, and if it is too small, not the whole screen will be filled. If I would use ImageViews I could change the ScaleType like so:
ImageView myImageView = (ImageView) findViewById(R.id.myimageview);
myImageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
But there is no such property for other Views, than the imageView. So how can I accomplish the same for (Radio)Buttons?
You will have to provide images with different sizes (see Supporting Multiple Screens)
I am a puzzled about using src or background for an ImageView.
I know the former means the content of this ImageView and the latter means the background of the ImageView.
But how to decide which one to use? I don't see the difference.
All views can take a background image.
The src to an ImageView has additional features:
different scaling types
adjustViewBounds for setting bounds to match image dimensions
some transformations such as alpha-setting
And more that you may find in the docs.
when you use android:background, image will be set to fit on ImageView area(i.e according to width and height of ImageView). It doesn't matter if the image is smaller or larger than ImageView.
when you use android:src, then image will display in its original size. No
automatic scaling, adjustments will happen.
Note: Using android:src, we can get additional benefit of adjustViewBounds property
If you set an image to be the background of your ImageView, then the image will scale to whatever size the ImageView is. Other than that, src is a foreground image and background is a background image. Pretty much as it implies.
The difference between XML attribute src and background in ImageView:
The background will stretch according to the length given by the ImageView component, and SRC will hold the size of the original image without stretching. SRC is the picture content (foreground), BG is the background, can be used at the same time.
In addition: ScaleType only works on SRC; BG can set transparency, for example, in ImageButton, you can use Android:scaletype to control how the image is scaled, sample code as follows:
<ImageView
android:id="#+id/img"
android:layout_height="60dip"
android:layout_width= "60dip"
android:src="#drawable/logo"
android:scaleType="centerInside"
android:layout_centerVertical= "true"/>
Feel free to ask doubt if you get any.
I have an ImageView that initially has a Bitmap Image:
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
By default that imageview scales the bitmap to fill the width that is what I want.
But the problem is that when I change the source of the ImageView to an AnimationDrawable, that is build by several images of the same size of the original, then animation isn't scaled at all and returns to the original.
How can I do that scale the animation drawable in the same way that is scaled the bitmap?
I dealt with this programmatically by using ImageView setPadding(). I calculated the difference between the ImageView height and width and what I wanted to display and set my own padding so that the usable size of the ImageView exactly matched my animation.
It's brute force and not very flexible but I've not seen any better solution posted and this issue has come up many times.