Android animation framework, making panning animation more attractive

Android animation framework, making panning animation more attractive

Using ObjectAnimator

ObjectAnimator is a powerful animation framework introduced in Android 3.0, which is used to animate the properties of any object. You can use ObjectAnimator to change the translationX and translationY properties of a View to implement the translation animation of a View.

 View view = findViewById(R.id.view); ObjectAnimator animatorX = ObjectAnimator.ofFloat(view, "translationX", 0f, 100f); // 平移X轴ObjectAnimator animatorY = ObjectAnimator.ofFloat(view, "translationY", 0f, 50f); // 平移Y轴AnimatorSet animatorSet = new AnimatorSet(); animatorSet.play(animatorX).with(animatorY); // 同时执行X轴和Y轴动画animatorSet.setDuration(1000); // 设置动画时长animatorSet.start(); // 开始动画

Using ValueAnimator

ValueAnimator is a lower-level animation framework that generates a series of values ​​during the animation process and then uses these values ​​to update the properties of the View. For translation animation, update the translationX and translationY properties of the View by listening to the value changes of ValueAnimator.

 ValueAnimator animator = ValueAnimator.ofFloat(0f, 100f); // 生成0到100的值animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { float value = (float) animation.getAnimatedValue(); view.setTranslationX(value); // 更新View的X轴位置} }); animator.setDuration(1000); animator.start();

Using ViewPropertyAnimator

Starting from Android 3.0, the View class provides an animate() method that returns a ViewPropertyAnimator object that can be used to chain multiple animation methods.

 view.animate() .translationX(100f) // 平移X轴.translationY(50f) // 平移Y轴.setDuration(1000) // 设置动画时长.start(); // 开始动画

Using XML Animation

Animations can be defined in XML files and loaded and applied when needed.

 <!-- res/anim/translate_animation.xml --> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="0%p" android:toXDelta="100%p" android:fromYDelta="0%p" android:toYDelta="50%p" android:duration="1000"/> </set>
 Animation animation = AnimationUtils.loadAnimation(this, R.anim.translate_animation); view.startAnimation(animation);

Note: When using the startAnimation() method, the position of the View will be reset to its original position after the animation ends, unless you manually update the position of the View at the end of the animation. If you want the View to remain in its final position after the animation ends, consider using the ObjectAnimator, ValueAnimator, or ViewPropertyAnimator methods mentioned above.

Using drawBitmap

Draw pictures at different positions through drawBitmap, which is suitable for the need of using pictures as translation animation.

 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.image); int width = ScreenUtils.getScreenWidth() - bitmap.getWidth(); //int height = bitmap.getHeight(); //绘制原图//canvas.drawBitmap(bitmap, 0, 0, paint); canvas.drawBitmap(bitmap, progress * width / 100, 0, null); //平移图片Matrix matrix = new Matrix(); matrix.postTranslate(progress * width / 100, height); canvas.drawBitmap(bitmap, matrix, null);

<<:  Several methods to implement delayed operation in Android development

>>:  iOS 18 new features only support iPhone 15 Pro and above!

Recommend

Douyin monetization promotion, 7 ways to make money on Douyin!

2018 may be the year when everyone tried out TikT...

The Pretender: The winding road of using sugar substitutes to quit sugar

We who have been on the road to losing weight for...

"I have breast cancer? But I'm a man!"

Expert in this article: Shi Libin, Director of th...

After spending money, how can we judge the effectiveness of promotion?

You know that half of your advertising budget is ...

Is AI’s “knowing people by their tongues” a waste of money?

Produced by: Science Popularization China Author:...

How to adjust the high cost of bidding ocpc? Done in 4 steps!

How to adjust the high cost of bidding ocpc? When...

There are so many advertising channels, how do you choose?

Faced with the intensified competition of product...

10 hot topics that will definitely become popular on Douyin

Hot Template 01: "A Decade of XXX" For ...