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

Ding Tianyu - Technical Practice Guide, Make Money from Scratch in Stock Trading

Ding Tianyu - Technical Practice Guide, Make Mone...

Chi = red, adzuki bean ≠ red adzuki bean? ?

Are you confused about the difference between adz...

Hu Q&A: How to measure a temperature of 100 million degrees?

How to measure a temperature of 100 million degre...

Perfect Diary’s Methodology for Explosive Products

Brands such as Perfect Diary , Hua Xizi, and Cha ...

How to turn users who hate your product into the most loyal ones

During my short 5-year operation career, I spent ...

Talk about the traffic monetization of self-media

I posted an advertisement two days ago about a ce...

What will the weather be like tomorrow? AI predicts

The Earth we live on is experiencing one of the h...

Deep dive into the four major ways to grow educational apps

Online education has been a very hot field in rec...

Antioxidant King! People who often eat these 6 types of food really age slowly

Planning丨Zhong Yanping Visual丨Zhu Hangyue Design丨...

Zhihu Marketing Promotion Strategy for 2019!

It is becoming increasingly difficult to promote!...