自定义带动画的Toast
一、style样式:
1、 // 移动和透明渐变结合的动画
<style name="anim_view">
<item name="@android:windowEnterAnimation">@anim/anim_in</item>
<item name="@android:windowExitAnimation">@anim/anim_out</item>
</style>
anim_in.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="1"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="0"
android:toYDelta="85" />
<translate
android:duration="350"
android:fillAfter="true"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="0"
android:interpolator="@android:anim/decelerate_interpolator"
android:toYDelta="-105" />
<alpha
android:duration="100"
android:fromAlpha="0"
android:toAlpha="1" />
<translate
android:duration="80"
android:fillAfter="true"
android:fromXDelta="0"
android:fromYDelta="0"
android:startOffset="350"
android:toXDelta="0"
android:toYDelta="20" />
</set>
anim_out.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<alpha
android:duration="800"
android:fromAlpha="1"
android:toAlpha="0" />
</set>
2、 // 缩放效果的动画
<style name="anim_view_scale">
<item name="@android:windowEnterAnimation">@anim/lite_toast_enter</item>
<item name="@android:windowExitAnimation">@anim/lite_toast_exit</item>
</style>
lite_toast_enter.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<!-- 进入动画 -->
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@android:integer/config_longAnimTime"
android:fromXScale="0"
android:toXScale="1"
android:fromYScale="0"
android:toYScale="1"
android:pivotX="50%"
android:pivotY="50%"
android:interpolator="@android:anim/decelerate_interpolator" />
lite_toast_exit.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<!-- 出去动画 -->
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@android:integer/config_longAnimTime"
android:fromXScale="1"
android:toXScale="0"
android:fromYScale="1"
android:toYScale="0"
android:pivotX="50%"
android:pivotY="50%"
android:interpolator="@android:anim/decelerate_interpolator" />
二、自定义的Toast类文件
第一种方法是:
package com.bright.shuiyin.toast;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.Toast;
@SuppressLint("ShowToast")
/** 自定义带动画的Toast */
public class MyCToast {
/** 显示时长 */
boolean mShowTime;
long mShowTimeShort = 2000;
long mShowTimeLong = 3500;
/** 显示的 Y 轴所在的位置 */
int mShowAtY = 140;
/** 是否正在显示 */
boolean mIsShow;
WindowManager mWdm;
View mToastView;
LayoutParams mParams;
/** 动画资源 */
int mStyleId;
/**
* @param context
* @param text
* 提示信息
* @param showTime
* 提示显示的时长,false为短(2000)、true为长(3500)
*/
public MyCToast(Context context, String text, boolean showTime, int styleId) {
// 记录Toast的显示长短类型
this.mShowTime = showTime;
// 记录当前Toast的内容是否已经在显示
this.mIsShow = false;
this.mWdm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
// 通过Toast实例获取当前android系统的默认Toast的View布局
this.mToastView = Toast.makeText(context, text, Toast.LENGTH_SHORT)
.getView();
this.mStyleId = styleId;
setParams();
}
/** 参数的添加:添加动画 */
private void setParams() {
mParams = new WindowManager.LayoutParams();
mParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
mParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
mParams.format = PixelFormat.TRANSLUCENT;
mParams.windowAnimations = mStyleId;
// 设置进入退出动画效果
mParams.type = WindowManager.LayoutParams.TYPE_TOAST;
mParams.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
mParams.gravity = Gravity.CENTER_HORIZONTAL;
mParams.y = mShowAtY;
}
/** 显示:addView */
public void show() {
// 如果Toast没有显示,则开始加载显示
if (!mIsShow) {
mIsShow = true;
// 将其加载到windowManager上
mWdm.addView(mToastView, mParams);
cancelShow();
}
}
/** 获取 Toast 对象 */
public static MyCToast makeText(Context context, String text,
boolean showTime, int styleId) {
MyCToast result = new MyCToast(context, text, showTime, styleId);
return result;
}
/** 退出显示:removeView */
public void cancelShow() {
if (mToastView != null) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mWdm.removeView(mToastView);
mIsShow = false;
}
}, (long) (mShowTime ? 3500 : 2000)); // 这里设置显示的时长,之后执行 removeView
}
}
}
第二种方法是:
package com.bright.shuiyin.toast;
import java.lang.reflect.Field;
import android.content.Context;
import android.view.WindowManager;
import android.widget.Toast;
/**
* 使用反射自定义带动画的Toast
*/
public class MyToast extends Toast {
public MyToast(Context context) {
super(context);
}
/**
* 调用有动画的Toast
* @param context
* @param text
* @param duration
* @param 自定义的动画id
* @return
*/
public static Toast makeTextAnim(Context context, CharSequence text,
int duration, int styleId) {
Toast toast = makeText(context, text, duration);
toast.setText(text);
toast.setDuration(duration);
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;
}
}
三、使用:
第一种的使用:
MyCToast.makeText(MyToastActivity.this, "MyCToast !", false, R.style.anim_view_scale).show();
第二种的使用:
MyToast.makeTextAnim(MyToastActivity.this, "自定义缩放Toast", 0, R.style.anim_view_scale).show();
自定义带动画的Toast的更多相关文章
- [转]Android UI:看看Google官方自定义带旋转动画的ImageView-----RotateImageView怎么写(附 图片淡入淡出效果)
http://blog.csdn.net/yanzi1225627/article/details/22439119 众所周知,想要让ImageView旋转的话,可以用setRotation()让其围 ...
- 微信小程序之自定义模态弹窗(带动画)实例
1.基本需求. 实现用户自定义弹框 带动画(动画可做参靠,个人要是觉得不好看可以自定义动画) 获取弹出框的内容,自定义事件获取 2.案例目录结构 二.程序实现具体步骤 1.弹框index.wxml代码 ...
- Android UI:看看Google官方自定义带旋转动画的ImageView-----RotateImageView怎么写(附 图片淡入淡...)
众所周知,想要让ImageView旋转的话,可以用setRotation()让其围绕中心点旋转,但这个旋转是不带动画的,也就是旋转屏幕时图片噌的一下就转过去了,看不到旋转的过程,此UI体验不大好,为此 ...
- android标题栏上面弹出提示框(二) PopupWindow实现,带动画效果
需求:上次用TextView写了一个从标题栏下面弹出的提示框.android标题栏下面弹出提示框(一) TextView实现,带动画效果, 总在找事情做的产品经理又提出了奇葩的需求.之前在通知栏显示 ...
- android标题栏下面弹出提示框(一) TextView实现,带动画效果
产品经理用的是ios手机,于是android就走上了模仿的道路.做这个东西也走了一些弯路,写一篇博客放在这里,以后自己也可用参考,也方便别人学习. 弯路: 1.刚开始本来用PopupWindow去实现 ...
- Android实现自定义带文字和图片的Button
Android实现自定义带文字和图片的Button 在Android开发中经常会需要用到带文字和图片的button,下面来讲解一下常用的实现办法. 一.用系统自带的Button实现 最简单的一种办法就 ...
- ToastCustom【自定义显示风格的Toast】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 基于系统Toast的自定义显示风格的Toast. 效果图 代码分析 ToastCustom类基于系统Toast,不是继承Toast, ...
- ToastMiui【仿MIUI的带有动画的Toast】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 仿MIUI的带有动画的Toast 效果图 代码分析 ToastMiui类基于WindowManager 为了和Toast用法保持一致 ...
- iOS学习笔记-自定义过渡动画
代码地址如下:http://www.demodashi.com/demo/11678.html 这篇笔记翻译自raywenderlick网站的过渡动画的一篇文章,原文用的swift,由于考虑到swif ...
随机推荐
- 获得本机IP,并且将ip放在CIpAdress里
char szHostName[MAX_PATH + 1]; gethostname(szHostName, MAX_PATH); //得到计算机名 hostent *p = gethostbynam ...
- 渗透杂记-2013-07-13 ms10_061_spoolss
[*] Please wait while the Metasploit Pro Console initializes... [*] Starting Metasploit Console... M ...
- Swift_1基础
// swift中导入类库使用import,不再使用<>和""import Foundation // 输出print("Hello, World!" ...
- DbContext 那些事 —— 数据库初始化
数据库初始化 上图,这个图解释了,数据库初始化的流程,是基于我们在上下文类中的构造函数中传递的参数. 在上面的图中,context类中的base构造器中,可以填入下面的参数: 无参数(No Param ...
- 安装rabbitMQ delayed-messaged
由于一些原因,消息需要延迟发送给消费者,可以用delayed-messaged插件 在 /usr/lib/rabbitmq/lib/rabbitmq_server-3.6.5/plugins目录下 # ...
- 30.Nginx集群搭建笔记
源码安装Nginx: tar -zxvf nginx-1.8.0.tar.gz -C /nginx/ #解压Nginx rpm -ivh keepalived-1.2.13-5.el6_ ...
- 【树莓派】【转】利用USB网卡配置树莓派为无线热点
由于Wifi很慢,基本不可用:树莓派有无线网卡,恰好看到有文章用树莓派来做无线热点,利用树莓派来共享无线网络.比较有用,转发后续尝试. 本文转自:https://www.embbnux.com/201 ...
- css实现div的高度填满剩余空间
css实现div的高度填满剩余空间 .top{ width: 100%; height: 70px;} .bottom{background-color: #cc85d9;width: 100%;po ...
- T-Sql 递归查询(给定节点查所有父节点、所有子节点的方法)
-- 查找所有父节点with tab as( select Type_Id,ParentId,Type_Name from Sys_ParamType_V2_0 where Type_Id=316-- ...
- JAVA 1.4 算术运算
1. 如果在一个算术运算中有int,double,float那么最终运算的结果是double,那么也就是说参与运算的类型和得到的结果:结果一定是参与运算的精度最高的那个类型 2. 算术运算中的除法 i ...