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

Control Cat Short-term Basic Training Camp, Control Cat Double Plan

Introduction to the resources of the Kongpanmao s...

YaYaYa! The 280th Danxia flyer turned out to be this one!

Danxia bird season is here, and the Guangdong Dan...

iOS Programming Basics: How does the Hello World App work?

Translator’s Note: 1. Since this is a technical a...

Android high imitation taxi and other software project source code

Source code introduction This project is modeled ...

Mobile Map Technology Sharing

I am currently working in a domestic map navigati...

Second category e-commerce advertising!

Second-class e-commerce advertising : Tencent adv...

What is user growth operation and what does it do?

From data products to user growth operations , he...

10 ways to monetize TikTok

Douyin is not just a short video tool, but a cont...

How to get a higher ranking in Google keyword advertising promotion?

Google Search has more than 80% market share in t...