package com.loaderman.toastdemo;
import android.content.Context;
import android.graphics.PixelFormat;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
/**
*
* 自定义归属地吐司
*
private WindowManager mWM;
private View mView;
private final WindowManager.LayoutParams mParams;
private final TextView tvAddress;
private int startX;
private int startY;
private Context mContext;
private int\[\] mIcons = new int\[\]{R.drawable.shape\_address\_normal, R.drawable
.shape\_address\_orange, R.drawable.shape\_address\_blue, R.drawable.shape\_address\_gray,
R.drawable.shape\_address\_green};
public AddressToast(Context ctx) {
mContext = ctx;
//窗口管理器
//窗口: android系统中最顶级的布局, 任何的界面和组件都显示在窗口中, Activity, 状态栏, dialog
//当需要在第三方app中显示布局时, 由于没有activity可以依托,此时可以直接使用WindowManager在窗口上添加布局
mWM = (WindowManager) ctx.getSystemService(Context.WINDOW\_SERVICE);
//初始化布局参数, 定义当前布局的宽高,位置,显示方式等信息
mParams = new WindowManager.LayoutParams();
mParams.height = WindowManager.LayoutParams.WRAP\_CONTENT;//高度包裹内容
mParams.width = WindowManager.LayoutParams.WRAP\_CONTENT;//宽度包裹内容
mParams.format = PixelFormat.TRANSLUCENT;//显示格式,默认就行
//params.windowAnimations = com.android.internal.R.style.Animation\_Toast;//窗口动画
mParams.type = WindowManager.LayoutParams.TYPE\_PHONE;//类型, 吐司类型(不支持触摸)
//params.setTitle("Toast");//标题
//标记, 保持屏幕常亮, 没有焦点, 不可触摸
mParams.flags = WindowManager.LayoutParams.FLAG\_KEEP\_SCREEN\_ON
| WindowManager.LayoutParams.FLAG\_NOT\_FOCUSABLE;
// | WindowManager.LayoutParams.FLAG\_NOT\_TOUCHABLE;
// mParams.x;
// mParams.y;
//初始化布局
// mView = new TextView(ctx);
// mView.setText(text);
// mView.setTextColor(Color.RED);
// mView.setTextSize(25);
mView = View.inflate(ctx, R.layout.toast\_address, null);
tvAddress = (TextView) mView.findViewById(R.id.tv\_address);
//设置窗口布局的触摸事件
mView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION\_DOWN:
System.out.println("按下...");
//1.记录起点坐标
startX = (int) event.getRawX();
startY = (int) event.getRawY();
break;
case MotionEvent.ACTION\_MOVE:
System.out.println("移动...");
//2. 记录移动后的坐标
int moveX = (int) event.getRawX();
int moveY = (int) event.getRawY();
//3. 计算偏移量
int dx = moveX - startX;
int dy = moveY - startY;
//4. 根据偏移量更新布局位置
mParams.x += dx;
mParams.y += dy;
mWM.updateViewLayout(mView, mParams);//更新控件位置
//5. 重新初始化起点坐标
startX = moveX;
startY = moveY;
break;
case MotionEvent.ACTION\_UP:
System.out.println("抬起...");
break;
default:
break;
}
return true;//消费掉此事件
}
});
}
//显示布局
public void show(String text) {
//mView.setText(text);
tvAddress.setText(text);
//获取当前选中的样式
tvAddress.setBackgroundResource(mIcons\[0\]);//重新设置背景
//给窗口添加布局对象
mWM.addView(mView, mParams);
}
//隐藏布局
public void hide() {
if (mWM != null && mView != null) {
//移除窗口布局
//布局没有添加给窗口时, 从窗口移除布局就会出异常
try {
mWM.removeView(mView);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
package com.loaderman.toastdemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private AddressToast mToast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToast = new AddressToast(this);
}
public void display(View view){
mToast.show("自定义的哦");
}
public void hide(View view){
mToast.hide();
}
}
activity_main.xml
<Button
android:id="@+id/btn\_show"
android:layout\_width="wrap\_content"
android:layout\_height="wrap\_content"
android:onClick="display"
android:text="显示"/>
<Button
android:id="@+id/btn\_hide"
android:onClick="hide"
android:layout\_width="wrap\_content"
android:layout\_height="wrap\_content"
android:text="隐藏"/>
toast_address.xml
<TextView
android:id="@+id/tv\_address"
android:layout\_width="wrap\_content"
android:layout\_height="wrap\_content"
android:background="@drawable/shape\_address\_normal"
android:drawableLeft="@drawable/location"
android:text="未知号码"
android:padding="8dp"
android:textColor="@color/white"
android:textSize="18sp"/>
在res/drawable下
shape_address_blue.xml shape_address_gray.xml shape_address_green.xml shape_normal_blue.xml shape_address_orange.xml只需要把下面代码需要以下color即可
在res/values/colors.xml添加需求颜色值即可如
添加权限:
效果图:
手机扫一扫
移动阅读更方便
你可能感兴趣的文章