Java实现帧动画的实例代码

时间:2022-10-30 09:50:53

本文讲述了java实现帧动画的实例代码。分享给大家供大家参考,具体如下:

1、效果图

Java实现帧动画的实例代码

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