安卓控制項屬性動畫使用大全

2021-07-05 20:02:52 字數 2535 閱讀 5948

首先寫乙個xml布局檔案,用於顯示效果,如下

然後在activity中抓取所有的按鈕

button alphabutton = (button) findviewbyid(r.id.alpha);

button rotatebutton = (button) findviewbyid(r.id.rotate);

button transbutton = (button) findviewbyid(r.id.translate);

button scalebutton = (button) findviewbyid(r.id.scale);

button setbutton =(button) findviewbyid(r.id.set);

button propbutton = (button) findviewbyid(r.id.prop);

1、透明度動畫

在activity中寫法1:

直接通過物件進行設定

animation alpha = new alphaanimation(0,1);

alpha.setduration(5000);

alphabutton.setanimation(alpha);

寫法2:

通過xml檔案設定

animation alpha2 = animationutils.loadanimation(this,r.anim.animation_alpha);

alphabutton.setanimation(alpha2);

按照把動畫都寫在xml中比較清晰可靠,下面的例子全是依據xml的寫法

//旋轉動畫

animation rotate = animationutils.loadanimation(this, r.anim.animation_rotate);

rotatebutton.setanimation(rotate);

//平移動畫

animation translate = animationutils.loadanimation(this, r.anim.animation_translate);

transbutton.setanimation(translate);

//大小動畫

animation scale = animationutils.loadanimation(this, r.anim.animation_scale);

scalebutton.setanimation(scale);

//多重動畫

animation set = animationutils.loadanimation(this, r.anim.animation_set);

setbutton.setanimation(set);

//逐幀動畫

imageview imglist = (imageview) findviewbyid(r.id.imglist);

animationdrawable animationdrawable = (animationdrawable) imglist.getdrawable();

animationdrawable.start();

//屬性動畫

objectanimator oa=objectanimator.offloat(propbutton, "rotationx", 0.0f, 360f);

oa.setduration(5000);

oa.start();

下面是xml檔案的寫法

1、透明度漸變

<?xml version="1.0" encoding="utf-8"?>

2、旋轉

<?xml version="1.0" encoding="utf-8"?>

3、大小變化

<?xml version="1.0" encoding="utf-8"?>

4、平移

<?xml version="1.0" encoding="utf-8"?>

如果想要加入插補效果,比如平移到頭超出一塊,再彈回來,可以這樣寫

<?xml version="1.0" encoding="utf-8"?>

5、多重屬性變化

<?xml version="1.0" encoding="utf-8"?>

6、幀動畫,注意幀動畫要用到imgview

<?xml version="1.0" encoding="utf-8"?>

安卓屬性動畫的總結

做了乙個月的安卓屬性動畫,要過是對一張進行從一點緩緩展開,然後在上顯示數字,顯示一段時間之後數字消失,消失後也緊跟著緩緩消失的效果。剛開始用的canvas進行對畫實現動畫的效果,通過matrix來手動的調節放大 傾斜的效果近似的模擬展開的效果,後來發現如果用canvas.drawbitmap的方法會...

安卓學習筆記之動畫屬性

這是安卓中做動畫效果比較常用的類他繼承valueanimator類。1 如果我們想實現讓控制項變透明再變回來的效果我們的 可以這樣寫 objectanimator animator objectanimator.offloat 控制項型別,alpha 1f,0f,1f animator.setdur...

安卓幀動畫簡單使用

今天給大家分享乙個安卓的逐幀動畫.下面就簡稱幀動畫了.先說一下優點,簡單,設定簡單,而且動畫流暢.再說乙個最大的缺點,只能用於輕量級的動畫,過大,過多都有可能導致記憶體溢位 常見使用情景.新介面空白頁的載入動畫,下拉重新整理動畫,網路請求載入動畫等等.說完了基礎介紹,下面就介紹一下幀動畫吧.首先最簡...