ImageButton LayoutProblem with setImageDrawable - android

I'm trying to set an image of an ImageButton at runtime. It should fill the width of the screen and use as much space in height as it needs to scale correctly.
The ImageButton is declared as followed:
<ImageButton
android:id="#+id/map_plan_topview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:onClick="onMapImageClicked"
android:background="#0000"
android:scaleType="fitXY" />
Now I'm trying to set the image with:
ImageButton topView = (ImageButton) findViewById(R.id.map_plan_topview);
Drawable d = Drawable.createFromPath(path);
topView.setImageDrawable(d);
The Images width is scaled with fill_parent, but is not correctly stretched in height.
If I use
topView.setImageResource(R.drawable.button_where_citymap); instead, everything works fine, but I do want to set the source from a filepath at runtime.
What am I doing wrong?

fitXY stretches the image without respecting proportions. If proportions matter, then use center_crop, or center_inside
You can set the ScaleType in code as well:
ImageButton topView = (ImageButton) findViewById(R.id.map_plan_topview);
Drawable d = Drawable.createFromPath(path);
topView.setScaleType(ScaleType.CENTER_CROP);
topView.setImageDrawable(d);

You should use "SetImageResource" in place of "setImage Drawable"...!

Related

Android set ImageView's width as a ratio of another element (given the id)

I have an ImageView:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/pbc"
android:layout_below="#id/ryc"/>
Is it possible to set it's width to be a percentage of that of another element of id "#id/ryc" just like i've done so in android:layout_below ?If so, how?
Thanks
You will need to set the width in your activity, like the following code (just change yourPercentage to the percentage you want)(I have added an id to your imageview, but you can change it):
XML:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/pbc"
android:id="#+id/pbcImageView"
android:layout_below="#id/ryc"/>
Activity:
float perc = yourPercentage / 100;
ImageView imgView = (ImageView) findViewById(R.id.ryc);
ImageView pbcImageView = (ImageView) findViewById(R.id.pbcImageView);
pbcImageView.setWidth(imgView.getWidth() * perc)
Hope it helps!
Possibly, you can put them into LinearLayout and assign proportional weights.
If your design doesn't allow to do so and the size of the views needs to reflect dynamical changes, try overriding onSizeChanged() of one of the views and resize the other in this method. Otherwise giacomoni's answer is the most suitable.

Setting a Bitmap to a ImageView

I have downloaded a Bitmap from a WebService, and set it to an ImageView. The ImageView has a set height of 120dip and a width set to FILL_PARENT, so it fills the complete width of the screen. Now i want to stretch the Bitmap to fit the width of the ImageView, but the if the height of the Bitmap is greater then the height of the ImageView, i want the remainder of the image to flow off the top and bottom of the ImageView. So in way, like i have a frame infront of the image and the only portion of the image visible is the contents of the frame. How can i do this ?
See Android Developers on ImageView ScaleType
You are interested in the scale_type enum. Use CENTER_CROP.
Here is complete example:
<ImageView android:layout_width="fill_parent"
android:layout_height="120dp"
android:layout_gravity="center"
android:src="#drawable/eureka"
android:scaleType="centerCrop">
</ImageView>
Here is also a cool visualization of all possible options for ImageView.

Android, Change Image size in ImageView

For Android, How can I change the image size in an ImageView in a layout?
My image is jpeg format.
<ImageView
android:id="#+id/imageView1"
android:layout_margin="20dp"
android:src="#drawable/stop"
android:layout_width="50dp"
android:layout_height="50dp"/>
Just change width and height attribute.
You can also set Params using LayoutParams
ImageView yourImageView= (ImageView)findViewById(R.id.imageId);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(30, 30);
//width and height of your Image ,if it is inside Relative change the LinearLayout to RelativeLayout.
yourImageView.setLayoutParams(layoutParams);
There are a few ways to change the size of an image in an imageView in XML.
Scale the image by pulling around the edges of the imageView in the Graphical Layout (this is not really advised since the Graphical Layout sometimes adds undesired properties into the XML).
Apply a weight to the imageView (this requires the parent object to have specified a weightSum), causing the image to scale according to the screen size. This seems to be the most reliable way to gauge how your layout will look on many different sized screens.
Simply adjust the weight and height in the imageView (as others have mentioned).
<ImageView
android:id="#+id/imageViewId"
android:src="#drawable/icon"
android:layout_width="xxdp"
android:layout_height="yydp"/>
where xx is the width in pixels and yy is the height in pixels.
(give integer values as you desire the size to be)

Android ImageView will not match_parent width

I have an ImageView that is 1x20px. The image is a .9.png. I am trying to use match_parent but when I run the app on my device, it is not stretched to fill the width of the screen. Here's the code in the XML for the ImageView
<ImageView
android:id="#+id/dividerBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/ic_bar"
android:layout_gravity="center_horizontal"
android:scaleType="centerInside" />
Any ideas on how I can get the ImageView to cover the entire width of the screen?
If it's a 9 patch it needs to be set as the background attribute of the imageview for it to scale properly. Right now it's trying to scale the image as it would any other image using the scaleType tag that you provided.
Try setting
android:background="#drawable/ic_bar"
and remove the src attribute
Additionally, if you are using the imageView only to display the 9-patch you can switch to using a regular View instead of the ImageView. As long as you set the background attribute you will be good to go.

How to create an ImageView that fills the parent height and displays an Image as big as possible?

I have an ImageView that is defined in the following way:
<ImageView
android:id="#+id/cover_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_below="#id/title"
android:layout_above="#id/divider"
android:adjustViewBounds="true"
android:src="#drawable/image_placeholder"
android:scaleType="fitStart"/>
Now after downloading a new bitmap I change the drawable. The image now appears in the top left corner of the ImageView. Is there a way to have the image fill up the whole height that is possible and then adjust the width of the view to enable scaling the image without changing the ascpect ratio?
The image fills up all the space on a standard screen but on a WVGA Resolution the image takes only about half of the actual height of the ImageView.
If I'm understanding you correctly, what you need to use is the centerCrop scaleType. fitStart scales the image proportionally, but neither the width nor height will exceed the size of the view, and the image will, as you said, have a top|left gravity.
Using centerCrop scales the image proportionally, but causes the shortest edge of the image to match the size of the view, and if there is additional data on the long side that does not fit, it is simply cropped off. The gravity is, of course, center. The below worked for me:
<ImageView
android:src="#drawable/image_placeholder"
android:id="#+id/cover_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="centerCrop"
/>
You can change scale type to fitXY via call to
imageView.setScaleType(ScaleType.FIT_XY);
simply do it in xml like
android:scaleType="fitXY"
Basically the answer here is that there is no predefined value for what you are trying to achieve. The solution is to create a Matrix that fits to your needs and call setImageMatrix(matrix) on your ImageView.

Categories

Resources