1.单例的toast
参考:Android 特殊的单例Toast(防止重复显示)
public class MyToast { /** 之前显示的内容 */ private static String oldMsg ; /** Toast对象 */ private static Toast toast = null ; /** 第一次时间 */ private static long oneTime = 0 ; /** 第二次时间 */ private static long twoTime = 0 ; /** * 显示Toast * @param context * @param message */ public static void showToast(Context context, String message){ if(toast == null){ toast = Toast.makeText(context, message, Toast.LENGTH_SHORT); toast.show() ; oneTime = System.currentTimeMillis() ; }else{ twoTime = System.currentTimeMillis() ; if(message.equals(oldMsg)){ if(twoTime - oneTime > Toast.LENGTH_SHORT){ toast.show() ; } }else{ oldMsg = message ; toast.setText(message) ; toast.show() ; } } oneTime = twoTime ; } }原理说明:其实就是计算两次调用showToast的时间,注意,此处所有变量都是静态的,两次调用toast的的时间如果小于
Toast.LENGTH_SHORT,就更改一下要显示的内容,然后再显示,如果大于这个值就直接显示内容
2.自定义布局及显示位置,背景文,字颜色(半自定义)
参考:自定义Toast
public void toast(View view){ //新建toast对象,然后给他加效果 Toast toast = Toast.makeText(this, "xixi33333333", Toast.LENGTH_SHORT); //出现在右上方,这个时候基准点是右上点,后面的数字是设置离右上点x轴10px,y轴100px toast.setGravity(Gravity.TOP|Gravity.RIGHT,10,100); TextView textView = new TextView(this); textView.setText("hahahh"); textView.setTextSize(18); textView.setTextColor(Color.GREEN); //获取toast的view,如果强转一下可以添加,不强专,可以直接setview方法,完全用自己的布局 //toast.setView(textView); LinearLayout ll = (LinearLayout) toast.getView(); //添加到原view的上方,也就是第0个位置 ll.addView(textView,0); toast.show(); }
3.自己写toast,完全自定义(可自定义显示时间)
package com.z.c;
import android.content.Context;
import android.graphics.PixelFormat;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
/**
* Created by z on 2016/5/9.
*/
public class CustomToast {
private Context mContext;
private WindowManager mWM;
private View mView;
private TextView mTvAddress;
private int mStartX;
private int mStartY;
private WindowManager.LayoutParams mParams;
/**
* 初始化toast
*
* @param context
*/
public void initToast(Context context) {
mContext = context;
mWM = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
mParams = new WindowManager.LayoutParams();
mParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
mParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
mParams.format = PixelFormat.TRANSLUCENT; // 显示效果
//如果设置为这个值:TYPE_SYSTEM_ALERT,那么
mParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
// mParams.type = WindowManager.LayoutParams.TYPE_TOAST;//主要就是这句代码,把type设置成toast类型即可
//保存屏幕常亮,根据情况而定,多在播放视频时用
mParams.flags = //WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
//不允许获得焦点,toast的通性
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
//不允许接收触摸事件
// | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
}
/**
* 显示toast
*
* @param context
* @param address
*/
public void showCustomToast(Context context, String address) {
initToast(context);
mView = View.inflate(mContext, R.layout.view_toast_address, null);
mTvAddress = (TextView) mView.findViewById(R.id.toast_tv_address);
mTvAddress.setText(address);
mWM.addView(mView, mParams);//核心代码
}
/**
* 隐藏toast
*/
public void hideCustomToast() {
if (mView != null) {
if (mView.getParent() != null) {
mWM.removeView(mView);//核心代码
}
mView = null;
}
}
}
//至于自定义显示时间,可以定义计时器,到时间就调用隐藏toast方法,这里就不实现了,大家自己动手吧
4.toast的进出场动画
mParams.windowAnimations = R.style.dialog_anim;//在initToast方法中,添加这句代码就好了,至于里面的内容麻,欢迎看我的前一篇帖子,popupwindow的动画,附送部分,不重复造轮子了
popupwindow你需要的都在这里,看完这篇,你想要什么效果都可以,附送动画解析
5.高级篇:随手指拖动的toast悬浮窗,可悬浮到其他窗口
代码如下:其实跟之前一样的,只是添加了拖动的功能,设置了触摸监听事件
package com.z.c;
import android.content.Context;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;
/**
* Created by z on 2016/5/9.
*/
public class CustomToast {
private Context mContext;
private WindowManager mWM;
private View mView;
private TextView mTvAddress;
private int mStartX;
private int mStartY;
private WindowManager.LayoutParams mParams;
/**
* 初始化toast
*
* @param context
*/
public void initToast(Context context) {
mContext = context;
mWM = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
mParams = new WindowManager.LayoutParams();
mParams.windowAnimations = R.style.dialog_anim;
mParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
mParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
mParams.format = PixelFormat.TRANSLUCENT; // 显示效果
//如果设置为这个值:TYPE_SYSTEM_ALERT,那么
mParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
mParams.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
// |WindowManager.LayoutParams.FLAG_FULLSCREEN
// |WindowManager.LayoutParams. FLAG_WATCH_OUTSIDE_TOUCH;
//不允许获得焦点,toast的通性
// WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
//不允许接收触摸事件
// | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
}
/**
* 显示toast
*
* @param context
* @param address
*/
public void showCustomToast(Context context, String address) {
initToast(context);
mView = View.inflate(mContext, R.layout.view_toast_address, null);
mTvAddress = (TextView) mView.findViewById(R.id.toast_tv_address);
mTvAddress.setText(address);
mView.setBackgroundColor(Color.BLUE);
mView.setLayoutParams(mParams);
//mView.setAnimation(R.animator.ani);
mTvAddress.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext,"hah",0).show();
Log.d("xii","");
}
});
mView.setOnTouchListener(new View.OnTouchListener() {
int lastX, lastY;
int paramX, paramY;
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
lastX = (int) event.getRawX();
lastY = (int) event.getRawY();
paramX = mParams.x;
paramY = mParams.y;
break;
case MotionEvent.ACTION_MOVE:
int dx = (int) event.getRawX() - lastX;
int dy = (int) event.getRawY() - lastY;
mParams.x = paramX + dx;
mParams.y = paramY + dy;
// 更新悬浮窗位置
mWM.updateViewLayout(mView, mParams);
break;
}
return true;
}
});
mWM.addView(mView, mParams);
}
/**
* 隐藏toast
*/
public void hideCustomToast() {
if (mView != null) {
if (mView.getParent() != null) {
mWM.removeView(mView);
}
mView = null;
}
}
}
在Activity中调用:启动一个Service
startService(new Intent(this, MyService.class));
Service中代码:
package com.z.c;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class MyService extends Service {
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
CustomToast toast = new CustomToast();
toast.showCustomToast(this,"hah");//显示Toast
super.onCreate();
}
}
注意:全局显示的系统弹出框也就是params的type为TYPE_SYSTEM_ALERT时必须有权限:
如果是用Toast的type可以不用权限
大坑:(Activity中开启悬浮窗,自己的程序中可以拖动,其他程序中不可以,怎么办?)这个全局的悬浮窗必须在Service中开启并响应拖动事件,否则在桌面无法响应拖动和点击事件,原因:Service可以长期运行后台,而不再Service中,退出窗口的时候有些悬浮窗需要的东西就销毁了,所以不行,这个坑耽误了很长时间
效果图如下: