自定义带动画的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 ...
随机推荐
- 18-ES6(1)
第18课-ES6(1) 模块化 1.export和import // model.js export default let m = 1; // 出错 export default n = 2; le ...
- Myeclipse出现 java文件中文乱码问题
一.将整个project设置编码UTF-8(UTF-8可以最大的支持国际化) windows->Preferences->general->Workspace->Text fi ...
- .proto 文件转js 文件方法【nodejs】
npm install protobufjs -g pbjs proto\IM.Other.proto -t js>proto\IM.Other.js
- hdu4273Rescue(三维凸包重心)
链接 模板题已不叫题.. 三维凸包+凸包重心+点到平面距离(体积/点积) 体积-->混合积(先点乘再叉乘) #include <iostream> #include<cstd ...
- mysql mybatis-generator plugin 有page实体类的分页
page实体类 package cn.zsmy.tmp; import java.io.Serializable; /** * 分页对象. * */public final class Page im ...
- AOP 面向切面编程
AOP http://blog.csdn.net/xiang_j2ee/article/details/6851963 Android 支持 AspectJ 这个库来实现面向切面编程. 使用 Apac ...
- 第四周作业-yjw
运动策略分析 首先我们可以得到我方击球手质心的位置,球的质心的位置,对方球门中心位置.同时以桌面边缘为坐标轴,建立笛卡尔坐标系.可以明确的一点是,击球手的运动为曲线,球进框的运动为直线,或者折线. 击 ...
- solr的增删改查
solr的配置请查看:http://www.cnblogs.com/byteworld/p/5898651.html 创建Core:(可以复制模版到solrhome\test\conf文件夹中) 简化 ...
- Numpy Study 2----* dot multiply区别
使用numpy时,跟matlab不同: 1.* dot() multiply() 对于array来说,* 和 dot()运算不同 *是每个元素对应相乘 dot()是矩阵乘法 对于matrix来说,* ...
- zookeeper能做什么?
Zookeeper是Hadoop的一个子项目,虽然源自hadoop,但是我发现zookeeper脱离hadoop的范畴开发分布式框架的运用越来越多.今天我想谈谈zookeeper,本文不谈如何使用zo ...