标签:
1. 初始化项目:主要是几个按钮,分别调起展示销毁和移动等方法.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:weightSum="3"> <Button android:id="@+id/btn_dispaly" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Display" /> <Button android:id="@+id/btn_gone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Gone" /> <Button android:id="@+id/btn_move" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Move" /> </LinearLayout>
public class MainActivity extends Activity implements OnClickListener{ /** * button display */ private Button btnDisplay; /** * button gone */ private Button btnGone; /** * button move */ private Button btnMove; /** * def toast class */ private MyToast myToast; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init() { btnDisplay = (Button) this.findViewById(R.id.btn_dispaly); btnDisplay.setOnClickListener(this); btnGone = (Button) this.findViewById(R.id.btn_gone); btnGone.setOnClickListener(this); btnMove = (Button) this.findViewById(R.id.btn_move); btnMove.setOnClickListener(this); myToast = new MyToast(this); } @Override public void onClick(View v) { if(v.getId() == R.id.btn_dispaly){ myToast.simpleDisplay("show toast for test"); } if(v.getId() == R.id.btn_move){ myToast.moveToast("move toast for test"); } if(v.getId() == R.id.btn_gone){ myToast.destoryToast(); } } }
2. 定义Toast类:MyToast
public class MyToast { /** * View inflater */ private LayoutInflater inflater = null; /** * prop window for toast */ private WindowManager windowManage; /** * Toast show view */ private View view = null; /** * toast show params */ private WindowManager.LayoutParams params = null; /** * * @param context */ public MyToast(Context context) { inflater = LayoutInflater.from(context); params = new WindowManager.LayoutParams(); windowManage = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); } /** * toast display and moveable * @param text */ public void moveToast(String text) { checkParamsEnv(text); // inflate view view = inflater.inflate(R.layout.activity_toast, null); // init toast view TextView tvDesc = (TextView) view.findViewById(R.id.tv_toast_desc); tvDesc.setText(text); // set display location params.gravity = Gravity.CENTER; params.width = WindowManager.LayoutParams.WRAP_CONTENT; params.height = WindowManager.LayoutParams.WRAP_CONTENT; // add touch listener for move view.setOnTouchListener(new OnTouchListener() { int initX,initY; @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { // touch down case MotionEvent.ACTION_DOWN: initX = (int) event.getRawX(); initY = (int) event.getRawY(); break; // touch move case MotionEvent.ACTION_MOVE: int newX = (int) event.getRawX(); int newY = (int) event.getRawY(); int dx = newX - initX; int dy = newY - initY; params.x += dx; params.y += dy; windowManage.updateViewLayout(view, params); initX = (int) event.getRawX(); initY = (int) event.getRawY(); break; // touch up case MotionEvent.ACTION_UP: break; default: break; } return false; } }); // set params flags for keep screen on and no focusable params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON; params.format = PixelFormat.TRANSLUCENT; // toast display type params.type = WindowManager.LayoutParams.TYPE_TOAST; // pop window windowManage.addView(view, params); } /** * simple toast show * @param text */ public void simpleDisplay(String text) { checkParamsEnv(text); view = inflater.inflate(R.layout.activity_toast, null); TextView tvDesc = (TextView) view.findViewById(R.id.tv_toast_desc); tvDesc.setText(text); params.gravity = Gravity.CENTER; params.width = WindowManager.LayoutParams.WRAP_CONTENT; params.height = WindowManager.LayoutParams.WRAP_CONTENT; params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE; params.format = PixelFormat.TRANSLUCENT; params.type = WindowManager.LayoutParams.TYPE_TOAST; windowManage.addView(view, params); } public void destoryToast() { if (view != null) { windowManage.removeView(view); } } private void checkParamsEnv(String text) { if (text == null || "".equals(text)) { return; } } }
标签:
原文地址:http://www.cnblogs.com/cbooy/p/4663030.html