Android Pixel Perfect Alignment - java

I'm creating a bar at the top of my app much like the one above.
The graphics are embedded into the background and the numbers that represent the level etc is using TextViews.
As you can see the TextView has to be pixel(dp) perfect to fit in the center of the white bar thingy. With some margins or padding i can achieve that.
But how do games like that keep the same pixel perfect across all devices?
I know about resource qualifiers and such but some devices within the same bucket have different sizes.
Is there a good way or trick to achieve the perfect centering across most devices?

In general Android will handle the relative sizes across devices as long as you do everything in relative units and include 9-patch or images for each screensize.
For your question of how to handle images attached to a TextView, you can accomplish this a few ways.
A TextView will allow you to attach a drawable to it on the top, bottom, left, and right. You can do this with code using the various setCompoundDrawable* methods. For example, this will add a drawable to the right and left of the TextView.
txtScore.setCompoundDrawables(R.drawable.imageLeft, null, R.drawable.imageRight, null)
You can also do this in XML with the drawableLeft, etc properties. You may need to mess with padding and bounds for the drawables.
<TextView
android:id="#+id/txtScore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:drawableLeft="#drawable/imageLeft"
android:drawableRight="#drawable/imageRight"
android:drawablePadding="10dp"
android:singleLine="true"
android:text="125"/>
There are a number of different options in code as well if you look over those setCompoundDrawable type APIs for the TextView.
Your other option is to create your own View that does all of the work for you, but may be overkill if you only use it in one spot.
Again, you will really want to consider a 9-patch image or create an image for each screensize in the drawable folders. This will allow Android to determine which image to use for each device.

Related

Android - CardView becomes a diamond shape because radius too large

I'm using android.widget.CardView in my application.
In my Android 11 device, when radius is larger than the View, it will automatically adjust to half the size of the View.(I don’t know which version this was adjusted)
In the lower system version(I use Android 7.1 to test), CardView will turn into a diamond shape when the radius is larger than the View.
Anyone know why and how to solve? Thx
Very often, the Glide library is used to load images. This library can help round the edges as follows
Glide.with(context)
.load(url)
.circleCrop()
.into(imageView)
Make friends with this library and you will be happy. Not only does it help the image to take shape, but it also uses memory correctly https://github.com/bumptech/glide
P.S: In my opinion, using CardView is very limiting for you.
You could use as a root element for example a Constraint Layout and as a background use a rounded shape as you like (rectangle) and place your image inside the layout.
To hold the shape I often using the next attribute (an example is below):
app:layout_constraintDimensionRatio="W,1:2".
In your case ratio must be 1:1 because it is circle not ellipse, then you can use
app:layout_constraintDimensionRatio="W,1:1" or app:layout_constraintDimensionRatio="H,1:1"
I'm not sure if this will work in CardView, but it works well in ConstraintLayout.
Here is one of the common and most simple method to make the image get a circular border using card view in android in XML
first create a card view inside your XML and inside that create a image view with exactly 1dp less then the card view in both width and height and then try to adjust the cardCornerRadius as per your requirements, that's it it is as simple as that.
I'm using androidx.cardview.widget.CardView here.
<androidx.cardview.widget.CardView
android:layout_width="70dp"
android:layout_height="70dp"
app:cardCornerRadius="35dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true">
<ImageView
android:id="#+id/rounded_profile_img"
android:layout_width="69dp"
android:layout_height="69dp"
android:scaleType="centerCrop"/>
</androidx.cardview.widget.CardView>
Thank you hope y'll like it and it is helpful for everyone.

Straightforward way to place text on a specific location in an image?

Overhead schematic of Docks map in Modern Warfare
Overhead schematic of Docks with one callout name
I'm creating an app that will display names - "callouts" - for various specific areas of each map in the video game Modern Warfare. I want to be able to place the text (or a placeholder) in the same location, regardless of scale, transformations, etc. Is there a way to do this besides getting the (x,y) coordinates of each location in an image editor, then using those coordinates in my layout? That method seems extremely tedious because there's 10 - 20 callouts per map, and approximately 20 maps! I also want to avoid adding the text graphically so that I can change it as necessary in Android Studio.
If I place TextViews visually in the [Design] tab in Android Studio, will they stay in the same position relative to the background ImageView, regardless of scaling, transforms, etc.? Please advise, and I'll be happy to clarify if necessary. Thanks!
I think I've found the solution I'm looking for, folks:
Within a ConstraintLayout, have an ImageView to hold the map graphic, and TextViews for each callout label.
Constrain each TextView to the Start, End, Top, and Bottom of the ImageView.
Drag the TextViews to the desired location in the [Design] tab of the layout editor. This will automatically adjust the Horizontal and Vertical Bias, which is essentially a percentage of the parent's width and height, relative to the left and top of the parent.
In my preliminary testing, this seems to preserve the location of each callout label relative to the map, which is what I was hoping for. If anyone has anything to add, please do so!

Splash screen which fills the whole samrtphone screen, but not crop

I want to have a splash screen, something like a full picture, which doesn't crop in heigth or width on different smartphone screens.
Now I achieved a splash screen with android:scaleType="fitXY", but now the image is cropped on top or bottom or if the devices screen size changes to another aspect ratio it is cropped on the left and right.
What do I have to do? I've already read the android developer article Supporting Multiple Screens, but I don't get it how to achieve this.
A simple picture in the middle of the screen is just simple to get, but a picture which fills the screen is hard to get. Can you help me pls?
you should use center_crop per this purpose. From the doc
Scale the image uniformly (maintain the image's aspect ratio) so that
both dimensions (width and height) of the image will be equal to or
larger than the corresponding dimension of the view (minus padding).
There is no way to create one single asset and expect it to do not be cropper and to do not create black areas when the application is deployed in different screen sizes.
The android platform is designed to work dynamically with multiple screen sizes that any manufacture can change at any time, including new resolutions that you haven't thought about it yet.
Android can specify minimums for screen hight/width categories in which your resources will fall, but those are generics.
In order to use them, you will have to specify qualifiers in your drawables and create a different splash screen for every qualifier, as for example if you use drawable-w420dp, all the resources there will be used when the screen has a minimum width of 420dp (notice that are not pixels)
So you have two options:
You can use one single splash image and design margins of that image flexible enough in order to cope with the image being cropped in certain cases. You can play with different scaleTypes in your ImageView and take as a reference this website http://etcodehome.blogspot.co.uk/2011/05/android-imageview-scaletype-samples.html even though as commented before, "center-crop" will be your best shot.
You can programatically use a specific image for a specific resolution.
2.1 Put in the assets directory, all the splash images that you want for all the specific resolutions or aspect ratios that you want to use
2.1 Get the screen size of the device with Get screen dimensions in pixels
2.2 Now you can load from the assets the image that you want dynamically
Use the below code
android:layout_width="match_parent"
android:layout_height="match_parent"
which will fill the entire screen.
Try Using Width and Height of image to "match_parent"

ImageView, why different size?

First thing, I know many people have asked same thing, but this one is nearly same but I have few more questions.
I have an image of 48px x 48px, so if I set this image in ImageView with
layout_width="wrap_content"
layout_height="wrap_content"
then image looks bigger but if I use following fixed size, it gets smaller
layout_width="48dp"
layout_height="48dp"
I think it's now showing properly(not pixelated ?) with 48dp, but why it got bigger if I use wrap_content ? And if the image size through wrap_content is correct then whats the point of using dp(if it make image smaller) ?
48dp (or 48dip) stands for "Density Independent Pixel". This means that the physical size of the image as displayed on the screen of an Android should be the same regardless of screen density and size. In order to support this, Android adjusts the actual pixel size displayed based on screen density. Your findings of different sizes will vary from device to device.
The conversion of dp units to screen pixels is: pixels = dps * (density / 160). For example, on 240 dpi screen, 1dp would equal 1.5 physical pixels. Using dp units to define your application’s UI is highly recommended, as a way of ensuring proper display of your UI on different screens.
This probably happens because you don't explicitly set the scaleType. Set your scaleType in XML as follows and you should be fine:
android:scaleType="center"
Try this
<ImageView
android:id="#+id/seprator2"
layout_width="48dp"
layout_height="48dp"
android:scaleType="fitXY"
android:src="#drawable/your_image" />
And if still it shows small then your image must contains alpha around it.
If the image is larger than the view, then you could scale it using:
android:scaleType="center"

Apply 9-patch drawable to Button, keeping default padding

I'm attempting to customise the look of a Button using a 9-patch drawable with no padding defined within in (no pixels on the right or bottom inside the 9-patch editor).
Is there a simple way to apply a 9-patch drawable to a button and maintain the button's default size. For example, when I apply the 9-patch to the Easy button shown below, it is a different size to the other default styled buttons, which share the same layout, except for the 9-patch style.
Mdpi layout:
Hdpi layout:
So basically, is there a way to make the Easy button the same size as Medium and Hard, on both mdpi and hdpi screens, without making a new 9-patch for each density, and working out the correct padding.
Thanks in advance for your help, I can provide more information if required.
Well after some more research it seems that it isn't easily possible to use one 9-patch drawable for all screen densities.
The link below states you need different drawables for each density:
Custom Color Buttons for Android
3.1. Create images 9-patch images for the different colors that you need and put them into drawable-hdpi and drawable-mdpi (yes, you will need two versions if you want your buttons to look good on different devices).
If anyone knows any different though, your help would be much appreciated.

Categories

Resources