Android仿一点资讯收藏Toast动画效果
作者:wangw-csdn 时间:2022-01-15 18:42:33
最近在做一个项,有一个收藏的功能。后来看到了一点资讯的收藏动画,感觉不错,所有自己就实现了一下。
这是效果:
附上完整的代码,其中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>
来源:http://blog.csdn.net/wangwasdf/article/details/73564244
标签:android,一点资讯,收藏动画
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
基于java集合中的一些易混淆的知识点(详解)
2023-08-29 03:06:26
C#中的DateTime是值类型还是引用类型
2023-08-07 17:54:17
![](https://img.aspxhome.com/file/2023/5/98285_0s.png)
一文详解Java抽象类到底有多抽象
2023-08-27 01:41:26
![](https://img.aspxhome.com/file/2023/8/62168_0s.png)
超简洁java实现双色球若干注随机号码生成(实例代码)
2023-12-03 22:39:08
![](https://img.aspxhome.com/file/2023/9/91119_0s.png)
Java中的字节,字符输出流与字节和字符输入流的简单理解
2022-11-30 01:56:13
Java中instanceof关键字实例讲解
2023-08-04 07:48:31
![](https://img.aspxhome.com/file/2023/5/63305_0s.png)
在IntelliJ IDEA中多线程并发代码的调试方法详解
2022-01-09 03:05:45
![](https://img.aspxhome.com/file/2023/6/67146_0s.jpg)
详细讲解Java的泛型
2021-07-06 02:31:24
详解Java进阶知识注解
2023-08-12 13:11:37
![](https://img.aspxhome.com/file/2023/5/65775_0s.png)
C#检查foreach判读是否为null的方法
2021-10-28 01:37:24
![](https://img.aspxhome.com/file/2023/3/78143_0s.png)
IDEA安装详细步骤(多图预警)
2022-02-28 14:38:39
![](https://img.aspxhome.com/file/2023/7/97787_0s.jpg)
android编程实现对话框的封装实例
2022-02-12 12:58:54
java自定义注解实现前后台参数校验的实例
2023-04-27 23:53:21
![](https://img.aspxhome.com/file/2023/5/92695_0s.png)
mybatis多表查询的实现(xml方式)
2021-07-11 01:21:38
![](https://img.aspxhome.com/file/2023/5/79235_0s.png)
快速了解Java中NIO核心组件
2023-01-01 12:21:13
![](https://img.aspxhome.com/file/2023/8/91538_0s.jpg)
C#实现二叉排序树代码实例
2021-10-10 06:26:12
![](https://img.aspxhome.com/file/2023/3/102223_0s.png)
springboot结合vue实现增删改查及分页查询
2023-11-24 15:53:44
![](https://img.aspxhome.com/file/2023/3/59783_0s.jpg)
JAVAsynchronized原理详解
2023-05-17 12:18:32
![](https://img.aspxhome.com/file/2023/8/80318_0s.png)
C#实现语音视频录制-附demo源码
2023-10-04 13:22:03
![](https://img.aspxhome.com/file/2023/8/91308_0s.png)
C# 获取 PC 序列号的方法示例
2022-11-19 01:40:57