今天說點簡單有意思的東西減減壓空凸,迎接即將來臨的春節(jié)。
之前有個產品Toolbar進行顏色漸變,如下:
演示
效果如絲綢般順滑。
思考了很久
不影響apk體積购啄,還如此順滑,只有shape進行漸變了狮含。
漸變以后怎么進行動畫效果了几迄?
對,動畫!原諒我平時不愛加特效解孙,去android開發(fā)者平臺(https://developer.android.google.cn/index.html 無需翻墻,google回歸的第一步)進行了查看弛姜,animation-list動畫集合,他有什么作用荠商?
進一步學習了解它掠剑。
首先創(chuàng)建漸變drawable
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#7141e2"
android:endColor="#d46cb3"
//漸變角度(45度倍數(shù)進行調整)
android:angle="0"/>
</shape>
帶動畫的drawable.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/color1"
android:duration="300" />
<item
android:drawable="@drawable/color2"
android:duration="300" />
<item
android:drawable="@drawable/color3"
android:duration="300" />
</animation-list>
animation-list可以設置每個文件的動畫時間属铁,次數(shù)焦蘑,形成一個幀動畫
應用:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:background="@drawable/toolbar_list"
android:layout_height="?android:attr/actionBarSize">
</LinearLayout>
部分實現(xiàn)代碼:
.......
anim = (AnimationDrawable) linearLayout.getBackground();
//更改drawable結束全局漸變持續(xù)時間,使動畫更加順滑。
//(此方法很重要,如果不設置動畫會很機械化)
anim.setExitFadeDuration(500);
//動畫執(zhí)行一次
anim.setOneShot(true);
anim.start();
........