当前位置: 移动技术网 > IT编程>移动开发>Android > Android仿一点资讯收藏Toast动画效果

Android仿一点资讯收藏Toast动画效果

2019年07月24日  | 移动技术网IT编程  | 我要评论

最近在做一个项,有一个收藏的功能。后来看到了一点资讯的收藏动画,感觉不错,所有自己就实现了一下。

这是效果:

附上完整的代码,其中animation_toast为动画:

<div style="text-align: left;"><span style="font-family: arial, helvetica, sans-serif;"></span></div><pre name="code" class="java">public class collecttoast { 
  private static collecttoast toastcollectsucceed = null; 
  private toast toast = null; 
  private textview text; 
  private collecttoast() {} 
  /** 
   * 单例模式 
   * 
   * @return 
   */ 
  public static collecttoast createtoast() { 
    if (toastcollectsucceed == null) { 
      toastcollectsucceed = new collecttoast(); 
    } 
    return toastcollectsucceed; 
  } 
  /** 
   * 显示toast 
   * 
   * @param context 
   * @param root 
   * @param tvstring 
   * @param result 是否成功 
   */ 
  public toast showtoast(context context, viewgroup root, string tvstring, int duration, boolean result) { 
    toast = null; 
    int styleid = r.style.animation_toast; 
    if (toast == null) { 
      view layout = layoutinflater.from(context).inflate(r.layout.toast_collect_layout, root); 
      text = (textview) layout.findviewbyid(r.id.title_tv); 
      imageview imageview = (imageview) layout.findviewbyid(r.id.iv); 
      if (result) 
        imageview.setbackgrounddrawable(drawableutil.getimagedrawable(context, r.mipmap.doneicon_popup_textpage)); 
      else 
        imageview.setbackgrounddrawable(drawableutil.getimagedrawable(context, r.mipmap.close_popup_textpage)); 
      text.settext(tvstring); 
      toast = new toast(context); 
      toast.setgravity(gravity.center_vertical, 0, 0); 
      toast.setduration(duration); 
      toast.setview(layout); 
      toast.show(); 
    } else { 
      text.settext(tvstring); 
      toast.show(); 
    } 
    //通过反射给toast设置动画 
    try { 
      object mtn = null; 
      mtn = getfield(toast, "mtn"); 
      if (mtn != null) { 
        object mparams = getfield(mtn, "mparams"); 
        if (mparams != null 
            && mparams instanceof windowmanager.layoutparams) { 
          windowmanager.layoutparams params = (windowmanager.layoutparams) mparams; 
          params.windowanimations = styleid; 
        } 
      } 
    } catch (exception e) { 
      e.printstacktrace(); 
    } 
    return toast; 
  } 
  /** 
   * 反射字段 
   * 
   * @param object  要反射的对象 
   * @param fieldname 要反射的字段名称 
   * @return 
   * @throws nosuchfieldexception 
   * @throws illegalaccessexception 
   */ 
  private static object getfield(object object, string fieldname) throws nosuchfieldexception, illegalaccessexception { 
    field field = object.getclass().getdeclaredfield(fieldname); 
    if (field != null) { 
      field.setaccessible(true); 
      return field.get(object); 
    } 
    return null; 
  } 
}</pre><br> 
<div style="text-align:left"><span style="font-family:arial,helvetica,sans-serif"></span></div> 
<pre></pre> 
<br> 
<br> 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网