How I check an imageview ontop of another imageview or not - android

suppose i have two image view v1,v2.
imageview v1,v2;
v1=(Imageview)findviewbyId(R.id.view1);
v2=(Imageview)findviewbyId(R.id.view2);
How do I check that v1 ontop of v2 or not.

You can set onclick listener for both of it so that when the user clicks so that the top of the imageview only gets clicked. By this u can get which imageview is up top.

you may check by getting co-ordinates of the image by using this link
and can check which image is top and which is bottom with that co-ordinates

one suggestiton: get their location and height
minus them and configure if on top of

You can calculate it with the position and the width and height of the View.
Position: Use View.getLocationOnScreen() and/or getLocationInWindow().
Width/height: Use View.getWidth() and View.getHeight().

The view that is defined first in your xml layout will be drawn first. The next view will be drawn on top of the previous view if there is any.
By that principle, you can ask your frame/relative layout to check the ordering where the child views are ordered:
int indexV1 = layout.indexOfChild(v1);
int indexV2 = layout.indexOfChild(v2);
if(indexV1 < indexV2) {
// v1 is below v2
}

Related

How to animate individual view in constraint layout using chain?

I wanted to animate view as shown in the image. How to do it?
Click here to see the image
I have the following code but how to set the parameter current?
How Animation works?
When the user moves his finger over the list of icons, the height of the current icon, where the user has touched, is set to 400.
TransitionManager.beginDelayedTransition(layout);
View child = layout.getChildAt(current);
child.setMinimumHeight(400);
where current is of type int.
Shouldn't try to change a View height, margin or padding if you want to do an animation.
Do a scale animation instead on focus:
View child = layout.getChildAt(current);
child.animate().scaleX(2f).scaleY(2f).setDuration(400).start();
Scale to normal size on unfocus:
View child = layout.getChildAt(current);
child.animate().scaleX(1f).scaleY(1f).setDuration(400).start();

Move button back to origin after dragging/moving it

I have an ImageButton that I need aligned to the bottom of the screen, and when I click and drag it, it moves with my finger by a certain distance, beyond which it will not move further from the origin but continue following the direction my finger is from the origin.
When I let go, the button has to slide back to the origin.
What I have managed to do so far is to have the button follow my finger via this answer: Moving buttons via Touch
However, how should I get the origin of the button at initialization for it to bounce back after I release?
Originally I implemented the alignment of the button by having a android:layout_gravity="center|bottom" to place it at the bottom, but it somehow messes with the position of the button relative to my finger (It follows my finger movements but it's off center).
Hence, I used mButton.setY() and mButton.setX() to place it at the bottom of the screen, but it seemed hackish to use the screen dimensions to place it. Can't think of a better way.
You can set the width and height of your layout in XML using
layout_width and layout_height.
Then in your MainActivity, instantiate the layout (I will use LinearLayout as an example) and get its width and height.
LinearLayout l = (LinearLayout)findviewbyid(R.id.l1);
int height = l.getHeight();
int width = l.getWidth();
Now, whenever you release the button, you can set your its position in the following way:
mButton.setY(height - 50)
This will position the ImageButton 50 pixels above the bottom of your layout. You can also set the x position of the button as you want in the same way. Also, you should probably store height - 50 as a global variable (origin) that you can call from anywhere in your code.

Animation hides the image android

i used below single line code for animation to my image view in android.
Coding
v.animate().translationXBy(1000).setDuration(5000).start();
but i hides the image. i don't want to hide my image using the above source. any helps are much welcome thanks.
Try this:
v.bringToFront();
Add this before animating.
Hope this helps
how do you want to hide your image?
you can also hide it by set 0 in alpha attribute
v.animate().alpha(0).setDuration(500).start();
EDIT
To move the view right to left without hide it at the end
You can try to use v.animate.x() instead
Set the min X Position, its 0 since you want to move it to left without goes out of screen
int minXPosition = 0;
Set the value how much the view move and calculate the view new X position
int moveXBy = 100;
int newXPosition = (int)view.getX()-moveXBy;
Animate the view, but check the position first -> if its past your minXPosition, use the minXPosition instead
view.animate()
.x(newXPosition<minXPosition?minXPosition:newXPosition)
.setDuration(500)
.start();

How to get the view coordinates in relativelayout in android

I want to get position of view located inside a RelativeLayout ?
You can use getLocationOnScreen method to detect position of your view and layout on screen. Then just calculate relative coordinates.

Local Co-ordinate of Image view w.r.t to the root View

I have relative layout as a main View and it has few Image views which are place w.r.t to margin top and margin left. Now I want to check the imageViews opacity or transparent region at the pixel where touch is happened. Say I touched some where 533,240 .. I want to check whether that position is transparent or transculent or opaque.. Since imageview will always be square and i guess there will be part of image will be transparent but it should not be included in onTouch event thats why I aim for this that if i get the position of my touch w.r.t to local co-ordinate of the Imageview then I can give that pixel to the drawable.getTransparentRegion.container(int x , int y )
Explaining further.. .what I am trying to do is I am getting View on click or on touch.. From that view I am getting imageview ....and from that imageview I am trying to get Drawable of that imageview ..I have reached till here ... Now I want to get the touch event position w.r.t to the imageview so I can check it in drawable.getTransparentRegion ? ... I hope you understood
At the end I want to differentiate between the transparent part of the imageView i.e.(not needed part of the Imageview ) and the Transculent part that will be there of the drawable in the imageView...
get coordinates of imageview on window by
public void getLocationInWindow (int[] location),
pass integer array of two size to this method. Invoke this method after layout has been drawn, use location array to get imageview's left and top co-ordinates, add transparent co-ordinates to it, and you will get co-ordinates which can be compared to touchevent co-ordinates.

Categories

Resources