I'm drawing an rectangle surface by using canvas and i want to disable onTouchEvent() of some part of area, example if you tap(Touch) right side of the rectangle it should not perform onTouchEvent(). can any one tell me How to do this. Thanks in advance.
You have to use Hit and Trial method and generate a formula to run your app on different screen size of devices. And check where the user clicked and if he clicked in the restricted region do nothing.
Like
#Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
int x = (int)event.getX();
int y = (int)event.getY();
if (x<height-100 && y>width-100) {
// Do nothing
}
return super.onTouchEvent(event);
}else{
//Do what you want here
}
Related
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
}
I use Android studio and I have this image with a transparent background. Whenever i click on it it'll bring me to another Activity. But even when I click on the transparent part of the image it'll bring me to the other Activity.
Is it possible to make the nontransparent part clickable (or touchable) and the transparent part unclickable?
Yes this is possible but it becomes much more difficult than just adding an OnClickListener.
The trick is to use a Touch listener instead of click and on either a DOWN or UP event take the position and then either use some simple maths to work out whether it was a transparent area (if the design is a simple one) or, do some more complicated stuff to work out your pixel values at the centre.
new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
If (event.getAction() == MotionEvent.ACTION_DOWN) {
final int x = (int) event.getX();
final int y = (int) event.getY();
//now map the coords we got to the
//bitmap (because of scaling)
ImageView imageView = ((ImageView)v);
Bitmap bitmap =((BitmapDrawable)imageView.getDrawable()).getBitmap();
int pixel = bitmap.getPixel(x,y);
//now check alpha for transparency
int alpha = Color.alpha(pixel);
If (alpha != 0) {
//do whatever you would have done for your click event here
}
}
return true; //we've handled the event
}
}
alpha_image.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
int action=event.getAction();
// TODO Auto-generated method stub
switch (action)
{
case MotionEvent.ACTION_DOWN:
{
// store the X value when the user's finger was pressed down
next();
break;
}
case MotionEvent.ACTION_UP:
{
// store the X value when the user's finger was pressed down
previous();
break;
}
}
return false;
}
});
This is the code I used for image move by finger touch but it is working oneside
(only moving left side) can someone please help me to move image right side also ,that is based on the user touch the image need to move.
Thanks in advance.
I suspect that you misunderstood the concepts of ACTION_UP and ACTION_DOWN (or you just trying to create some very customized UI) but nevertheless, you should return true after intercepting ACTION_DOWN in order ACTION_UP to get fired when you lift your finger.
I am developing an Android application and I need to handle various touch events.
Consider the following requirements:
If the user touches on the right side of the screen I want to display next screen.
If user touches on the left side of the screen I want to display previous screen.
On the whole I have about 25 screens.
How can I get the logical coordinates of the screen and trigger the required event?
You can use this method for getting screen X and Y coordinated:
#Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
float xcoordinated =event.getX();
float ycoordinated =event.getY();
return super.onTouchEvent(event);
}
Override
public boolean onTouchEvent(MotionEvent event) {
float x =event.getRawX();
float y =event.getRawY();
return super.onTouchEvent(event);
}
Note : getX() and getY(), returns the position(x,y) of touch in relation with the View, getRawX() and getRawY() returns the position ( x,y ) in relation of the screen of your Android phone.
I am making a canvas and setting its background which is an image
I am adding text on it by canvas. Drawtext method which works perfectly alright
now I want these text to be clickable but i couldn't find any method
The other method I could think of was to add text box on canvas add write on click event of these text box but could not find any example related to this can anybody suggest what to do.
Canvas is a space where you can just draw some graphics, thus the only way to do what you want is detecting when the user click the surface the canvas is drawn on (e.g. a SurfaceView), and using the coordenates you just fire an event. Of course, you need to verify whether the click was done on the specific part you want (e.g. the area where you drew a button or something).
Use the onTouchEvent method. Here is an example I used for finding out if the user's click coordinates are in a List of rectangles (aka buttons):
#Override
public boolean onTouchEvent( MotionEvent event) {
super.onTouchEvent(event);
int x = (int)event.getX();
int y = (int)event.getY();
xStored = x; yStored=y;
if (event.getAction()==MotionEvent.ACTION_UP){
}else if(event.getAction()==MotionEvent.ACTION_DOWN){
System.out.println("Touching down!");
for(Rect rect : rectangles){
if(rect.contains(x,y)){
System.out.println("Touched Rectangle, start activity."+x+","+y);
invalidate();
}else{
}
}
}else if(event.getAction()==MotionEvent.ACTION_MOVE){
}
this.postInvalidate();
return true;
}