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!
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 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
I am trying to achieve multi touch in such as way that when user pinch out with two finger outside I need to handle one event and on pinching In other event. No ZOOMING IN or ZOOMING OUT NEEDED.
So I am handling onTouchEvent on layout, but could not get conditions true for pinching in and pinching out.
Here is code,
layout.setOnTouchListener(new View.OnTouchListener() {
private int mActivePointerId;
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
mActivePointerId = motionEvent.getPointerId(0);
Log.d(TAG, "COUNT"+motionEvent.getPointerCount());
if(motionEvent.getPointerCount() > 1){
if((motionEvent.getAction() == motionEvent.ACTION_UP) && (motionEvent.getAction() == motionEvent.ACTION_POINTER_DOWN))
Log.d(TAG, "Multi Touch Upwards ");
return true;
} else if((motionEvent.getAction() == motionEvent.ACTION_DOWN) && (motionEvent.getAction() == motionEvent.ACTION_POINTER_UP)){
Log.d(TAG, "Multi Touch DownWard ");
return true;
}
else{
Log.d(TAG, "It is signal Touch");
}
return true;
}
});
I referred this link but not very clear example
Any suggestion to make it work...thanks
You can make it work with the help of ScaleGestureDector. Just need to check the scalefactor if the scale factor would be more then 1 then it would be Pinch Out otherwise Pinch In.
Here is good example, it is very easy and fits your need well,
Just follow the instruction from this link ScaleGestureDector
So , you need to use a GestureDetector.
Whatsoever you need to do with that pinching out and in, you can most likely do with a gesture listener.
Check out this link and take 5 minutes to read, your answer is there for sure: http://developer.android.com/training/gestures/detector.html
Just for the record:I have been using GestureListener recently ,so it's a "sure thing".
I am currently trying to detect an ongoing touch event in my Android app.
In detail I want to recognize within a fragment whether the user touches any part of the app's screen.
Android's OnTouchListener works as expected except if the touch event lasts longer than a few seconds without moving.
For example the first 1-2 seconds of touch are being detected but everything after won't.
Is there something like an "OnPressListener" or a workaround?
If it's a well defined gesture you are trying to detect, have a look at GestureDetector.
http://developer.android.com/reference/android/view/GestureDetector.html
You can use
aButton.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View arg0) {
Toast.makeText(getApplicationContext(), "Long Clicked " ,
Toast.LENGTH_SHORT).show();
return true; // <- set to true
}
});
on your aButton and if you are using API < 19 you have to add
android:longClickable="true"
Attribute to your aButton in layout xml.
I finally found it out.
The solution is to use a simple OnTouchListener:
private boolean pressed = false;
#Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
pressed = true;
} else if ((action == MotionEvent.ACTION_UP)
|| (action == MotionEvent.ACTION_CANCEL)) {
pressed = false;
}
return true;
}
i am developing game.i am displaying gun object center bottom of the screen.when user tap on screen i need to rotate gun that direction.i done rotating image.but when user tap on screen i need to rotate image that direction.can you please help me
Thanks in advance
Aswan
is your image a bitmap? could you convert it to one? Would an onClick listener not work and when the user clicks do something like.. http://www.anddev.org/resize_and_rotate_image_-_example-t621.html
Seeing some code as to what you've attempting would be nice also. The following will help you with checking if the bitmap has been clicked in case you're stuck on that also http://developer.android.com/reference/android/view/View.html .
I've never attemped this myself but I think that would work. Try redrawing the bitmap when you have resized it.
Just use onTouchListener for your View and use this code for that
#Override
public boolean onTouch(View v, MotionEvent event) {
float currentX = event.getX();
float currentY = event.getY();
Log.i(TAG, "action type is"+event.getAction());
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE: {
Log.i(TAG, "Entering in onTouch");
double rotationAngleRadians = Math.atan2(currentX - dialer.centerX, dialer.centerY - currentY);
dialer.rotationAngle = (int) Math.toDegrees(rotationAngleRadians);
Log.i(TAG, "rotaion angle"+dialer.rotationAngle);
dialer.invalidate();
return true;
}
}
return true;
}