This article describes the example code for Java to implement frame animation. Share it for your reference, as follows:
1. Reproduction diagram
2. Brief code for frame animation
private ImageView bgAnimView; private AnimationDrawable mAnimationDrawable; //Initialize mAnimationDrawable = new AnimationDrawable(); bgAnimView = new ImageView(mContext); bgAnimView.setBackgroundDrawable(getAnimationDrawable(mAnimationDrawable)); params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); params.topMargin = Util.Div(176 + 58); params.gravity = Gravity.CENTER_HORIZONTAL; addView(bgAnimView, params); private AnimationDrawable getAnimationDrawable(AnimationDrawable mAnimationDrawable) { int duration = 50; mAnimationDrawable.addFrame(mContext.getResources().getDrawable(R.drawable.loading1), duration); mAnimationDrawable.addFrame(mContext.getResources().getDrawable(R.drawable.loading2), duration); mAnimationDrawable.addFrame(mContext.getResources().getDrawable(R.drawable.loading3), duration); mAnimationDrawable.setOneShot(false); return mAnimationDrawable; } //Anime Start public void animLoadingStart() { this.setVisability(View.VISIBLE); if (mAnimationDrawable != null) { mAnimationDrawable.start(); } } //Anime end public void animLoadingEnd() { if (mAnimationDrawable != null) { mAnimationDrawable.stop(); } 3. Extension:
//X-axis translation public void animY(int y, int nextY, int duration) { LinearInterpolator ll = new LinearInterpolator(); //Constant-speed ObjectAnimator animator = ObjectAnimator.ofFloat(yourView, "translationY", 0, 300);//300 If it is a negative value, it is translated upwards animator.setDuration(duration); animator.setInterpolator(ll); animator.start(); } //Y-axis translation public void animX(int x, int nextX, int duration) { LinearInterpolator ll = new LinearInterpolator(); ObjectAnimator animator = ObjectAnimator.ofFloat(yourView, "translationX", x, nextX); animator.setDuration(duration); animator.setInterpolator(ll); animator.start(); } //Lonear compression 0.5 times LinearInterpolator ll = new LinearInterpolator();//Static ScaleAnimation scaleAnimation = new ScaleAnimation(1, 1, 1, 0.5f);//Default from (0,0) scaleAnimation.setDuration(500); scaleAnimation.setInterpolator(ll); scaleAnimation.setFillAfter(true); chartView.startAnimation(scaleAnimation); // Horizontal compression 0.5 times LinearInterpolator ll = new LinearInterpolator(); ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.5f, 1, 1);// Default from (0,0) scaleAnimation.setDuration(500); scaleAnimation.setInterpolator(ll); scaleAnimation.setFillAfter(true); chartView.startAnimation(scaleAnimation);Click to open the download address of the material
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.