美文网首页
Android 自定义Toast 可移动

Android 自定义Toast 可移动

作者: 146ec3176f2f | 来源:发表于2017-12-15 20:00 被阅读0次

    public classCustomToastimplementsView.OnTouchListener {

    privateWindowManagermWM;

    //  private View tvCustom;

    private finalWindowManager.LayoutParamsmParams=newWindowManager.LayoutParams();

    privateContextmContext;

    private floatstartX;

    private floatstartY;

    privateTextViewtvCustom;

    publicCustomToast(Context context) {

    this.mContext= context;

    //得到WindowManager对象

    mWM= (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

    tvCustom=newTextView(mContext);

    tvCustom.setOnTouchListener(this);

    mParams.height= WindowManager.LayoutParams.WRAP_CONTENT;

    mParams.width= WindowManager.LayoutParams.WRAP_CONTENT;

    mParams.flags= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE

    | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;

    mParams.format= PixelFormat.TRANSLUCENT;

    mParams.type= WindowManager.LayoutParams.TYPE_TOAST;

    mParams.type= WindowManager.LayoutParams.TYPE_PRIORITY_PHONE;

    mParams.gravity= Gravity.CENTER_VERTICAL| Gravity.RIGHT;

    mParams.setTitle("Toast");

    }

    @Override

    public booleanonTouch(View v,MotionEvent event) {

    intaction = event.getAction();

    DisplayMetrics outMetrics =newDisplayMetrics();

    mWM.getDefaultDisplay().getMetrics(outMetrics);

    switch(action) {

    caseMotionEvent.ACTION_DOWN:

    startX= event.getRawX();

    startY= event.getRawY();

    break;

    caseMotionEvent.ACTION_MOVE:

    floatmoveX = event.getRawX();

    floatmoveY = event.getRawY();

    floatdiffX = moveX -startX;

    floatdiffY = moveY -startY;

    mParams.x-= diffX;

    mParams.y+= diffY;

    //更新视图

    mWM.updateViewLayout(tvCustom,mParams);

    // 重置前一个点

    startX= moveX;

    startY= moveY;

    break;

    caseMotionEvent.ACTION_UP:

    // X轴方向的动画

    intstartX =mParams.x;

    intendX = outMetrics.widthPixels;

    if(endX /2> startX) {

    endX =0;

    }

    ValueAnimator animatorX = ValueAnimator.ofInt(startX,endX);

    animatorX.setDuration(300);

    animatorX.addUpdateListener(newValueAnimator.AnimatorUpdateListener() {

    @Override

    public voidonAnimationUpdate(ValueAnimator animator) {

    intvalue = (Integer) animator.getAnimatedValue();

    mParams.x= value;

    mWM.updateViewLayout(tvCustom,mParams);

    }

    });

    //Y轴方向的动画

    intstartY =mParams.y;//Y轴的其实位置

    intendY =0;//Y轴的结束位置

    ValueAnimator animatorY = ValueAnimator.ofInt(startY,endY);

    animatorY.setDuration(300);

    animatorY.addUpdateListener(newValueAnimator.AnimatorUpdateListener() {

    @Override

    public voidonAnimationUpdate(ValueAnimator animator) {

    intvalue = (Integer) animator.getAnimatedValue();

    mParams.y= value;

    mWM.updateViewLayout(tvCustom,mParams);

    }

    });

    //设置动画集合

    AnimatorSet set =newAnimatorSet();

    set.playTogether(animatorX);

    set.start();

    break;

    default:

    break;

    }

    return true;

    }

    public voidshow(String s) {

    tvCustom.setText(s);

    mWM.addView(tvCustom,mParams);

    }

    public voidhide() {

    if(tvCustom!=null) {

    if(tvCustom.getParent() !=null) {

    mWM.removeView(tvCustom);

    }

    //把view设置为null

    tvCustom=null;

    }

    }}

    相关文章

      网友评论

          本文标题:Android 自定义Toast 可移动

          本文链接:https://www.haomeiwen.com/subject/otzdwxtx.html