I'm using a ImageView with a fixed size for adding an icon to a menu. In my application, I use it a lot of times, but on this ImageView the Layout Params seem to not work.
Unlike the others ImageViews, in this case, I'm using a template directly, but I think that's not the problem.
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="1dp"
android:layout_height="1dp"
android:scaleType="fitCenter"
android:src="#drawable/ic_menu_moreoverflow_normal_holo_dark"
android:contentDescription="ICON" />
Its been used in code as follows.
ImageView iview =(ImageView) View.inflate(context, R.layout.icon, null);
Theoretically, It should resize automatically the image, however, the images continues with the original size, although the size was 1dp. Where is the problem? Thanks a lot!
android:scaleType="fitCenter"
you should remove it.
Change the ImageView assignment of the resource from src to background.
Related
I have image in drawable, which needs to work as the background. It needs to stretch to fill the screen..
I know how to stretch the image full screen using java code.. but in XML itselt how can do this.
android:scaleType="fitXY" will stretch the image to fit the full size of the ImageView
Try with this property:
android:scaleType="fitXY"
Here you can find other constant values for resizing.
Try to implement your ImageView like this and change the ic_launcher to your own image name which you are trying to add.
<ImageView
android:id="#+id/mImageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:src="#drawable/ic_launcher" />
Hope this will be helpful. Thanks
I prefer to use scaleType="centerCrop". This way the image isn't stretched out of its original proportions.
I have an image that I am downloading from the internet in my android application. I do not want this image to be resized/scaled. Is there anyway I can stop the image from being scaled?
Here is my XML entry for the ImageView:
<ImageView
android:id="#+id/conditionImage"
android:scaleType="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/textMain"
android:contentDescription="#string/noaaLogoDescription" />
Here is my code for showing it in the image view:
BitmapFactory.Options bmpOptions = new Options();
bmpOptions.inScaled = false;
URL url = new URL("URLPATHTOIMAGE");
ImageView conditionImageView = (ImageView)findViewById(R.id.conditionImage);
InputStream content = (InputStream)url.getContent();
conditionImage = BitmapFactory.decodeStream(content, null, bmpOptions);
conditionImageView.setImageBitmap(conditionImage);
I have tried changing the scaleType in the ImageView XML element to use matrix, center, and centerInside. My last try was using the above code to set inScale to false. Does anyone have any other ideas?
The odd part is this is not happening in the emulator.
Have you put all images in simple "drawable" folder. I mean without extending hdpi,mdpi etc.
Try set
android:layout_width="wrap_content"
android:adjustViewBounds="true"
EDIT:
Not sure why, but changing
conditionImageView.setBackgroundDrawable(getResources().getDrawable(R.id.conditionImage));
to
conditionImageView.setImageDrawable(getResources().getDrawable(R.id.conditionImage));
solves my problem.
Adnan, using your example of giving the image view a fixed width and height and also removing the scaleType=centerImage fixed the issue.
This is a simple problem. The scaling happens when the ImageView is a lot larger than the rendered image itself. To avoid this ugly pixelated scailing, just "wrap content" the imageview and you should get your original image quality.
I resolved this problem by hardcoding height of imageview ( as I know it in advance) and then set the scaleType as cropCenter
see the below code for more clearity.
<ImageView
android:id="#+id/iv"
android:layout_width="match_parent"
android:layout_height="75dp"
android:scaleType="centerCrop" />
I have an Android Activity with an ImageView.
I put the ScaleType of the imageview on CENTER_CROP, so the image itself would not stretch.
<ImageView
android:id="#+id/imgView"
android:layout_width="fill_parent"
android:layout_height="135dp"
android:scaleType="centerCrop"
android:src="#drawable/capture" />
The image that's loaded at first does what I expect, it's cropped to the center, filling the whole ImageView.
Now, if I set the image of the ImageView programatically,
imgView.setImageBitmap(b)
imgView.setScaleType(ImageView.ScaleType.CENTER_CROP);
The picture gets displayed, but it's completely stretched out to fill the ImageView...
AdjustViewBounds has no effect what so ever, nor does anything else.
The difference between the working XML and the not-working java code is that in XML I'm putting the image as a resource, and the code is putting a Drawable in the ImageView from a web-service...
Does anyone have an idea to the solution?
I am not sure but android:scaleType="fitXY" or android:scaleType="fitCenter" may be the solution of your problem.
I solved this by using the original image from my camera-intent. For some reason, all thumbnail bitmaps were stretched out and in landscape mode.
I am displaying a tiny png drawable resource in an ImageView of larger dimensions than the original image. This is normal and what I want by the way :)
When the ImageView is displayed, the image is blurry, because of the scaling method used I suppose.
I would like to achieve an effect similar to :
http://www.41post.com/4241/programming/android-disabling-anti-aliasing-for-pixel-art
where the original image is upscaled without antialiasing.
Is there a way to achieve that directly with an ImageView of certain width and height (in dips) and a drawable, without having to use an intermediate Bitmap?
<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/image"
android:antialias="false" />
You need to create an drawable, copy the code above on an xml in the drawable folder, then on your layout instead of using your image as source use this xml. This way you disable the antialias for the image.
Edit: doing this in code.
BitmapDrawable draw = new BitmapDrawable(R.drawable.image);
draw.setAntiAlias(false);
imageView.setImageDrawable(draw);
Did you try turning off antialiasing in the layout?
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:antialias="false" />
I found this great thread describing how to "eat the cake and have it too", i.e. use image for a Button instead of ImageButton (which doesn't allow SetText(), resizing, etc.).
This is achieved by using the View attribute:
android:background="#drawable/bgimage"
The only problem with this is that it stretches the image to fit the button size.
Short of hard-coding a fixed button size (in pixels!), is there a way to tell Android not to stretch the background image at all and either crop or pad it?
You can create an xml bitmap and use it as background for the view. To prevent stretching you can specify android:gravity attribute.
for example:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/dvdr"
android:tileMode="disabled" android:gravity="top" >
</bitmap>
There are a lot of options you can use to customize the rendering of the image
http://developer.android.com/guide/topics/resources/drawable-resource.html#Bitmap
You should use ImageView if you don't want it to stretch.
Background images will always stretch to fit the view.
You need to set it as a Drawable to force the image aspect to the object.
Otherwise, if you are sticking with the Button idea, then you will need to force the scaling in the button to prevent the image from stretching.
Code:
onCreate(Bundle bundle) {
// Set content layout, etc up here
// Now adjust button sizes
Button b = (Button) findViewById(R.id.somebutton);
int someDimension = 50; //50pixels
b.setWidth(someDimension);
b.setHeight(someDimension);
}
Simply using ImageButton instead of Button fixes the problem.
<ImageButton android:layout_width="30dp"
android:layout_height="30dp"
android:src="#drawable/bgimage" />
and you can set
android:background="#null"
to remove button background if you want.
Quick Fix !! :-)
I am using an ImageView in an RelativeLayout that overlays with my normal layout. No code required.
It sizes the image to the full height of the screen (or any other layout you use) and then crops the picture left and right to fit the width. In my case, if the user turns the screen, the picture may be a tiny bit too small. Therefore I use match_parent, which will make the image stretch in width if too small.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/main_backgroundImage"
android:layout_width="match_parent"
//comment: Stretches picture in the width if too small. Use "wrap_content" does not stretch, but leaves space
android:layout_height="match_parent"
//in my case I always want the height filled
android:layout_alignParentTop="true"
android:scaleType="centerCrop"
//will crop picture left and right, so it fits in height and keeps aspect ratio
android:contentDescription="#string/image"
android:src="#drawable/your_image" />
<LinearLayout
android:id="#+id/main_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</RelativeLayout>
I had the same problem: you should only use a 9-patch image (.9.png) instead of your original picture.
Serge
Use draw9patch... included within Android Studio's SDK tools. You can define the stretchable areas of your image. Important parts are constrained and the image doesn't look all warped. A good demo on dra9patch is HERE
Use draw9patch to change your existing splash.png into new_splash.9.png,
drag new_splash.9.png into the drawable-hdpi project folder
ensure the AndroidManifest and styles.xml are proper as below:
AndroidManifest.xml:
<application
...
android:theme="#style/splashScreenStyle"
>
styles.xml:
<style name="splashScreenStyle" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowBackground">#drawable/new_splash</item>
</style>
I had a background image, not big in size, but with weird dimensions - therefore the stretching and bad performance. I made a method with parameters Context, a View and a drawable ID(int) that will match the device screen size. Use this in e.g a Fragments onCreateView to set the background.
public void setBackground(Context context, View view, int drawableId){
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),drawableId);
bitmap = Bitmap.createScaledBitmap(bitmap, Resources.getSystem().
getDisplayMetrics().widthPixels,
Resources.getSystem().getDisplayMetrics().heightPixels,
true);
BitmapDrawable bitmapDrawable = new BitmapDrawable(context.getResources(),
bitmap);
view.setBackground(bitmapDrawable);
}
Here's a version of Santosh's answer for programmatically-created buttons, without the need for a separate XML configuration:
Button button = new Button(getContext());
Bitmap backgroundBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.my_button);
BitmapDrawable backgroundDrawable = new BitmapDrawable(getResources(), backgroundBitmap);
backgroundDrawable.setGravity(Gravity.CENTER); // also LEFT, CENTER_VERTICAL, etc.
backgroundDrawable.setColorFilter(new PorterDuffColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP));
button.setBackground(backgroundDrawable);
I included the ColorFilter line since that works a little differently from buttons with a normal background image.
You can use a FrameLayout with an ImageView as the first child, then your normal layout as the second child:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/background_image_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/your_drawable"/>
<LinearLayout
android:id="#+id/your_actual_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</FrameLayout>
The key is to set the drawable as the image of the button, not as a background. Like this:
rb.setButtonDrawable(R.drawable.whatever_drawable);
One can use a plain ImageView in his xml and make it clickable
(android:clickable="true")?
You only have to use as src an image that has been shaped like a button i.e round corners.