How to keep the aspect ratio of a foreground image? - java

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.

Related

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.

Options to put corners in Android ImageView

I'm trying to put corners im my ImageView and I find these "workarounds":
1 - Take the Bitmap soure and paint then
2 - put a second ImageView on my layout and use shape+corners as its source
It's not possible take the image view and put a corner around then?
In my app i have a List with a lot of ImageView (the Bitmap used in this ImageView's is programmatically) and I want to put corners in every ImageView.
There is any other option?
You can create a selector that contains shape with rounded corners, and then apply it as the ImageView's background using xml.
Look at this answers here
You can also do it via code by using PorterDuffXfermode. PorterDuffXfermode uses alpha compositing that will allow you to create and intersection (things of mathematical sets here) between the Canvas you get from onDraw() and an off screen Canvas you will have to create. You will use one of the PorterDuff.Mode flags to tell the framework you want to only render the pixels that intersect from the two Bitmaps in each Canvas.
More on Alpha Compositing

How do I keep my splash screen's background from stretching?

How do I go about setting the background for a splash screen that does not stretch, and maintains a density-independent size? Here's a visual of what I'm trying to go for:
I have a large image file (1000x1000px) of the background. I want the background to maintain its density-independent size, with the excess just not visible, so that it will be displayed correctly on various screen sizes. I also want the center of the image anchored to the center of the screen. (the image above is just a mockup, the focal point should be at the center of the image)
How do I do this?
You have a link here in stackoverflow that might help.
You can use an ImageView as your background (otherwise backgrounds of any view will just stretch).
And use it as the background of your ImageView. Your ImageView can then have the layout_width and the layout_height as wrap_content.
Then, set the ScaleType as android:scaleType="centerCrop".
Note that this ImageView has to be the first element of your layout. Otherwise it will just hide everything in your layout.

Positioning images to fit all size screens

I'm having a problem with positioning images,I need to position a lot images over another larger background image.
An image Like this
I have tried Absolute but it does not keep the position of the image say I wanted to put a clip art image of a board pin over the background image and need it pointing at a sun and when it's clicked I get a popup dialog,
but then when I change the size of the emulator screen the clip art image is not at the same position I wanted it on the background image.
I first tried just putting the clip art on it with a image editor and used onTouch Listener but that didn't work out when I changed the size of the screen with the x and y coordinates. And tried Absolute Layout and that doesn seem to keep the position.
any ideas would help me big time thanks
AbsoluteLayout is deprecated, so it's probably best to use relative layout alongside with dp.
You could use relative layout so you can use layout_below="#id/view1", android:layout_toRightOf="#id/view2", and android:layout_toLeftOf="#id/view3". You can also use android:layout_marginLeft="10dip", android:layout_marginRight="10dip", android:layout_marginTop="10dip", and android:layout_marginBottom="10dip" to move the views left/right and up/down relative to their current positions. There is also ALIGN_PARENT_LEFT, ALIGN_PARENT_RIGHT, ALIGN_PARENT_TOP, and ALIGN_PARENT_BOTTOM. Click here for more properties.
You can manually calculate visible width and height of the image by using its drawable's getIntrinsicHeight() and getIntrinsicWidth() and then set Image's scaleType to FIT_XY (this saves image's ratio, and also makes its size correct, while FIT_CENTER does not).

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