I'm trying to add 'kinetic' move in Y-axis from top of the screen to bottom (and vice versa) to custom layout displayed above all apps using the window manager.
I tried to do it in this way:
private void animateFlingInYAxis(View view) {
FlingAnimation fling = new FlingAnimation(view, DynamicAnimation.SCROLL_Y);
fling.setStartValue(0.9F);
fling.setStartVelocity(0f);
//fling.setMinValue(0.5F);
fling.setFriction(0.2F);
fling.start();
}
But the following result is no working correctly. I tried to change values SCROLL_Y
to TRANSLATION_Y
and set a custom range of values but without luck.
Action is processed in ACTION_MOVE
event in this way:
case MotionEvent.ACTION_MOVE:
params.y = initialY + (int) (event.getRawY() - initialTouchY);
if(isMovingInYAxis((int) initialTouchY ,(int) event.getRawY())) {
windowManager.updateViewLayout(view, params);
animateFlingInYAxis(view);
}
break;
I would like to ask how to animate moving in Y-axis in the right way?
Many thanks for any advice.
Edit1: After setting setStartVelocity(2000);
seems to be working better, but the view is partially hiding (see screenshot below).
Edit2: I Implemented GestureDetector.OnGestureListener in Custom View, now I am able to catch fling and scroll events. In onFling method, I am able to recognize direction of fling gesture (TOP_BOTTOM, BOTTOM_TOP), but I am not able to to do the animation of view of given direction.
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Logger.d("onFling");
if(e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
Logger.d("Bottom to top");
animateFlingInYAxis(view, "BOTTOM_TOP");
return false; // Bottom to top
} else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
Logger.d("Top to bottom");
animateFlingInYAxis(view, "TOP_BOTTOM");
return false; // Top to bottom
}
return false;
}
private void animateFlingInYAxis(View view, String type) {
FlingAnimation fling = new FlingAnimation(view, DynamicAnimation.TRANSLATION_Y);
if(type.equals("BOTTOM_TOP")) {
fling.setStartValue(100); //WHICH PARAMS TO PASS?
fling.setMinValue(480); //WHICH PARAMS TO PASS?
fling.setMaxValue(0); //WHICH PARAMS TO PASS?
} else{
fling.setStartValue(470);
fling.setMinValue(480);
fling.setMaxValue(0);
}
fling.setStartVelocity(2000);
fling.setFriction(0.8F);
fling.start();