Android Show Images while maintaining Aspect Ratio - java

I am working on an App which shows images (by capturing using camera or choose from gallery) in some activity. The problem I am facing is showing images of different sizes. The actual size of image is very big even those which are captured by camera of same phone are large in size. So if I show them in an ImageView the get stretched.
I have seen in some apps like Facebook, when image is displayed in Full Screen mode, it maintains the aspect ratio I guess. The whole image, whatever its size may be, is shown in the screen but it doesn't get stretched. The image automatically adjusts its height and width as per the need. How to achieve the same functionality?
How to make ImageView which would be able to adjust its height and width (maximum already defined) as per the need.
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:scaleType="matrix"
android:src="#drawable/defaultimage"
android:padding="10dp" />

You need a different android:scaleType in your layout. The ImageView.ScaleType documentation lists the different ones that are available. It sounds like you probably want to change matrix to fitCenter in your example.

Related

Admob ad with 100% screen width and custom(90dp) height

Im trying to display an admob ad which completely covers the width of screen (like SMART_BANNER), and 90dp constant height.
using this code, i can set the height to 90dp, but how can i make the ad width fill the width?
AdSize adSize = new AdSize(deviceSpecificWidth, 90);
According to this, these are the standard sizes of SMART_BANNER
Screen width x 32|50|90
can i force the smart banner to only use 90dp height in every device?
I would suggest to use the newly introduced AdMob Adaptive Banners. They don't require adjustments on your side and they utilize all the width available and keeping the aspect ratio (which was a problem before).
Embed them into a FrameLayout like following
<FrameLayout
android:id="#+id/adFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center|bottom" />

how to create button with an icon?

I'm creating a app which contains many button, Each button's background is different. I have created button background from Photoshop. Now my question is what should be the size of background. I have kept my button height and width as wrap content. When i use android:background:"#drawable/background_img" (image size 30X30), the buttons look like broken (like blurry). What is the best size for background which suits for all screen.
Use a ImageButton set width and height as you like and then set android:scaleType="fitCenter" and android:adjustViewBounds="true". Try to use bigger image input than 30x30 (its to small), try 96x96 and then try to fit it to height and width you want.
A few days ago i had the same problem, when i gave 32x32 images were blurry, but when I try 64x64 or 96x96 (dont't remember) and android:scaleType="fitCenter" and android:adjustViewBounds="true" it looks so much better.

Android, ImageView, unwanted padding

I'm using something like that to present my logo or user profile picture:
<ImageView
android:id="#+id/logo"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#drawable/circle_bg"
android:src="#drawable/account_circle_grey"
android:layout_gravity="center_horizontal" />
#drawable/circle_bg:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#color/colorWhiteOpacity"/>
<size
android:width="100dp"
android:height="100dp"/>
</shape>
and #drawable/account_circle_grey is just an image I took from MaterialUI(the black 192x192) and used Final-Android-Resizer.
The problem is that I get something like that:
Basically the problem is that when the user clicks and pick a picture, I use glide to load the picture to replace the default #drawable/account_circle_gray but as you can see the padding is gone:
The picture takes the full space, as it should!, what I wanted to add is padding="5dp" and that will give a bit of padding between the background and the Glide loaded user picture, the problem is that there is already a weird padding to #drawable/account_circle_grey, I noticed however that not all drawables get the padding, when I chosen an old picture I used as a full page background I didn't get the padding, maybe I can force android to pick bigger picture, why do I get this padding?
Edit:
To clerify I used this tutorial to try all possible(scaleType+adjustViewBounds) values but nothing worked.
This is likely happening when you're using images that are physically smaller than the size of your ImageView. You can scale the image to fill the space or use a larger image. A mix of both is likely the best solution.
To achieve the former of the two, try this:
<ImageView
android:id="#+id/logo"
android:layout_width="90dp"
android:layout_height="90dp"
android:background="#drawable/circle_bg"
android:src="#drawable/account_circle_grey"
android:layout_gravity="center_horizontal"
android:scaleType="fitXY"
android:padding="5dp" />
Notice that I reduced the width and height by 2x the padding. That is because adding padding to a view increases the entire view's size by the size of the padding. In this particular case we're adding 5dp to the left, to the right, to the top, and to the left. This means we must subtract left and right from the width, and top and bottom from the height.
set the scaleType of your ImageView to centerFit or you can add only high dpi drawable to your drawables
thanks for the answers, the problem was so obvious but I made it complicated, although I used many "account_circle" images, what I didn't think was that they all comes with a transparent padding, so if the image is 192x192 for instance it's actual size is about 182x182 with 5px transparent padding on each side, anyway the simple solution is to open it on photoshop, go to Image>Trim and that's it.

How to keep the aspect ratio of a foreground image?

I currently have a FrameLayout with a background and a foreground image. The foreground image has transparency using alpha in order to see through and watch the background below.
The foreground image needs to mantain the same aspect ratio of the original bitmap because consists of a trasparent circle in a black screen, much like a spyglass effect. I'm looking for any resolution compatibility.
Unfortunately, I am incapable of achieve this and the foreground shows always its circle stretched.
This is part of the XML code:
<FrameLayout
android:id="#+id/layout_parent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/background"
android:foreground="#drawable/foreground"
android:foregroundGravity="center|fill_horizontal"
>
I am also trying to solve my issue providing two diferent images, one with 4:3 ratio and the other with 16:9 ratio, each one in a different drawable folder with the long and notlong qualifiers.
/drawable-long/foreground.png (16:9 aspect)
/drawable-notlong/foreground.png (4:3 aspect)
Still with no success.
Do you have any idea of how to get this effect?
Thanks for your time.
Change this attribute in your FrameLayout:
android:foregroundGravity="center"
This will place the foreground drawable in the center of the FrameLayout, not changing its size.
"center|fill_horizontal" will place it in the center and stretch its width to fill the FrameLayout width, which is clearly not what you want.
I don't believe you can change the scale type of either the foreground or the background. What I would recommend is to either:
Place an extra ImageView in your FrameLayout to serve as your background, and use the src and scaleType attributes to handle the image and how it scales.
Since it's a simple black background with just a transparent circle, you could convert that into a 9-patch, making sure to stretch it evenly around the circle. The circle will not scale, but the black area will extend to fill the view.
Number 1 is my personal recommendation if the circle needs to scale as well.

Android: How to prevent image from being scaled in ImageView or ImageButton?

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.)

Categories