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

How hardcore are modern attack helicopters? What weapons can they carry?

In the early days of the Vietnam War, many Americ...

Meizu MX5 hands-on experience with full metal body

Meizu, which releases new phones at a rapid pace,...

Aiti Tribe Stories (7): Meet 51CTO and aim high

[51CTO.com original article] The protagonist of t...

It’s been 10 years! This is the first code of WeChat

WeChat was officially released on January 21, 201...

Can eating mushrooms relieve depression? Science debunks rumors of the week

Eating mushrooms daily may reduce depression and ...

Brand promotion: Brainwash advertising has no effect at all!

Every year there are always a number of brainwash...

How to promote online for free (low cost) (Part 1)

Recently, many people have asked me a question, h...

Introduction to WeChat Circle Points System, How to Play in WeChat Circle?

【WeChat circle points system】 WeChat circle point...