Select single View drawn on Android canvas - java

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.

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.

how to make the square into arc borders?

I want to make this kind of arc square border in android. The idea is to set border of the image and only inside picture will show up like the mask. outer picture will be cropped or won't be displayed
I have tried many ways but not able to solve this issue.
Any help would be appreciated!
You can draw that piece by piece using drawLine() and drawArc() functions from the Canvas.
You can check how to use this here.
Try this
You can achieve it by Canvas.
Please go through to draw the shape you want
You can use the library android-shape-imageview
<com.github.siyamed.shapeimageview.mask.PorterShapeImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:siShape="#drawable/shape_bg"
android:src="#drawable/face"
app:siSquare="true"/>
Output on using Library looks like
You can get different shapes by replacing siShape in ImageView

Android : One background for two fragments

I try to make an app but I have an issue with the background.
The above image shows what should look like the app. Every rectangle of color is a fragment and the red one already have the correct part of the background. But now I try to figure out how can i make the green rectangle background respect this rules :
Image width needs to match parent
The image needs to keep his ratio
Image need to "stick" the bottom (in that way the upper part shows in the red rectangle won't appears in the green one)
In the green fragment I have a ConstraintLayoutand an ImageView like above :
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/ss_background_blur"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>
The #drawable/ss_background_blur is my PNG image.
Thx
As Neo Shen pointed out in the comments, the only thing you need to do is set the background property of the container element to the drawable resource (the image).
If you have an Activity which only contains three Fragment elements, then you can set the background of the main Activity layout.
You can see and clone a quick demo I put together here:
https://github.com/sipox11/full-background-android-app
This is the result:
Note: I did it very quickly so obviously the tabs are not real tabs and the top bar is not a real nav bar, but you get the idea.
In this case the background image is actually below the tabs area, but if you want it to start on top of it, you can wrap the top and middle fragments within a LinearLayout and set the background image to that container.
Hope it helps,
Cheers.
Set the background of the rootlayout to green and add android:layout_margin="2dp" to the imageview .
Hope this helps.

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.

How to create a 10x10 grid in Android?

I am creating a small Battleship game and for the Android UI I was hoping to create a 10x10 grid of image buttons or something (anything easy).
I was following the tutorial provided at this link
My problem is that I can't get the images to display in a proper 10x10 format. Using the GridView, I set the num_columns to "10" in the XML but there is no such attribute for the number of rows.
As phone sizes differ, is there a way to have a 10x10 grid auto fit any screen?
By following the steps in the above link I can display 100 clickable images but my problem is with the formatting. Here's a screenshot of said result:
I've tried changing values in XML to scale the images down but cannot figure out how to. Apologies for lack of code snippets but I really haven't altered much from the sample given in the above link other than the vertical and horizontal spacing.
Would appreciate any enlightenment, even if it's just to say I am I going about this the wrong way completely.
You can achieve this with a table layout : http://www.mkyong.com/android/android-tablelayout-example/
GridViews, just as ListViews have been designed to contain any number of rows.
But I really doubt you want to use views in your game and not only use a canvas, draw anything and detect clicks without relying on views built-in mechanisms. It's more work but you end with something that will make you much more free to do what you want (for instance not having to deal with the number of rows of a container...)
go to this link and try 2nd example Custom Adapter example.
just change android:numColumns="auto_fit" to android:numColumns="10".
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridView1"
android:numColumns="10"
android:gravity="center"
android:columnWidth="100dp"
android:stretchMode="columnWidth"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</GridView>
You can probably do what you want with a GridLayout, but not with a GridView (which automatically scrolls vertically). Using a GridLayout, set all the buttons to have 0dp layout width and height, and set their layout gravity to fill.

Categories

Resources