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.
Related
I tried following:
Created a frame layout containing that relative layout and made foreground /background color transparent. But I got transparent layout above my Relative Layout But Cannot see the textviews of Relatives layout which was initially of dark black color.
Also tried dynamically by creating a drawable with transparent color setting it to foreground of frame layout containing that relative layout.
getting still the same result.
Any help would be greatly appreciated ?
I solved it by just setting correct Alpha.
view.setAlpha(x);
I had initially set up the value of x as 90 which has no effect on the view.
After setting up the value of x as 0.9f (float/double), it worked.
Is it possible to change an imageview's size after initializing but keep the size the image has after initiliazing? Or is there maybe a way to change an image's size without changing the imageview.
Edit: I need to be able to crop the image from bottom to top without affecting the size of the imageview, so that the bottom part of the image is cut off.
You mist likely looking for android:scaleType of ImageView. See docs.
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.
In a ViewGroup I want to put two ImageViews overlapping: The imageview A have a transparent background and some drawing across the image, the imageview B.
I want both them to be at the same place and have the same size (but can't fix their size in the XML because if the image at ivB changes the ivA should resize to match the new size.
Since the ivA source image does not change I tried ivA.getWidth() and ivA.getHeight() for setting the ivB size but their value are 0,0 in the onCreate() method.
Is there some "easy" way of doing this?
Override the ImageView and in onLayout() you can get proper width or height after calling super.onLayout(). Try setting values in the place.
Try getting the dimensions in onWindowFocusChangedListener.
An easier way would be putting the ImageViews in a FrameLayout (which is built to put Views on top of the other), and resize the FrameLayout instead.
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).