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;
}
});
Related
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()
I am currently working on galaxy s4 app, in which i want moving image with moving finger, but how to implement this onHoverListener. Please Help.
And thanks in advance.
you can implementing onTouchListener() to your imageView like below:
img = ((ImageView) findViewById(R.id.img_arrow));
img.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent motionEvent) {
int rawX = (int) motionEvent.getX();
int rawY = (int) motionEvent.getY();
if (motionEvent.getAction() == MotionEvent.ACTION_MOVE) {
TranslateAnimation anim = new TranslateAnimation(rawX, rawY, 0, 0);
anim.setDuration(500L);
img.startAnimation(anim);
}
return true;
}
});
Here we are applying TranslationAnimation to our imageView img
Instead of hoverListener implement touch listener on ur image and also implement touchevent in ur activity. then u can use OnTouchEvent's actionMove.
when user doing actionMove find the coordinates using motionEvent object and translate ur view to those coordinates using translate animation.this will give look as if u r moving the image.
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
I have an android code which starts vibrating for a random amount of time when the user touch a screen. Now I want to change it a bit. I want to ask the user to touch a predefined position, for example in the middle of screen. what should I do that only in some special coordinates touching become effective?
You can use a onTouchListener. The Listener gets a MotionEvent from which you can get th coordinates and check the position:
something.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// get coordinates from touch event
int x = (int) event.getX();
int y = (int) event.getY();
// check if the coordinates are in a specific area
return true;
}
});
To get the width and height you can use the following code:
Display d = getWindowManager().getDefaultDisplay();
width = d.getWidth();
height = d.getHeight();
onTouch(View v, MotionEvent event)
onTouch give you a MotionEvent, with this, you can simply do
event.getX();
event.getY();
for getting coordinates and do something with it (or not :))
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 .