欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android抛物线下载动画制作过程

程序员文章站 2024-02-27 18:34:51
下载动画经常出现在下载需求多的app中,比如游戏下载平台,应用市场…… 先看看效果图: 实现 private void startanim() {...

下载动画经常出现在下载需求多的app中,比如游戏下载平台,应用市场……

先看看效果图:

Android抛物线下载动画制作过程

实现

private void startanim() {  
  //以bitmap创建new imageview
  iv.setdrawingcacheenabled(true); 
  bitmap bitmap = iv.getdrawingcache();  
  imageview logo = new imageview(this);   
  logo.setscaletype(imageview.scaletype.fit_xy);   
  logo.setimagebitmap(bitmap); 
  int[] startlocation = new int[2];  
  iv.getlocationinwindow(startlocation);   
  end.getlocationinwindow(location_download); 
  setanim(logo, startlocation, location_download);
}

设置动画

private void setanim(final imageview logo, int[] startlocation,int[] location_download) {  
 viewgroup animmasklayout = createanimlayout();  
 animmasklayout.addview(logo);// 把动画小球添加到动画层 
 // 计算位移
 final view view = addviewtoanimlayout(logo, startlocation);
 // 动画位移的x坐标   
 int endy = location_download[1] - startlocation[1];
 // 动画位移的y坐标  
 translateanimation translateanimationx = new translateanimation(0,  endx, 0, 0);  
 translateanimationx.setinterpolator(new linearinterpolator());   
 translateanimationx.setrepeatcount(0);// 动画重复执行的次数  
 translateanimationx.setfillafter(true);  translateanimation 
 translateanimationy = new translateanimation(0, 0, 0, endy);   
 translateanimationy.setinterpolator(new accelerateinterpolator());  
 translateanimationy.setrepeatcount(0);// 动画重复执行的次数  
 translateanimationx.setfillafter(true);  animationset set = new 
 animationset(false); 
 set.setfillafter(false);  
 set.addanimation(translateanimationy);  
 set.addanimation(translateanimationx); 
 set.setduration(2000);// 动画的执行时间  
 view.startanimation(set);  // 动画监听事件  
 set.setanimationlistener(new animation.animationlistener() {  
    // 动画的开始    
    @override  
    public void onanimationstart(animation animation) {  
       logo.setvisibility(view.visible);  
   }     
   @override 
   public void onanimationrepeat(animation animation) {  
   }   
   // 动画的结束    
   @override  
   public void onanimationend(animation animation) {      
       logo.setvisibility(view.gone);  
   } 
  });
}

创建动画父布局

private viewgroup createanimlayout() {
  viewgroup rootview = (viewgroup) getwindow().getdecorview();    
  linearlayout animlayout = new linearlayout(this);    
  linearlayout.layoutparams lp = new linearlayout.layoutparams(
    linearlayout.layoutparams.match_parent, linearlayout.layoutparams.match_parent);  
  animlayout.setlayoutparams(lp);   
  animlayout.setid(integer.max_value);  
  animlayout.setbackgroundresource(android.r.color.transparent);   
  rootview.addview(animlayout); 
  return animlayout;
}

设置动画布局参数

private static view addviewtoanimlayout(final view view, int[] location) { 
 int x = location[0]; 
 int y = location[1]; 
 linearlayout.layoutparams lp = new linearlayout.layoutparams(80, 80); 
 lp.leftmargin = x; 
 lp.topmargin = y; 
 view.setlayoutparams(lp); 
 return view;
}

代码就到此结束了,看起来并不难,动手试试吧。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。