In the Android Notification display, we usually display some static elements, even if we use a custom layout, becauseRemoteViews
has many limitations, such as only those basic controls are used. Controls and interface updates must also pass the various set methods provided by RemoteViews
. But sometimes, we just want to display animation, because animation can provide better visual effects. Then this article provides two simpler methods.
(For code examples, see: https://gitee.com/spectre1225/notification-anim-demo)
Method 1: Use ViewFlipper to achieve frame-by-frame animation effects
The first method is to use the controls supported by RemoteViews
with multiple to display in a loop to achieve a similar The effect of frame-by-frame animation. The corresponding content in the custom notification layout is:ViewFlipper
ImageView
<ViewFlipper
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="end|center_vertical"
android:autoStart="true"
android:flipInterval="90">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/stat_sys_battery_charge_anim0" />
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/stat_sys_battery_charge_anim15" />
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/stat_sys_battery_charge_anim28" />
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/stat_sys_battery_charge_anim43" />
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/stat_sys_battery_charge_anim57" />
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/stat_sys_battery_charge_anim71" />
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/stat_sys_battery_charge_anim85" />
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/stat_sys_battery_charge_anim100" />
</ViewFlipper>
No additional settings are required in Java code. Note that ViewFlipper
must be set to android:autoStart="true"
so that the animation will play automatically.
- Advantages: All versions of the system have good compatibility and can be used.
- Disadvantages: There are too many ImageViews and a lot of code, making modification and replacement troublesome.
Method 2: Use AnimatedImageDrawable to display GIF animation
A new Drawable is introduced in Android 9.0 to display GIF images: AnimatedImageDrawable
, and the corresponding xml tag is <animated-image>
. In this way, we can Directly place a GIF imageic_test_gif.gif
into the drawable directory, and then create a new oneic_anim_gif.xml
to reference:
<?xml version="1.0" encoding="utf-8"?>
<animated-image xmlns:android="http://schemas.android.com/apk/res/android"
android:autoStart="true"
android:autoMirrored="true"
android:src="@drawable/ic_test_gif" />
Write directly in the custom notification layout:
<ImageView
android:id="@+id/iv_logo"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/ic_anim_gif" />
HereImageView
'ssrc
does not directly quote the gif image because it needs to use animated-image
's< a i=4> attribute, after all, cannot directly take out the drawable of . autoStart
RemoteViews
ImageView
Considering system version compatibility, you can choose to set animation resources toImageView
in the code:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
remoteViews.setImageViewResource(R.id.iv_logo, R.drawable.ic_anim_gif);
}
The advantages and disadvantages of this method are as follows:
- Advantages: Few resources, one gif, only one xml of
animated-image
, and the replacement is simple. - Disadvantages: It can only be used on Android 9.0 and above systems.