ToastMiui【仿MIUI的带有动画的Toast】
版权声明:本文为HaiyuKing原创文章,转载请注明出处!
前言
仿MIUI的带有动画的Toast
效果图
代码分析
ToastMiui类基于WindowManager
- 为了和Toast用法保持一致,ToastMiui类中也使用了makeText、show、setGravity、setText方法。方便在项目中直接替换Toast。
使用步骤
一、项目组织结构图
注意事项:
1、 导入类文件后需要change包名以及重新import R文件路径
2、 Values目录下的文件(strings.xml、dimens.xml、colors.xml等),如果项目中存在,则复制里面的内容,不要整个覆盖
二、导入步骤
将ToastMiui复制到项目中,并重新import R文件
package com.why.project.toastmiuidemo.views; import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView; import com.why.project.toastmiuidemo.R; /**
* CreateBy HaiyuKing
* Used 仿MIUI的带有动画的Toast效果实现,基于WindowManager【可以控制显示样式、位置、显示时间、动画,不可触发】
* 注意 Toast布局在源码中的布局是采用LinearLayout
*/
public class ToastMiui { /**显示的时间(长)*/
public static final int LENGTH_LONG = 3500;
/**显示的时间(短)*/
public static final int LENGTH_SHORT = 2000; private WindowManager mWindowManager;//整个Android的窗口机制是基于一个叫做 WindowManager
private View mToastView;
private WindowManager.LayoutParams mParams; private Handler mHandler;
private Context mContext; private int mShowTime = LENGTH_SHORT;//记录Toast的显示长短类型LENGTH_LONG/LENGTH_SHORT
private boolean mIsShow;//记录当前Toast的内容是否已经在显示 /**
* 逻辑简单粗暴,直接调用构造函数实例化一个MiuiToast对象并返回。*/
public static ToastMiui MakeText(Context context, CharSequence text, int duration) {
ToastMiui toastMiui = new ToastMiui(context, text, duration);
return toastMiui;
} public static ToastMiui MakeText(Context context, int toastStrId, int showTime) {
ToastMiui toastMiui = new ToastMiui(context, context.getString(toastStrId), showTime);
return toastMiui;
} /**在构造方法中,更多的是对数据的初始化,由于设置布局参数比较多,所以单独抽出一个函数来*/
private ToastMiui(Context context, CharSequence text, int duration){
mContext = context;
mShowTime = duration;//记录Toast的显示长短类型
mIsShow = false;//记录当前Toast的内容是否已经在显示 mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
//通过inflate获取自定义的Toast布局文件
mToastView = LayoutInflater.from(context).inflate(R.layout.toast_custom_view,null);
TextView tv = (TextView) mToastView.findViewById(R.id.tv_toast);
tv.setText(text);
//设置布局参数
setParams();
} /**设置布局参数
* 这个设置参数更多是参考源代码中原生Toast的设置参数的类型
* 在这里我们需要注意的是 mParams.windowAnimations = R.style.anim_view;这个是我们这个仿MIUI的Toast动画实现的基石。*/
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 = R.style.anim_view;//设置进入退出动画效果
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.alpha = 1.0f;
mParams.setTitle("ToastMiui");
mParams.packageName = mContext.getPackageName(); mParams.gravity = Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM;//设置显示的位置
mParams.y = 50;//设置偏移量
} /**
* 1、gravity是输入Toast需要显示的位置,例如CENTER_VERTICAL(垂直居中)、CENTER_HORIZONTAL(水平居中)、TOP(顶部)等等。
* 2、xOffset则是决定Toast在水平方向(x轴)的偏移量,偏移量单位为,大于0向右偏移,小于0向左偏移
* 3、yOffset决定Toast在垂直方向(y轴)的偏移量,大于0向下偏移,小于0向上偏移,想设大值也没关系,反正Toast不会跑出屏幕。*/
public void setGravity(int gravity, int xOffset, int yOffset) {
mParams.gravity = gravity;
mParams.x = xOffset;
mParams.y = yOffset;
} public void setText(CharSequence s){
TextView tv = (TextView) mToastView.findViewById(R.id.tv_toast);
tv.setText(s);
} public void setText(int resId){
TextView tv = (TextView) mToastView.findViewById(R.id.tv_toast);
tv.setText(resId);
} public void show() {
if(!mIsShow) {//如果Toast没有显示,则开始加载显示
mIsShow = true;
mWindowManager.addView(mToastView, mParams);
if (mHandler == null) {
mHandler = new Handler();
}
mHandler.postDelayed(timerRunnable, mShowTime);
}
} private final Runnable timerRunnable = new Runnable() {
@Override
public void run() {
removeView();
}
}; public void removeView() {
if (mToastView != null && mToastView.getParent() != null) {
mWindowManager.removeView(mToastView);
mHandler.removeCallbacks(timerRunnable);
mIsShow = false;
}
}
}
ToastMiui.java
将toast_custom_view.xml文件复制到项目中
<?xml version="1.0" encoding="utf-8"?>
<!-- 自定义显示风格的Toast的布局文件 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_custom_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/toast_custom_view_bg"
android:orientation="vertical"> <TextView
android:id="@+id/tv_toast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="@dimen/toast_custom_text_paddingTB"
android:paddingBottom="@dimen/toast_custom_text_paddingTB"
android:paddingLeft="@dimen/toast_custom_text_paddingLR"
android:paddingRight="@dimen/toast_custom_text_paddingLR"
android:text=""
android:textColor="@android:color/white"
android:textSize="@dimen/toast_custom_text_size"/> </LinearLayout>
toast_custom_view.xml
将toast_custom_view_bg.xml文件复制到项目中
<?xml version="1.0" encoding="utf-8"?>
<!-- 自定义显示风格的Toast的布局文件的背景 -->
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<!-- 圆角 -->
<corners android:radius="@dimen/toast_custom_view_bg_corners" />
<!-- 描边 -->
<stroke
android:width="1dp"
android:color="#666666" />
<!-- 填充 -->
<solid android:color="#666666" /> </shape>
toast_custom_view_bg.xml
将toast_anim_in.xml文件复制到项目中
<?xml version="1.0" encoding="utf-8"?>
<!-- ToastMiui【仿小米带有动画的Toast】进入动画 -->
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="0"
android:toYDelta="85"
android:duration="1"
/>
<translate
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="0"
android:toYDelta="-105"
android:duration="350"
android:fillAfter="true"
android:interpolator="@android:anim/decelerate_interpolator"
/>
<alpha
android:fromAlpha="0"
android:toAlpha="1"
android:duration="100"
/>
<translate
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="0"
android:toYDelta="20"
android:duration="80"
android:fillAfter="true"
android:startOffset="350"
/>
</set>
toast_anim_in.xml
将toast_anim_out.xml文件复制到项目中
<?xml version="1.0" encoding="utf-8"?>
<!-- ToastMiui【仿小米带有动画的Toast】退出动画 -->
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="1"
android:toAlpha="0"
android:duration="800"/>
</set>
toast_anim_out.xml
在dimens.xml中添加以下颜色标记的代码
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen> <!-- **************自定义显示风格的Toast布局文件********************* -->
<!-- 提示文字的大小 -->
<dimen name="toast_custom_text_size">18sp</dimen>
<!-- 提示文字的内边距(上下) -->
<dimen name="toast_custom_text_paddingTB">10dp</dimen>
<!-- 提示文字的内边距(左右) -->
<dimen name="toast_custom_text_paddingLR">20dp</dimen>
<!-- 背景的圆角 -->
<dimen name="toast_custom_view_bg_corners">30dp</dimen>
</resources>
在styles.xml文件中添加以下颜色标记的代码
<resources> <!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style> <!-- ToastMiui【仿小米带有动画的Toast】进入动画和退出动画的引用 -->
<style name="anim_view">
<item name="@android:windowEnterAnimation">@anim/toast_anim_in</item>
<item name="@android:windowExitAnimation">@anim/toast_anim_out</item>
</style> </resources>
三、使用方法
ToastMiui toastMiui = ToastMiui.MakeText(this,"仿MIUI的带有动画的Toast",ToastMiui.LENGTH_LONG);
toastMiui.show();
如果想要修改文本或者显示位置,参考下面的代码:
ToastMiui toastMiui = ToastMiui.MakeText(this,"仿MIUI的带有动画的Toast",ToastMiui.LENGTH_LONG);
toastMiui.setText(getResources().getString(R.string.app_name));
toastMiui.setGravity(Gravity.CENTER,0,0);
toastMiui.show();
混淆配置
无
参考资料
Android应用系列:仿MIUI的Toast动画效果实现(有图有源码)
http://www.cnblogs.com/net168/p/4237528.html?utm_source=tuicool&utm_medium=referral
Android:剖析源码,随心所欲控制Toast显示
http://www.cnblogs.com/net168/p/4058193.html
Android自定义Toast
http://blog.csdn.net/zhangweiwtmdbf/article/details/30031015
项目demo下载地址
https://github.com/haiyuKing/ToastMiuiDemo
ToastMiui【仿MIUI的带有动画的Toast】的更多相关文章
- Android应用系列:仿MIUI的Toast动画效果实现(有图有源码)
前言 相信有些人用过MIUI,会发现小米的Toast跟Android传统的Toast特么是不一样的,他会从底部向上飞入,然后渐变消失.看起来效果是挺不错的,但是对于Android原生Toast是不支持 ...
- Android应用系列:仿MIUI的Toast动画效果实现
前言 相信有些人用过MIUI,会发现小米的Toast跟Android传统的Toast特么是不一样的,他会从底部向上飞入,然后渐变消失.看起来效果是挺不错的,但是对于Android原生Toast是不支持 ...
- Android特效专辑(五)——自定义圆形头像和仿MIUI卸载动画—粒子爆炸
Android特效专辑(五)--自定义圆形头像和仿MIUI卸载动画-粒子爆炸 好的,各位亲爱的朋友,今天讲的特效还是比较炫的,首先,我们会讲一个自定义圆形的imageView,接着,我们会来实现粒子爆 ...
- android指纹识别、拼图游戏、仿MIUI长截屏、bilibili最美创意等源码
Android精选源码 一个动画效果的播放控件,播放,暂停,停止之间的动画 用 RxJava 实现 Android 指纹识别代码 Android仿滴滴打车(滴滴UI)源码 Android高仿哔哩哔哩动 ...
- Swift 带有动画效果的TabBarItem
额...貌似挺长时间没有总结新知识了,最近在看swift,之前swift刚出来的时候大体看了一遍,后来时间长了没看加之swift2.0做了比较大的调整,公司项目也不是用swift写的,也就没怎么看了, ...
- jquery仿搜狐投票动画代码
体验效果:http://hovertree.com/texiao/jquery/21/ 这是一款基于jquery实现的仿搜狐投票动画特效源码,运行该源码可见VS图标首先出现在中间位置,紧接着随着投票比 ...
- 自定义带动画的Toast
一.style样式: 1. // 移动和透明渐变结合的动画 <style name="anim_view"> <item name="@ ...
- javascript仿天猫加入购物车动画效果
javascript仿天猫加入购物车动画效果 注意:首先需要声明的是:代码原思路不是我写的,是在网上找的这种效果,自己使用代码封装了下而已:代码中都有注释,我们最主要的是理解抛物线的思路及在工作中 ...
- 自定义PopupWindow弹出框(带有动画)
使用PopupWindow来实现弹出框,并且带有动画效果 首先自定义PopupWindow public class LostPopupWindow extends PopupWindow { pub ...
随机推荐
- 显著性检测(saliency detection)评价指标之sAUC(shuffled AUC)的Matlab代码实现
AUC_shuffled.m function [score,tp,fp] = AUC_shuffled(saliencyMap, fixationMap, otherMap, Nsplits, st ...
- XSS过滤JAVA过滤器filter 防止常见SQL注入
Java项目中XSS过滤器的使用方法. 简单介绍: XSS : 跨站脚本攻击(Cross Site Scripting),为不和层叠样式表(Cascading Style Sheets, CSS)的缩 ...
- 玩转PHP中的正则表达式
玩转PHP中的正则表达式 检验用户输入.解析用户输入和文件内容,以及重新格式化字符串 级别: 中级 正则表达式提供了一种处理文本的强大方法.使用正则表达式,您可以对用户输入进行复杂的检验.解析用户输入 ...
- Docker的简单实用
一.docker的常用操作 二.其实用docker search images它就是去hub仓库里面查找:https://hub.docker.com/ 对容器的操作: 搜索镜像:[root@mast ...
- SpringBoot---页面跳转之WebMvcConfigurerAdapter
摘要:在springboot中定义自己的方法继承WebMvcConfigurerAdapter方法可以实现扩展springMvc功能,要全面实现接管springmvc就要在自己的方法上加上@Enabl ...
- Keras框架简介
Keras是基于Theano的一个深度学习框架,它的设计参考了Torch,用Python语言编写,是一个高度模块化的神经网络库,支持GPU和CPU.使用文档在这:http://keras.io/,中文 ...
- 分布式系统关注点(15)——「水平&垂直切分」详解
如果第二次看到我的文章,欢迎右侧扫码订阅我哟~
- sqlmap Bool型&延时型 检测策略分析
目录 sqlmap Bool型&延时型 检测策略分析 0x00 预备-queryPage() 0x01 bool型检测策略 判断依据 quick_ratio() 案例 0x02 延时型 判断依 ...
- 在ASP.NET Core中使用EPPlus导入出Excel文件
这篇文章说明了如何使用EPPlus在ASP.NET Core中导入和导出.xls/.xlsx文件(Excel).在考虑使用.NET处理excel时,我们总是寻找第三方库或组件.使用Open Offic ...
- Protocol Buffers(2):编码与解码
目录 Message Structure 解码代码一窥 varint Protobuf中的整数和浮点数 Length-delimited相关类型 小结 参考 博客:blog.shinelee.me | ...