Android - CardView becomes a diamond shape because radius too large - java

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.

Related

Place image's over image

Im working on an android app were you can setup your own Ring.
Now I want to give the user a preview of the ring, when they selected multiple rings.
Here a preview of how it should look like
The setup of how i thought to make it is this:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="5dp">
<ImageView
android:id="#+id/generator_ring_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#e7e7e7"/>
<FrameLayout
android:id="#+id/generator_ring_addon_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</FrameLayout>
I'm going to add the addon rings programmatically by just creating a new ImageView in the code.
Now the hard part of this is to place the image over the other ring, and have the view the same on every screen size..
Is ther anybody who can help me getting the correct code for placing the image on the correct position on every screen size?
thanks for reading
EDIT:
the moment I am selecting the addons rings the Base Rings is already visible, so I can get the height of the image
User Paint API: https://developer.android.com/reference/android/graphics/Paint.html
https://developer.android.com/reference/android/graphics/Canvas.html
Like that you can add whatever pictures you want, programatically, move them, even create animations if you want.
Also you will have just a view, which contains the canvas. So no need to have convoluted FrameLayouts into FrameLayouts.
Example on how to draw images on a canvas.
draw object/image on canvas
Using canvas you can get (x,y) coordinates of the ring.
First create canvas with ring image and than use canvas.draw() to draw another image on base image.

Prevent ImageButton from resizing with source image

I have a problem with my ImageButton because it appears to be resizing together with the source image. This is quite problematic because I'm using a TableLayout with equally weighted TableRows to achieve a uniform grid layout but for some reason, that particular ImageButton is resizing itself with the image and making that entire row appear bigger than the others even if they all have the same layout weight.
empty src:
https://gyazo.com/b50ab6d7afb6608db0505d701a2a40c9
with src, adjustViewBounds=true
https://gyazo.com/e2a2dae581d1fae4503b6e130ced776d
with src, adjustViewBounds=false
https://gyazo.com/2623f08fcffea586d5b7917332ae7291
XML Tag:
<ImageButton
android:id="#+id/stat_analyze"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/sp2_calc_button"
android:onClick="btnClicked"
android:src="#drawable/statistics_white"
android:adjustViewBounds="true"
android:scaleType="fitCenter"/>
Update:
So far, I've tried replacing the source image which is a png with a smaller version and with an android vector resource replacement but so far, I've still been stuck with the same results. I guess that means the only way to do this really is to lock the layout height. I've tried programatically setting the height at runtime but I still get the same result.
Update:
I tried once again, resizing the image to a fixed value. I noticed, however, that this particular button does not take up the whole area until it reaches a height quite far beyond the intended height. What I mean by this is for example, I set it to 60dp, the entire row will already begin to take up more space even if the button itself hasn't matched the parent height yet. I don't know why this is happening as this is the first time I've seen this happen. Can someone help me with this?
Have you tried the scaleType fitXY attribute:
android:scaleType="fitXY"

Select single View drawn on Android canvas

I'm new to Android and discover lots of things. But now I'm quite stucked.
I want to make a hexagonal board game, with tiles. I found many tutos and finally decided to draw as mush hexagons as I want to.
I created a 'Hex' class extending from View, add some variables and so on. Here is what I put in my main XML layout:
<flocoolb.app6.Hex
android:layout_width="wrap_content"
android:layout_height="wrap_content"
flocoolb:r="50"
android:id="#+id/h01"/>
<flocoolb.app6.Hex
android:layout_width="wrap_content"
android:layout_height="wrap_content"
flocoolb:r="50"
flocoolb:x="75"
flocoolb:y="43.30127"
android:id="#+id/h02"/>
<flocoolb.app6.Hex
android:layout_width="wrap_content"
android:layout_height="wrap_content"
flocoolb:r="50"
flocoolb:x="150"
android:id="#+id/h03"/>
r=size, x and y are offsets on canvas.
Here is what I get on my activity:
Main activity screen
But now I want to select single Hex. For example I want to select to top left one, or the bottom right one. I add OnClickListener on each of them, but actually the size of the view is the whole screen.
For example, if I change the background color of the view, it change that color on the whole screen. Is it possible to have the view limited to the drawn lines?
I hope you will understand what I mean.
Many thanks :)
Although,I don't understand you very much.But I suggest you to override onTouch Method of your view.And implement your function in onTouch method.

Android Pixel Perfect Alignment

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.

ImageViews in circle

I want to put 20 ImageView objects in LinearLayout rotated so that they form a circle. Is this possible and how, and is this the right approach for creating circular layout of ImageView objects?
Thanks
I'm not conversant with the android API, but it doesn't look like you can bend a LinearLayout into a circle. Rotating it should only change it's angle (horizontal versus vertical versus diagonal).
Probably, you're going to want to do something based off of an AbsoluteLayout or RelativeLayout, then perform geometry-related math to find out the positions the various elements need to be at.
Ideally, for future re-use, you should actually create a new CircularLayout that could be applied to containers. You want the API to require a List of some sort of the elements (plus optionally padding), then have the layout perform the necessary math.
LinearLayout can only present child elements either vertical or horizontal. You can position the ImageViews to form a circle if you use a RelativeLayout, but rotating them will include lots of calculations
consider using the canvas and draw paint functions
Try this:
Your XML
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/myImageontoolbar"
android:padding="5dp"
android:layout_width="10mm"
android:layout_height="10mm"
android:layout_gravity="left"
app:srcCompat="#android:drawable/sym_def_app_icon" />
Your Gradle
compile 'de.hdodenhof:circleimageview:2.1.0'

Categories

Resources