After working for a while, I haven't had time to summarize the problems encountered in my work. Not only can I use them in the future, but also I can provide help to other development partners. In the future, I will help to sort out the problems or learning experience.
Business problem: need to use gesture monitoring to achieve various effects of animation. For example, slide left and right to enlarge an animation by one time, translate right by how much distance, or display multiple animations at the same time.
I. gesture monitoring
1. Implement view.OnTouchListener
public class myApplication imlements implements View.OnTouchListener{ @Override public boolean onTouch(View v, MotionEvent event) { //Use listening events for gesture listening return mGestureDetector.onTouchEvent(event); } }
2. Initialization of gesture monitoring
private GestureDetectorCompat mGestureDetector;
mGestureDetector = new GestureDetectorCompat(this, new gestureListener());
public class gestureListener implements GestureDetector.OnGestureListener {
@Override public boolean onDown(MotionEvent e) { return false; } @Override public void onShowPress(MotionEvent e) { } @Override public boolean onSingleTapUp(MotionEvent e) { //Animation effective gestureAnimationRight(); return false; } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { return false; } @Override public void onLongPress(MotionEvent e) { } private int verticalMinDistance = 100; private int minVelocity = 0; @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { //Animation effective gestureAnimationLeft(); return false; } }
II. Animation effect
Animation only put part of the code, mainly to explain the use of animation translation and zoom. There are many effects not shown in the animation, such as animation loop playing, such as returning to the initial point after playing once, etc
AnimationSet set = new AnimationSet(false); int big = (mRlayout1.getWidth() >= mRlayout2.getWidth()) ? mRlayout1.getWidth() : mRlayout2.getWidth(); //Animation scale effect initialization Animation animationtop = new ScaleAnimation(0.4f, 1.0f, 0.4f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); //Duration of the effect animationtop.setDuration(1000); set.addAnimation(animationtop); //Animation translation effect initialization animationtop = new TranslateAnimation(20 - (int) (0.3 * big), circleOffsetWidth, 0, 0); //Duration of the effect animationtop.setDuration(1000); set.addAnimation(animationtop); //Start animation mRlayout1.startAnimation(set); //Whether the animation stays at the end of the animation set.setFillAfter(true);
Basically understand the above usage, for gesture monitoring to achieve the effect of animation basically no problem. In addition, the use of animation may affect the hiding and display effect of the control. The next article will talk about the solution to this problem