I wan't to use a image view like below, to create a virtual cross controller or d-pad (It's not possible in my case to do this with different buttons).
When the cross image is displayed on the screen, I wan't to check on wich side the user has pressed to call a function like up(), down(), left() and right().
Cross image:
imageView1.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int)event.getX();
int y = (int)event.getY();
if((x>170&&x<230)&&(y>0&&y<170))
{//up
Toast.makeText(MainActivity.this,"up",Toast.LENGTH_SHORT).show();
}
else if((x>170&&x<230)&&(y>230&&y<400))
{//down
Toast.makeText(MainActivity.this,"down",Toast.LENGTH_SHORT).show();
}
else if((x>0&&x<170)&&(y>170&&y<230))
{//left
Toast.makeText(MainActivity.this,"left",Toast.LENGTH_SHORT).show();
}
else if((x>230&&x<400)&&(y>170&&y<230))
{//right
Toast.makeText(MainActivity.this,"right",Toast.LENGTH_SHORT).show();
}
return false;
}
});
//take iamge view 400px*400px
Related
Given this method (http://appium.io/docs/en/commands/element/actions/click/) only clicks the center of an element, are there means/methods to click on a specific spot on an element?
You can get the current XY coordinate and then compare these coordinates with your SPECIFIC spot.
Sample:
element.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN){
if(event.getX()>a && event.getY()<b) {
//Your code goes here
}
}
return true;
}
});
where a and b are specific spot in terms of X and Y axis respectively.
I am trying to implement a "swipe and hold" gesture in Android where the user swipes from a point on the left to another point on the right, then continues to hold at the right point until a server-side action is complete. The idea being that if the user lets go of the hold, the server-side action will be cancelled.
I've been trying to go about this the following way but have encountered an issue when getting to the hold part of the gesture. What I'd like to have happen is that when the user reaches point B on the right, the ACTION_MOVE is somehow converted into an ACTION DOWN for the view on the right and from then on, as long as the event ACTION_DOWN continues the gesture is valid, but if the user lets go or leaves the view area, it is invalid. So the question is, is it possible to chain the ACTION_MOVE from left to right into a "hold" gesture once the user reaches point B on the right? I've tried adding an onTouch to point B after the swipe, but the only way that works is if the user lets go of the swipe, then presses point B again. Will this be possible, or should I go about this a different way, such as drag and drop?
Point A and B as found below are currently image views, but that's more placeholder than anything.
private boolean mValidGesture = false;
Rect outRect = new Rect();
int[] location = new int[2];
private boolean isViewInBounds(View view, int x, int y){
view.getDrawingRect(outRect);
view.getLocationOnScreen(location);
outRect.offset(location[0], location[1]);
return outRect.contains(x, y);
}
...
mPointA.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int)event.getRawX();
int y = (int)event.getRawY();
if(event.getAction() == MotionEvent.ACTION_MOVE){
if (isViewInBounds(mPointB, x, y)) {
mPointB.dispatchTouchEvent(event);
Log.d(TAG, "onTouch ViewB");
Toast.makeText(MainActivity.this, "Swipe complete", Toast.LENGTH_SHORT).show();
//This is the part that happens only after you let go of the swipe :(
mPointB.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
mValidGesture = true;
Log.d(TAG, "Gesture: " + String.valueOf(mValidGesture));
return true;
case MotionEvent.ACTION_UP:
mValidGesture = false;
Log.d(TAG, "Gesture: " + String.valueOf(mValidGesture));
return true;
}
return false;
}
});
} else if(isViewInBounds(mPointA, x, y)){
Log.d(TAG, "onTouch ViewA");
}
}
// Further touch is not handled
return false;
}
});
You don't need to put touchListener on pointB. Try something like this:
boolean serverActionStarted = false;
#Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int)event.getRawX();
int y = (int)event.getRawY();
if(event.getAction() == MotionEvent.ACTION_MOVE){
if (isViewInBounds(mPointB, x, y)) {
// TODO : swipe completed. Start server action
serverActionStarted=true;
}
}else if(event.getAction() == MotionEvent.ACTION_UP && serverActionStarted){
// TODO : cancel the server action
serverActionStarted = false;
}
So basically, when the user swipes from pointA to pointB, you start the server action and set a boolean serverActionStarted to true. After that, if ACTION_UP event occurs, with the severAction having started, you cancel the server action.
I'm working on my first game app, when user touches an image the app shows a message to the user (or does other things in other situations).
My photos are non-geometric shapes (for example animal photos) with 100% transparent backgrounds
(I used Photoshop and saved them in PNG format.)
My problem is that I need it to react (and show the message,...) only when the animal shape itself (NOT the imageview's transparent background/corners) is touched by user.
I used the solution that's offered in this question to find out if the pixel that's touched is transparent or not, but it doesn't work the way I need. Here's part of my onCreate() method in MainActivity.java:
tempIV=(ImageView)findViewById(R.id.birdIV);
final Bitmap bitmap = (BitmapDrawable)tempIV.getDrawable()).getBitmap();
tempIV.setOnTouchListener(new View.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event){
int x = (int)event.getX();
int y = (int)event.getY();
int transparency = bitmap.getPixel(x,y);
if (transparency == 0)
{ //Do nothing
return false;}
else {
Toast.makeText(MainActivity.this,
"This is an animal!", Toast.LENGTH_LONG).show();
}
return true;
}
});
What should I do?
Try using this :-
if (bitmap.getPixel(x, y) == Color.TRANSPARENT)
{
return false; //don't react
}
else
{
return true; //do something like intent
}
Good Day, I'm newbie in android, and I would like to add one functionality to my app, but I haven't idea how to do that.
I would like to Zoom Out image by Two-finger touch
I had implemented Zoom In functionality using Subsampling Scale Image View library, but how to add Zoom Out I don't know.
What I have for now? What I must to add under the Toast to get ImageView normal state?
ImageView mImageView;
mImageView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
int action = motionEvent.getAction();
int fingersCount = motionEvent.getPointerCount();
if((action == MotionEvent.ACTION_POINTER_UP) && (fingersCount == 2)){
Toast.makeText(getActivity(), "Double tap", Toast.LENGTH_SHORT).show();
}
return false;
}
});
Anyway, thank you!
I'm kinda trying to clone the ripple effect found in Google's material design. Now, I've noticed that the ripple always starts from the position that was touched. For this, I've created a drawable which will be initially invisible and but will become visible once the view is touched. The ImageView containing the drawable will be then moved to the position that was touched and the ripple animation will start thereafter. Now, while trying to do this, I'm stumbling upon one thing which is I can't figure out how to find out the position that was touched. I came across several questions on stackoverflow which said that the touch position can be found out by using the following code.
#Override
public boolean onTouch(View v, MotionEvent e) {
int X = (int)(e.getX());
int Y = (int)(e.getY());
return false;
}
I did try to implement this code in my app but I ended up with a NullPointerException. Please tell me how to find the exact location of the screen that was touched and move the view there?
Use like this:
#Override
public boolean onTouch(View v, MotionEvent e) {
if(e.getAction() == MotionEvent.ACTION_DOWN) { //ACTION_DOWN for the position touched.
int X = (int)(e.getX());
int Y = (int)(e.getY());
}
return false;
}
You should get position for ACTION_DOWN event and then move your view
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
float x = event.getX();
float y = event.getY();
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}