On fling to cause a simple animation - java

I am not sure how to make a simple onFling simply start an animation. It doesnt matter which direction the swipes go any contact and slide across the screen should cause the animation to start. The other thing I am wondering is how to get it to let the animation run through and when the animation finishes I want it to display a picture. To give you an idea of what I am doing picture a twister spin board. The animation is the spinner spinning and after it finishes spinning it stops and points in a direction. How can I get a similar effect. (The animation is a spinning animation and the pictures are all pointing in differnt directions)
Would appreciate any help anyone can give. Thanks.

one possibility: use a gesturedetector to detect the fling.
psuedocode:
public class WhatEver extends Activity implements OnGestureListener {
private GestureDetector gestures;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gestures = new GestureDetector(mContext, this);
View yourViewYouWantToHaveThemFling=(View) findViewById(blah..);
yourViewYouWantToHaveThemFling.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent event) {
if (gestures.onTouchEvent(event)) {
return true;
}
return false;
}
});
}
//all your normal stuff
#Override
public boolean onDown(MotionEvent e) {
return true; //must return true to continue
}
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
//do something
return false;
}
#Override
public void onLongPress(MotionEvent e) {
}
#Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
return false;
}
#Override
public void onShowPress(MotionEvent e) {
}
#Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}
}
as for the animation, set a listener on your animation,
yourAnimation.setAnimationListener(new DisplayPicture());
then
private final class DisplayPicture implements Animation.AnimationListener {
.
.
public void onAnimationEnd(Animation animation) {
//code to display your picture here
}
.
.
}

Related

I want to display some text onDoubleClick event how to do so ? i have attached my sourcecode Along

I want to apply a doubleClick event on a text field which displays some text on double click event but it keeps giving me errors is there any method to just simply doing this
TextView tv;
#SuppressLint("ClickableViewAccessibility")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.mytext);
tv.setOnTouchListener(new OnDoubleTapListener() {
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
return true;
}
#Override
public boolean onDoubleTap(MotionEvent e) {
tv.setText("DoubleTouch");
return true;
}
#Override
public boolean onDoubleTapEvent(MotionEvent e) {
tv.setText("Double Touch ");
return true;
}
}
);
}
You should initialize the Gesture Detector like this
GestureDetector gd = new GestureDetector(this, new GestureDetector.OnGestureListener() {
#Override
public boolean onDown(MotionEvent motionEvent) {
return false;
}
#Override
public void onShowPress(MotionEvent motionEvent) {
}
#Override
public boolean onSingleTapUp(MotionEvent motionEvent) {
return false;
}
#Override
public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
return false;
}
#Override
public void onLongPress(MotionEvent motionEvent) {
}
#Override
public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
return false;
}
});
Then set on double click listener like this
gd.setOnDoubleTapListener(new GestureDetector.OnDoubleTapListener() {
#Override
public boolean onSingleTapConfirmed(MotionEvent motionEvent) {
return false;
}
#Override
public boolean onDoubleTap(MotionEvent motionEvent) {
return false;
}
#Override
public boolean onDoubleTapEvent(MotionEvent motionEvent) {
return false;
}
});
Finally you apply the listener to your View like this
tv.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
gd.onTouchEvent(event);
return false;
}
});
Refer Android docs for more info Detect Common gestures

Why isnt Gesture Listener working?

I want to use Gesture Listener so I wrote a simple code to test it out but it is not working. I watched a YouTube tutorial and copied it. I did not test this but it is something very similar to what I am trying to accomplish. This is my first time using Gesture. Here is what I am:
MyClassActivity.java
public class MyClassActivity extends Activity implments GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener{
private GestureDetector gestureDetector;
private TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scrolling);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
this.gestureDetector = new GestureDetector(this, this);
gestureDetector.setOnDoubleTapListener(this);
textView = (TextView) findViewById(R.id.textView);
} //end of Oncreate
#Override
public boolean onTouchEvent(MotionEvent event) {
this.gestureDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
#Override
public boolean onDown(MotionEvent e) {
textView.setText("onDown");
return false;
}
#Override
public void onShowPress(MotionEvent e) {
textView.setText("onShowPress");
}
#Override
public boolean onSingleTapUp(MotionEvent e) {
textView.setText("onSingleTap");
return false;
}
#Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
textView.setText("onScroll");
return false;
}
#Override
public void onLongPress(MotionEvent e) {
textView.setText("onLongPress");
}
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
return false;
}
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
return false;
}
#Override
public boolean onDoubleTap(MotionEvent e) {
return false;
}
#Override
public boolean onDoubleTapEvent(MotionEvent e) {
return false;
}
Try this at the end of your onCreate method.
View v = //Get your view
v.setOnTouchListener(new OnTouchListener(){
public boolean onTouch(View v, MotionEvent e){
return gestureDetector.onTouchEvent(e);
}
});
Can you try with
#Override
public boolean onTouchEvent(MotionEvent event) {
this.gestureDetector.onTouchEvent(event);
return true; // paas it true , as you are handling it.
}

View setVisibility with GestureDetector in Android

I have a GestureListener with a view. I want to change view visibility based on the GestureListener results. On double tap show the view, on single tap hide it. And I want to show the vie when I'll hold my finger on the display (snapchat like feature).
mDetector = new GestureDetectorCompat(this, this);
mDetector.setOnDoubleTapListener(this);
view.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
mDetector.onTouchEvent(event);
return true;
}
});
Here's my gesture listeners.. On double tap it'll show my view but it keeps the view visible
#Override
public boolean onSingleTapUp(MotionEvent event) {
Log.d(DEBUG_TAG, "onSingleTapUp: " + event.toString());
text.setVisibility(View.INVISIBLE);
return true;
}
#Override
public boolean onDoubleTap(MotionEvent event) {
Log.d(DEBUG_TAG, "onDoubleTap: " + event.toString());
text.setVisibility(View.VISIBLE);
return true;
}
I had a similar problem and I added
ImageView imgView11 = (ImageView)findViewById(R.id.imagek11d);
just before imgView11.setVisibility(View.INVISIBLE);
and before imgView11.setVisibility(View.VISIBLE);

Swipe gesture not recognized

For my Buttons onClickListener I have the following code:
on_enter_button.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
enter(words, linearLayout, llp, view);
}
});
Instead of pressing enter Button, I simply want to Swipe the screen in any direction and run the same method: enter(words, linearLayout, llp, view);
Currently I think my program does not recognize Swipe gestures at all. I assume it's because I placed enter(words, linearLayout, llp, view); incorrectly. Also, I put all the Gesture recognition code inside the onCreate(). Is that the correct way to program the code?
Can someone please help?
EDITED CODE:
public class Game extends Activity{
public void enter(String[] w, LinearLayout ll, LinearLayout.LayoutParams params, View v){
... //some code
... //some code
... //some code
}
#Override
public boolean onTouchEvent(MotionEvent event) {
return myGestureDetector.onTouchEvent(event);
//error myGestureDetector not recognized since it's created in onCreate so it does not exist here
}
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.game_activity);
SimpleOnGestureListener mySimpleGestureListener = new SimpleOnGestureListener()
{
#Override
public boolean onDoubleTap(MotionEvent e) {
return super.onDoubleTap(e);
}
#Override
//swipe does not work!
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY)
{
enter(words, linearLayout, llp, view);
return super.onFling(e1, e2, velocityX, velocityY);
}
#Override
public void onLongPress(MotionEvent e) {
super.onLongPress(e);
}
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
return super.onSingleTapConfirmed(e);
}
private boolean permissibleYVelocity(float velocityY)
{
if ((velocityY < -200) || (velocityY > 200))
{
return false;
}
else
{
return true;
}
}
};
final GestureDetector myGestureDetector = new GestureDetector(getApplicationContext(), mySimpleGestureListener);
//button listener works
on_enter_button.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
enter(words, linearLayout, llp, view);
}
});
}
}
You need to override View.onTouchEvent().
#Override
public boolean onTouchEvent(MotionEvent event) {
return mGestureDetector.onTouchEvent(event);
}
Call your method in SimpleOnGestureListener.onFling():
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY)
{
enter(words, linearLayout, llp, view);
return true;
}
EDIT:
Setup your GestureDectector in onCreate() as well:
mGestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener()
{
#Override
public boolean onDoubleTap(MotionEvent e) {
return super.onDoubleTap(e);
}
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY)
{
enter(words, linearLayout, llp, view);
return super.onFling(e1, e2, velocityX, velocityY);
}
#Override
public void onLongPress(MotionEvent e) {
super.onLongPress(e);
}
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
return super.onSingleTapConfirmed(e);
}
private boolean permissibleYVelocity(float velocityY)
{
if ((velocityY < -200) || (velocityY > 200))
{
return false;
}
else
{
return true;
}
}
});

Java: Breakout game, move player as I swipe?

I'm making a simple Breakout game, and so far I have created the ball and the player. I made the ball so that it moves to the position you touch, but I'd rather have it moving towards the direction I swipe. As in, the player moves along with my finger. Here's the code I have in my MainActivity:
package com.example.breakout;
import android.os.Bundle;
import android.app.Activity;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
public class MainActivity extends Activity implements OnTouchListener, OnGestureListener{
BreakoutView bv;
GestureDetector gs;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bv = new BreakoutView(this);
setContentView(bv);
bv.setOnTouchListener(this);
gs = new GestureDetector(MainActivity.this, this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
/*if(arg1.getY()<600){
bv.p1.x = arg1.getX();
bv.p1.y = 600;
}*/
return false;
}
#Override
public boolean onTouchEvent(MotionEvent me){
return gs.onTouchEvent(me);
}
#Override
public boolean onDown(MotionEvent arg0) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
/*if(e2.getX()>e1.getX()){
//Move right
bv.p1.x = e2.getX();
}*/
return false;
}
#Override
public void onLongPress(MotionEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
float arg3) {
// TODO Auto-generated method stub
return false;
}
#Override
public void onShowPress(MotionEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public boolean onSingleTapUp(MotionEvent arg0) {
// TODO Auto-generated method stub
return false;
}
}
So, the two methods I have tried are the onTouch and the onFling ones. The onTouch "teleports" my player where I touch, which isn't what I want. The onFling only moves my player when I'm done swiping, so that doesnt work either. My question is, how can I make it move as I swipe my finger? Do I use onDown? Any help is appreciated.
Thanks!
Assuming you have some sort of game loop set up where the UI is updated on intervals:
/**
* Onclick event for Touch Screen
*/
#Override
public boolean onTouchEvent(MotionEvent input) {
if (input.getAction() == MotionEvent.ACTION_MOVE) {
float currX = input.getX();
float currY = input.getY();
if (!swiping) {
swiping = true;
swipeStartY = currY;
swipeStartX = currX;
}
if (swiping) {
double newDirection = Math.atan2((currY-ball.getY()),currX-ball.getX());
ball.setDirection(newDirection);
}
} else if (input.getAction() == MotionEvent.ACTION_UP) {
if (swiping) {
swiping = false;
}
}
}
This way the ball's direction is updated on each tick of the game thread and moves towards your finger (the touch event X/Y location). I'm not sure how you have set up your drawing/physics/etc. so I can't provide meaningful function calls here. If you need to modify it so it swipes in a straight line after you lift your finger then store the initial X/Y point (swipeStartX/swipeStartY in this case) and only set the new direction after you stop swiping.
EDIT:
I should mention, my class definition that contains this code is as follows:
public class GameView extends SurfaceView implements SurfaceHolder.Callback {
....
}

Categories

Resources