How to get canvas area click? - android

I have drawn piechart using canvas method of view , but now i want get click of individual pie ? how can i dot that?

I got perfect answer for this question:
get color code of click area and check if color match with your color code this will get click you want.
#Override
public boolean onTouchEvent(MotionEvent event) {
float touchX = event.getX();
float touchY = event.getY();
Logger.debug("X-->"+touchX+" Y---->"+touchY);
//get drawing cache of your view
Bitmap bitmap = getDrawingCache(true);
//Get color code of pixle where you have tap
int colorCode=bitmap.getPixel((int)touchX,(int)touchY);
if(colorCode == context.getResources().getColor(R.color.pie_blue)) {
Logger.debug("Color blue");
onPieClick.onBluePieClick(touchX,touchY);
}else if(colorCode == context.getResources().getColor(R.color.pie_green)) {
Logger.debug("Color green");
onPieClick.onGreenPieClick(touchX,touchY);
}
return super.onTouchEvent(event);
}

What you can do is,
Override onTouch event & You will get Motion event,
You will get x & y co-ordinates of the click by event.getX() &
event.getY() respectively.
identify where this x & y intersect in pie.
Sample code:
1)Simple
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_UP){
float xCord=event.getX();
float yCord = event.getY();
....
Write condition to identify where this x & y intersect in pie.
...
}
return true;
}
2) Another way getting touch (good way)
OnGestureListener mGestureListener=new GestureDetector.SimpleOnGestureListener(){
public boolean onSingleTapConfirmed(MotionEvent e) {
float xCord=e.getX();
float yCord = e.getY();
....
identify where this x & y intersect in pie.
...
};
};
GestureDetector gestureDetector=new GestureDetector(context, mGestureListener);
public boolean onTouchEvent(MotionEvent event) {
gestureDetector.onTouchEvent(event);
return true;
}

You can't. Well, at least not directly.
You could do the following though:
In the click handler for the view, determine the xy coordinates of the click
Compare the drawing code you wrote, thereby determining in which piece of the pie the click was

Related

How to get the input from a touch screen from individual pixels?

I am creating a paint app for android, I have a doubt when we touch the display, can we get the input pixel by pixel to make the painting more accurate. Any one help me how to get pixel by pixel input ?
You can just set an OnTouchListener on your canvas view and get the x & y coordinates from there.
view.setOnTouchListener(new View.OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
if (event.getActionMasked() == MotionEvent.ACTION_DOWN)
{
//Here you can do whatever you need with the coordinates
int xPos = event.getX();
int yPos = event.getY();
}
return false;
}
});

How to allow clicks inside Floating Action Button's circle only?

These are the FAB dimensions following google specs.
In my application case I need a click listener which is fired by clicking in the FAB's circle only, avoiding clicks inside the square container (56x56).
I was already thinking about get X,Y in each onClick event in my "entire" button and then verify if it's inside my Fab's circle, but i would like to escape from this workaround because it could cause accuracy-click problems with different devices resolutions.
Do you have any idea for an alternative solution?
Thanks in advance.
EDIT:
I've actually implemented this javascript-solution translated in Java
LINK
Do you think i will always get the same accuracy on device screen changes?
I don't think there is a way to actually make a clickable area other than rectangular.
You should make your floating button have a width and height set to wrap_content if your circle is an image. Any way you should set it's width and height to match the diameter of the circle. And process the event like this:
view.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
ViewGroup.LayoutParams layoutParams = v.getLayoutParams();
float radius = layoutParams.width / 2f;
float x = event.getX() - radius;
float y = event.getY() - radius;
if (isInCircle(radius, x, y)) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// perform your action
}
return true;
} else {
return false;
}
}
private boolean isInCircle(float radius, float x, float y) {
if (Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)) < radius) {
//touch was performed inside the circle;
return true;
} else {
//touched ouside the circle
return false;
}
}
}
);
This will work on any device, as long as your button is tightly wrapped around your drawn circle.
If you don't need the touch event falling deeper, under your floating button when you miss the circle, you can avoid some redundant calculations like this:
view.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
ViewGroup.LayoutParams layoutParams = v.getLayoutParams();
float radius = layoutParams.width / 2f;
float x = event.getX() - radius;
float y = event.getY() - radius;
if (isInCircle(radius, x, y)) {
// perform your action
}
}
return true;
}
private boolean isInCircle(float radius, float x, float y) {
if (Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)) < radius) {
//touch was performed inside the circle;
return true;
} else {
//touched ouside the circle
return false;
}
}
}
);
Keep in mind that for this to work your button should be strictly rectangular. if you want a button to be an ellipse the math should be different.

Find tapped icon using X and Y coordinates

I have file list in my android application. when the user double tap on a file I need to find out it. I could write the code to find out double tab action and X and Y coordinates of the user tap. But how can I find out the particular file or folder that the user did double tap?
This is the code what I used:
private View.OnTouchListener onTouchListener=new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
long thisTime=System.currentTimeMillis();
if(thisTime - lastTouchTime <500){
float x=event.getX();
float y=event.getY();
System.out.println("x :"+x);
System.out.println("y "+y);
}else{
lastTouchTime=thisTime;
}
}
return true;
}
};
I cannot find out it using icon sizes because they are not similar and user able to change it as they wish for icon image. Is there anyway to find out particular icon using X and Y coordinates of user tap?

Update the position of textview/imageview at relative layout after moving it by touch

I want to have possibility to change position of items(textviews/imageview) on the screen by using touch. So I made OnTouchListener which looks like this:
float x = 0,y = 0 ;
private void clickOnObjectTaker() {
cream.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
boolean touched= false;
if(event.getAction()==MotionEvent.ACTION_DOWN)
{
messageMaker("touched");
x= event.getX();
y= event.getY();
}
if(event.getAction()==MotionEvent.ACTION_MOVE )
{
Animation anim =
new TranslateAnimation(x, event.getX(), y, event.getY());
anim.setFillAfter(true);
anim.setFillEnabled(true);
cream.startAnimation(anim);
x= event.getX();
y= event.getY();
}
if(event.getAction()== MotionEvent.ACTION_CANCEL)
{
cream.setX(x);
cream.setY(y);
}
return true;
}});
}
My problem is that when i change the position of object, the listener don't get new position, so I see the object in different place on the screen, but when I want to move it I must click on old position. I thought i solve it by adding this lines:
if(event.getAction()== MotionEvent.ACTION_CANCEL)
{
cream.setX(x);
cream.setY(y);
}
but nothing changed.
UPDATE: I tried with this code. There |I haven't any problems with updating position element of layout, everything works properly except the process of moving object by touch.
What I don't like it in this method:
touched object vibrate when I move it (it doesn't look nice),
object doesn't follow perfectly my finger, like somewhere I should scale the coordinates ( for example I moved object from right to left side of screen, when my finger stops at the brink of screen, the object is lag behind of it.)
this two things makes this method useless for me.
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()== MotionEvent.ACTION_DOWN){
dx = event.getX()-v.getX();
dy = event.getY()-v.getY();
}
if(event.getAction()== MotionEvent.ACTION_MOVE)
{
v.setX(event.getX()-dx);
v.setY(event.getY()-dy);
}
return true;
}
}
The moving of oobject in method with animation looks great for me, everything works fluently. Only problem is connected with update layout, when I add code like this
if(event.getAction()==MotionEvent.ACTION_UP )
{
v.setX(event.getX());
v.setY(event.getY());
}
It put object at random position (I can't figure out, from what place he take this position), If somebody can give me any advice I would be grateful
EDITED
This code almost perfectly I could make repeatedly moves on view. It have two flaws:
small reallocation after Move gesture ends
During moving the view if I stop the finger on the screen, I can see the flashes of view at old position.
the code
OnTouchListener dealingwithproblems = new OnTouchListener()
{
#Override
public boolean onTouch( View v, MotionEvent event) {
if(event.getActionMasked()== MotionEvent.ACTION_DOWN){
//values for
dx = event.getX()-v.getX();
dy = event.getY()-v.getY();
x= event.getX();
y= event.getY();
}
if(event.getActionMasked()== MotionEvent.ACTION_MOVE)
{
Animation anim =
new TranslateAnimation(x, event.getX() , y, event.getY());
//anim.setFillAfter(true);
anim.setFillEnabled(true);
v.startAnimation(anim);
x= event.getX();
y= event.getY();
}
if(event.getActionMasked() == MotionEvent.ACTION_UP)
{
v.setX(event.getX()-dx);
v.setY(event.getY()-dy);
}
return true;
}
};
If somebody knew how to improve it, leave the answer pliz
it maybe animation problem, you are using ViewAnimation try to use change that in Object animator. it supports form API 11.
ObjectAnimator.ofFloat(view,"Translate",x,y,toX,toY);
I had a similar problem (touched object vibrates on move) and resolved it by using getRawX() and getRawY() instead of getX() and getY()

Android get x y coordinates of tab

In Android tabs, how does one get the x,y coordinates of the currently selected tab? My goal is to automatically scroll to that tab (using scrollTo(x,y)) when it is selected in code.
You can find the x and y location of place where screen has been touched using this
main.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View v, MotionEvent e)
{
float x = e.getX();
float y = e.getY();
return true;
}
});
Hope it helps .

Categories

Resources