快速弹出窗口的(二):自定义toast


1.写一个类.toast能够显示的关键在与windowMainger 的addview方法. 和remove方法.
 自定义toast代码
    
    
  1. public class LocationToast implements OnTouchListener {
  2. private Context context;
  3. WindowManager mWM;
  4. private float startX;
  5. private float startY;
  6. private TextView textView;
  7. private WindowManager.LayoutParams params;
  8. public LocationToast(Context context) {
  9. this.context = context;
  10. params = new WindowManager.LayoutParams();
  11. // 视图的高度
  12. params.height = WindowManager.LayoutParams.WRAP_CONTENT;
  13. // 视图的宽度
  14. params.width = WindowManager.LayoutParams.WRAP_CONTENT;
  15. // 图片的格式
  16. params.format = PixelFormat.TRANSLUCENT;
  17. // 设置显示的类型
  18. params.type = WindowManager.LayoutParams.TYPE_PRIORITY_PHONE;
  19. params.setTitle("Toast");
  20. params.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
  21. | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
  22. // | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE 不可触摸
  23. }
  24. public void show(String text) {
  25. //得到窗口管理器
  26. mWM = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
  27. if (textView == null) {
  28. textView = (TextView) View.inflate(context, R.layout.view_location, null);
  29. textView.setBackgroundResource(R.drawable.location_bg0);
  30. }
  31. textView.setText(text);
  32. textView.setOnTouchListener(this);
  33. //通过这个方法,把view添加动窗口
  34. mWM.addView(textView, params);
  35. }
  36. public void hide() {
  37. if (textView != null) {
  38. if (textView.getParent() != null) {
  39. //通过这个方法把view从窗口移除
  40. mWM.removeView(textView);
  41. }
  42. textView = null;
  43. }
  44. }
  45. @Override
  46. public boolean onTouch(View v, MotionEvent event) {
  47. // 获取事件的类型
  48. switch (event.getAction()) {
  49. // 手指按下
  50. case MotionEvent.ACTION_DOWN:
  51. // 获取起始点的坐标
  52. startX = event.getRawX();
  53. startY = event.getRawY();
  54. break;
  55. // 手指移动
  56. case MotionEvent.ACTION_MOVE:
  57. // 获取当前点的坐标
  58. float currentX = event.getRawX();
  59. float currentY = event.getRawY();
  60. // 移动的距离
  61. float gapX = currentX - startX;
  62. float gapY = currentY - startY;
  63. params.x += gapX;
  64. params.y += gapY;
  65. startX = currentX;
  66. startY = currentY;
  67. mWM.updateViewLayout(textView, params);
  68. break;
  69. }
  70. return false;
  71. }
  72. }
2..主页面的代码
    
    
  1. public class MainActivity extends AppCompatActivity {
  2. private LocationToast mToast;
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_main);
  7. mToast = new LocationToast(this);
  8. }
  9. public void go(View view) {
  10. mToast.show("你好吗");
  11. }
  12. }

3.主页面的布局和toast的布局
    
    
  1. <RelativeLayout
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. tools:context="com.example.mytoastdemo.MainActivity">
  7. <Button
  8. android:onClick="go"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:text="点我"/>
  12. </RelativeLayout>
     
     
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TextView xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:drawableLeft="@drawable/location"
  6. android:padding="8dp"
  7. android:text="fdjlsk" >
  8. </TextView>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值