本文講述了java實現幀動畫的實例代碼。分享給大家供大家參考,具體如下:
1、效果圖
2、幀動畫的簡要代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
private imageview bganimview; private animationdrawable manimationdrawable; //初始化 manimationdrawable = new animationdrawable(); bganimview = new imageview(mcontext); bganimview.setbackgrounddrawable(getanimationdrawable(manimationdrawable)); params = new framelayout.layoutparams(viewgroup.layoutparams.wrap_content, viewgroup.layoutparams.wrap_content); params.topmargin = util.div( 176 + 58 ); params.gravity = gravity.center_horizontal; addview(bganimview, params); private animationdrawable getanimationdrawable(animationdrawable manimationdrawable) { int duration = 50 ; manimationdrawable.addframe(mcontext.getresources().getdrawable(r.drawable.loading1), duration); manimationdrawable.addframe(mcontext.getresources().getdrawable(r.drawable.loading2), duration); manimationdrawable.addframe(mcontext.getresources().getdrawable(r.drawable.loading3), duration); manimationdrawable.setoneshot( false ); return manimationdrawable; } //動畫開始 public void animloadingstart() { this .setvisibility(view.visible); if (manimationdrawable != null ) { manimationdrawable.start(); } } //動畫結束 public void animloadingend() { if (manimationdrawable != null ) { manimationdrawable.stop(); } |
3、擴展:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
//x軸平移 public void animy( int y, int nexty, int duration) { linearinterpolator ll = new linearinterpolator(); //勻速 objectanimator animator = objectanimator.offloat(yourview, "translationy" , 0 , 300 ); //300若為負值,就是向上平移 animator.setduration(duration); animator.setinterpolator(ll); animator.start(); } //y軸平移 public void animx( int x, int nextx, int duration) { linearinterpolator ll = new linearinterpolator(); objectanimator animator = objectanimator.offloat(yourview, "translationx" , x, nextx); animator.setduration(duration); animator.setinterpolator(ll); animator.start(); } //縱向壓縮0.5倍 linearinterpolator ll = new linearinterpolator(); //勻速 scaleanimation scaleanimation = new scaleanimation( 1 , 1 , 1 , 0 .5f); //默認從(0,0) scaleanimation.setduration( 500 ); scaleanimation.setinterpolator(ll); scaleanimation.setfillafter( true ); chartview.startanimation(scaleanimation); //橫向壓縮0.5倍 linearinterpolator ll = new linearinterpolator(); scaleanimation scaleanimation = new scaleanimation( 1 , 0 .5f, 1 , 1 ); //默認從(0,0) scaleanimation.setduration( 500 ); scaleanimation.setinterpolator(ll); scaleanimation.setfillafter( true ); chartview.startanimation(scaleanimation); |
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。
原文鏈接:https://blog.csdn.net/meetings/article/details/78785424