I'm trying to do some ROI (region of interest) selection in my app, but I donĀ“t know, how to create some kind of resizable (by fingers) rectangle like you can see in Google Goggles.
Can you help me? Is there any source code example?
My final solution is to draw and rectangle in the midle of a view and implement onTouchEvent() in my activity to set new corners coordinates like this:
#Override
public boolean onTouchEvent(MotionEvent me) {
if(SETTING_ROI == true){
if (me.getAction() == MotionEvent.ACTION_DOWN) {
START_DRAGGING = true;
myView.selectCorner((int) me.getRawX(), (int) me.getRawY()); // selecst nearest corner
}
if (me.getAction() == MotionEvent.ACTION_MOVE){
Log.d(TAG, "ACTION_MOVE");
myView.moveCorner((int) me.getRawX(), (int) me.getRawY()); // move selected corner continuously
}
if (me.getAction() == MotionEvent.ACTION_UP){
if (START_DRAGGING == true) {
START_DRAGGING = false;
myView.moveCorner((int) me.getRawX(), (int) me.getRawY()); // final selected corner move
}
}
}
return false;
}
Could you reuse code from the Gallery-Cropper?
The CropImage class source code is available 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 have been developing a function that allows users to draw a pattern (similar to the pattern unlock feature that comes with Android). I successfully implemented this function in an Activity using onTouchEvent():
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
// finish
}
if (event.getAction() == android.view.MotionEvent.ACTION_MOVE) {
// get drawing location
int x = (int) event.getX();
int y = (int) event.getY();
......
}
return super.onTouchEvent(event);
}
Now, I hope to do the same thing using a DialogFragment. However, a DialogFragment does not have a onTouchEvent Listener. Therefore, I cannot use the code implemented in an Activity.
Any ideas will be highly appreciated.
I am creating my first Android app using this guide as a reference. Currently, I have a red button on my canvas and when the user clicks the button a boolean (green) will be set to true in order for the button's bitmap to a green button.
That part of the application works, however it works regardless where the user clicks on the canvas. I only want the boolean to be changed when the user clicks on the button's bitmap. Here is what I currently have in my code:
The onTouchEvent() method
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
button.handleActionDown((int)event.getX(), (int)event.getY());
if (button.isTouched()) {
green = true;
}
} if (event.getAction() == MotionEvent.ACTION_MOVE) {
} if (event.getAction() == MotionEvent.ACTION_UP) {
if (button.isTouched()) {
green = false;
button.setTouched(false);
}
}
return true;
}
The handleActionDown() Method
public void handleActionDown(int eventX, int eventY) {
if (eventX >= (x - bitmap.getWidth() / 2) && (eventX <= (x + bitmap.getWidth()/2))) {
if (eventY >= (y - bitmap.getHeight() / 2) && (eventY <= (y + bitmap.getHeight()/2))) {
setTouched(true);
} else {
setTouched(false);
}
} else {
setTouched(false);
}
}
Can anybody see what I am missing in order for the ACTION_DOWN event to make it so it only triggers when the bitmap's bitmap is touched?
Regards
Avoid using onTouchEvent with buttons, it is better to use onClick because the OS detects whether or not a specific button is clicked instead of you having to calculate the position of a button
First, set the onClickListener
btn.setOnClickListener(this);
This assumes the current class implements View.OnClickListener, so if you don't implement it you either have to use a different class or create an anonymous inner class.
Then, in the onClick method, make sure the ID matches and add whatever action you want to do inside an if-statement (or alternatively a switch statement)
#Override
public void onClick(View v) {
if(v.getId() == R.id.btn1){
//do whatever you want on press
}
}
I have to drag and i am not able to drag it perfectly:
The issues are:
1) I have to drag two to three times to bring that to desired position.Hence textview is not following the finger movement smoothly.
2)If i move textview in upward direction its only going downwards.
I am providing the code of textview on touch event.Please help.Thanks in advance.
final TextView t=(TextView)findViewById(R.id.textView4);
t.setOnTouchListener(new OnTouchListener(){
public boolean onTouch(View v, MotionEvent me) {
// TODO Auto-generated method stub
float x = me.getX();
float y = me.getY();
if (me.getAction() == MotionEvent.ACTION_DOWN) {
status = START_DRAGGING;
}
if (me.getAction() == MotionEvent.ACTION_UP) {
status = STOP_DRAGGING;
t.setPadding((int) me.getX(), (int) me.getY(), 10, 10);
Log.i("Drag", "Stopped Dragging");
} if (me.getAction() == MotionEvent.ACTION_MOVE) {
if (status == START_DRAGGING) {
System.out.println("Dragging");
t.setPadding((int) me.getX(), (int) me.getY(), 10, 10);
t.invalidate();
}
}return true;
}});
Perhaps you are not moving Views the way they are supposed to, in Android?
setPadding() is not the best way to go about it.
Here is an alternative way of Drag and Drop for Views
In my app I draw an image covering the entire screen.
I want to now how can I know where the user touched the screen?
Thanks
Answered here: How do i get the x,y coordinate for a screen touch?
Here is a snippet of one of my games that does this. There are different ways to do it, but here I did it by subclassing View:
public class WorldView extends View {
...
#Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
Log.d(TAG,"touch event "+action+" x="+event.getX()+" y="+event.getY());
if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_MOVE)
{
int x = (int)event.getX();
int y = (int)event.getY();
Log.d(TAG,"setting target to "+x+","+y);
}
else
return super.onTouchEvent(event);
return true;
}