Android: Draggable Toast, Or Alternative? - android

I'm drawing a number of bitmaps on a canvas, and using MotionEvents to let me drag them around.
When each item is pressed, I'd like to display a Toast, or, Toast-like mini information-panel that tracks the movement of the bitmap being dragged during an ACTION_MOVE. The "Toast" would appear on ACTION_DOWN and vanish on ACTION_UP.
The problem with using Toast is that I have to give it a duration, and, also, I can't change its position once it has been displayed. Unless I can kill the Toast for each ACTION_MOVE, and display a new one right away at the current coordinates? (Sorry, thinking aloud at this point, can't get to my dev machine to test...)
I don't know what other options there might be to achieve this, and I'd very much appreciate suggestions from the community.

Hope this helps, just whipped it up, might even compile!
private boolean mDragging = false;
private float mTouchX = 0, mTouchY = 0;
private Paint mTextPaint = new Paint();//need to set this up in onCreate!
public boolean onTouchEvent(MotionEvent event)
{
mTouchX = event.getX();
mTouchY = event.getY();
if(event.getAction() == ACTION_DOWN)
{
mDragging = true;
}
else if(event.getAction() == ACTION_UP)
{
mDragging = false;
}
return true;
}
protected void onDraw (Canvas canvas)
{
/* Put all your bitmap drawing here. */
/* Draw some info text on top of everything else. */
if(mDragging)
{
String text = mTouchX + ", " + mTouchY;
canvas.drawText(mTouchX, mTouchY + 50, text, mTextPaint);
}
}

A Toast isn't suitable in this case for reasons you already mentioned. It will be better to define a region on the Canvas and draw the message string there using drawText. Put this in the onDraw method and call invalidate whenever you need to update the text or the position of the message board.

Related

Handling the event of touching non-transparent parts of an imageview

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
}

make a transparent part of an image unclickable

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
}
}

Increase touch slop in Android button

We have an Android app that is meant to be mounted on the dash of a vehicle that is operating in rough terrain, so everything is really shaky. We've found that making a single tap on the screen in this kind of situation is difficult, as the taps are often interpreted as small drags.
What I need is for touch events that have a little wiggle before the finger comes up to be interpreted as clicks, not drags. I've been doing some reading into the way the 'touch slop' works in Android and I can see that they already account for this. All I really need to understand is how to increase the 'touch slop' for a subclass of an Android button widget.
Is this possible in just a couple of lines of code? Or do I need to do my own implementations of onInterceptTouchEvent and 'onTouchEvent`? If the latter, can anyone give me some direction on how this would work?
Here is what I did, hope that help you guys.
private Rect mBtnRect;
yourView.setOnTouchListener(new OnTouchListener() {
private boolean isCancelled = false;
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
isCancelled = false;
parent.requestDisallowInterceptTouchEvent(true); // prevent parent and its ancestors to intercept touch events
createClickArea(v);
// your logic touch down
return true;
case MotionEvent.ACTION_UP:
if(!isCancelled) {
// Click logic
}
// release mBtnRect when cancel or up event
mBtnRect = null;
return true;
case MotionEvent.ACTION_CANCEL:
isCancelled = true;
releaseTouch(parent, v);
return true;
case MotionEvent.ACTION_MOVE:
if(!isBelongTouchArea(event.getRawX(), event.getRawY())) {
isCancelled = true;
releaseTouch(parent, v);
}
return true;
default:
break;
}
}
// Create the area from the view that user is touching
private final void createClickArea(View v) {
// for increase rect area of button, pixel in used.
final int delta = (int) mContext.getResources().getDimension(R.dimen.extension_area);
final int[] location = new int[2];
// Get the location of button call on screen
v.getLocationOnScreen(location);
// Create the rect area with an extension defined distance.
mBtnRect = new Rect(v.getLeft() - delta, location[1] + v.getTop() - delta, v.getRight(), location[1] + v.getBottom() + delta);
}
//Check the area that contains the moved position or not.
private final boolean isBelongTouchArea(float rawX, float rawY) {
if(mBtnRect != null && mBtnRect.contains((int)rawX, (int)rawY)) {
return true;
}
return false;
}
private void releaseTouch(final ListView parent, View v) {
parent.requestDisallowInterceptTouchEvent(false);
mBtnRect = null;
// your logic
}
For our simple use-case with a android.opengl.GLSurfaceView, we solved the same problem you have by saying, "if the distance moved in the gesture is less than some_threshold, interpret it as a click". We used standard Euclidean distance between two 2D points = sqrt((deltaX)^2 + (deltaY)^2)), where deltaX and deltaY are the X and Y components of the distance moved in the user's motion gesture.
More precisely, let (x1,y1) be coordinates where user's finger gesture 'started' and let (x2,y2) be coordinates where the gesture 'ended'.
Then, deltaX = x2 - x1 (or it can also be x1 - x2 but sign doesn't matter for the distance 'cz we square the value) and similarly for deltaY.
From these deltas, we calculate the Euclidean distance and if it's smaller than a threshold, the user probably intended it to be a click but because of the device's touch slop, it got classified as a move gesture instead of click.
Implementation-wise, yes we overrode android.view.View#onTouchEvent(MotionEvent) and
recorded (x1,y1) when masked action was android.view.MotionEvent#ACTION_POINTER_DOWN or android.view.MotionEvent#ACTION_DOWN
and when masked action was android.view.MotionEvent#ACTION_MOVE, (x2,y2) are readily available from the event's getX and getY methods (see corresponding documentation)

Put a custom pin over an image and get its coordinates

I have an ImageView with an image loaded,
I want that when the user clicks in a point of the image, another little image(used as pin) is overlapped in this point and the coordinates of the point are returned.
But I haven't idea of how could I do this.
To place a image at a certain coordinates you will have to draw the image on the canvas .
To get the coordinates of the touch event use the following code:
#Override
public void onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) {
mTouchX = event.getX();
mTouchY = event.getY();//stores touch event
} else {
mTouchX = -1;
mTouchY = -1;
}
super.onTouchEvent(event);
}
Here is the code for drawing the image on canvas Image in Canvas with touch events
hope it helps.
Try this,
It will help you to place image based on the co-ordinates. So it can be overlapped
Link 1

android: how to add button or text box on canvas

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;
}

Categories

Resources