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

Technical topics·Virtual reality

[[162305]] Li Wenxu, Editor-in-Chief of Pinkang I...

What do you talk about and what do you push? Who is "monitoring" your phone?

I mentioned two brands casually. When you look do...

Category Marketing Script Framework for Live Broadcast Rooms

1. The core logic of marketing tactics The core l...

Is low-carbon investment worth looking forward to?

The oil and gas landscape is changing. The indust...

Will live streaming sales end sooner or later?

It is undeniable that live streaming with goods h...

E-commerce event design guide!

Although this year’s 12.12 did not contribute muc...

Android Aiyouman v2.1.3 All comics free to read_Taoduoduo

Android Aiyouman v2.1.3 All comics free to read_T...

Double 11 is here! How do those bidding veterans operate their accounts?

" Double Eleven has begun, has your traffic ...

New ideas for attracting new users: active and passive user acquisition

The article mainly explains in depth and in a sim...