I set the default size of an imageview as width=511 and height=385 .
Source of the imageview is from drawable.
Based on a dynamic value I need to change the size of the image view or the image fit in to it.
For example I want to change the size of the imageview or image inside it to width=140 and height=100
ImageView scale=(ImageView) findViewById(R.id.scaleimage);
If you set the width and height attributes of your ImageView to specific values, the system will automatically scale you images to fit.
Unless I don't understand your question, I think it is as simple as:
<ImageView
android:layout_width="140dp"
android:layout_height="100dp"
android:scale_type="fitXY"
/>
To set this in code, try:
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(140,100);
yourView.setLayoutParams(layoutParams);
Related
I am using imageview to show the photo. And i am showing photo in full screen. then my photo is streched. and my imageview height is 500 and width is 300 .How to solve this task.
Try changing the scaleType of the image view.
In xml,
android:scaleType="centerInside"
If center inside isn't what you want, see the other options at this link
https://thoughtbot.com/blog/android-imageview-scaletype-a-visual-guide
UPDATE: so i got the picture as a layout:http://gyazo.com/38a4c80dab36e15593a32f35951b9f7d
Now how do i make it so that i can put text on the picture. I want to make sure that the text doesnt go beyond the picture box. Thanks in advance!
Add a RelativeLayout where your picture is now. I suppose your picture has a fixed size (e.g. 60dp wide and 60dp high). Use that size for your RelativeLayout. Now add an ImageView as first child with both width and height set to match_parent. This ImageView will contain your picture. Add a TextView as second child, also with width and height set to match_parent. Now you have a TextView on top of an ImageView, both with the same size.
I have an ImageView and I want to to scale CENTER_CROP. If I set the image src and scale type in XML it works great. But I need to set the image in code. The following fails to scale correctly. Instead of center cropping it stretches the image to fill the width.
imageView.setBackgroundResource(R.drawable.my_image);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
I tried reordering these lines and I tried leaving out the setScaleType and leaving that to the XML. Neither helps.
setBackgroundResource sets imageView's background so it is normal behaviour that stretches...
Use setImageResource instead, it will set imageview's source so it will follow your scaleType definition.
and not forget that your ImageView must be NOT:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
but ONLY:
android:layout_width="match_parent"
android:layout_height="match_parent"
for proper transformation
Is there a way for me to tile an image along the right side of an activity view? The image i have is small and square.
i know you can tile the entire activity view like this:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/actual_pattern_image"
android:tileMode="repeat" />
but is there a way to restrain it? to make it just tile one image under the other along the right side?
i know i can do something like this:
android:layout_marginTop="5dip"
android:layout_marginBottom="5dip"
and just keep increasing the margin as i add another imageView below. my only fear with this approach is when scoll-bars come into picture. this is clearly a very static approach...
just a heads up - very new to android development...
If you know the width of your tile, you can create a bitMap whose width is the width of your tile, and whose height is the height of the full view. Then place this view on the right side of your activity view.
It would be easiest if your view had a relative layout that filled your view, and you had your bitmap align with the the right of the relative layout like this:
android:layout_alignParentRight="true"
How can I prevent my bitmap from being scaled automatically in an ImageView or ImageButton if the view or button is stretched using "fill_parent" or using "weight"?
This will be useful, for example, to create a 4-button toolbar at the top of the screen where the buttons are equally spaced, but the images inside the buttons keep getting streched even if I use scaleType="center", which should prevent scaling according to the doc, but it doesn't.
Any insight is appreciated!
Thanks,
I have found that when using android:background automatically scales the image you are using there to the size of the View.
I tried using the android:src to put the image in the view. The image did not scale, but I could not get the image to center relative to the size of the View.
So I tried making the whole Button it's own relative layout and this is what I used:
<RelativeLayout android:id="#+id/RelativeLayout01"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<ImageButton android:id="#+id/ImageButton01"
android:layout_height="fill_parent"
android:layout_width="fill_parent"></ImageButton>
<ImageView android:id="#+id/ImageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/cardback1"
android:layout_centerInParent="true"></ImageView>
</RelativeLayout>
The ImageButton is in the background and will always be the same size as the layout.
The ImageView however will always stay centered in the RelativeLayout and will not scale.
This way the RelativeLayout itself can grow and move and the Image on top of the button will always stay the same size, but the button will grow. The image will however shrink if the Layout becomes smaller than the image itself.
I think that's what you were looking for. There may be a better way to do this, but this is all I can come up with right now.
Simply modify your drawable, apply a larger transparent background. Say my drawable in hdpi is 41*48 px icon.png . I created a new image 60*60 px with a transparent background , copy-paste the previous icon.png and save under the same name. This way you don't need to touch your layouts .
Then you can check the button zone larger if you apply a non transparent android:background:
<ImageButton android:id="#+id/widget_open"
android:src="#drawable/icon_widget_arrow_up"
android:background="#ff777777"
android:layout_height="wrap_content"
android:layout_width="wrap_content"></ImageButton>
Apply the previous and new drawable to android:src , you should clearly see ImageButton area.
By the way, I am running the app in compatibility mode.
If the image is scaled, make sure you are not running your app in compatibility mode (for instance if you target Android 1.5/1.6 without supporting multiple densities and you run the app on Android 2.0.)