Android imageView 實現小紅點(動畫呼吸效果)

展示頁面:
<ImageView
    android:id="@+id/img_redpoint"
    android:layout_width="17dp"
    android:layout_height="17dp"
    android:layout_gravity="right"
    android:layout_marginTop="-1dp"
    android:layout_marginRight="-5px"
    android:src="@drawable/animati_redpoint"
    />

 

animati_redpoint.xml代碼:(android:drawable表示使用的動畫幀,android:duration表示播放時間間隔 oneshot=false表示循環播放

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">
    <item android:drawable="@drawable/frame01" android:duration="240"/>
    <item android:drawable="@drawable/frame02" android:duration="240"/>
    <item android:drawable="@drawable/frame03" android:duration="240"/>
    <item android:drawable="@drawable/frame04" android:duration="240"/>
    <item android:drawable="@drawable/frame05" android:duration="240"/>
</animation-list>

 

frame01.xml文件代碼(frame02,frame03...裏面代碼一樣,只有顏色和stroke的width不一樣):

原理是控制圓點邊框(白色,與背景顏色一樣)的大小來造成視覺實現圓點本身的大小變化

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval"
    android:useLevel="false">
    <solid android:color="@color/red_point" />
    <stroke
        android:width="3dp"
        android:color="@color/white" />
</shape>

 

後臺Java代碼:這裏是開啓動畫的

AnimationDrawable drawable=(AnimationDrawable) (R.id.img_redpoint).getDrawable();

drawable.start();

效果展示:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章