####自定義歸屬地的Toast
1. 在構(gòu)造方法蜘欲,構(gòu)建Toast的params
> 這段代碼是從Toast的內(nèi)部類TN構(gòu)造方法借鑒得出。
?public AddressToast(Context context){
? ? ? ? ? ?mWM = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
? ? ? ? ? ?this.mContext = context;
? ? ? ? ? ?mParams = new WindowManager.LayoutParams();
? ? ? ? ? ?mParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
? ? ? ? ? ?mParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
? ? ? ? ? ?mParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
? ? ? ? ? ? ? ? ? ?/* | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE*/
? ? ? ? ? ? ? ? ? ? | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
? ? ? ? ? ?mParams.format = PixelFormat.TRANSLUCENT;
? ? ? ? ? ?//mParams.windowAnimations = com.android.internal.R.style.Animation_Toast;
? ? ? ? ? ?mParams.type = WindowManager.LayoutParams.TYPE_PHONE; //此處要添加一個權(quán)限晌柬。 SYSTEM_ALERT_WINDOW
? ? ? ? ? ?mParams.setTitle("Toast");
? ? ? ?}
2. 顯示Toast
?mView = ?(TextView) View.inflate(mContext, R.layout.view_address_toast, null);
? ? ? ?mWM.addView(mView, mParams);
3. 隱藏Toast
? if (mView.getParent() != null) {
? ? ? ? ? ? ? ? ?mWM.removeView(mView);
? ? ? ? ? ? ?}
? ? ? ? ? ? ?mView = null;
4. 移動Toast
? ?1. 對當前的view姥份,執(zhí)行觸摸監(jiān)聽
? ? ? ? ? ?mView.setOnTouchListener(this);
? ?2. 移動toast
? ? ? ? ? ?switch (event.getAction()) {
? ? ? ? ? ?case MotionEvent.ACTION_DOWN: //按下
? ? ? ? ? ? ? ?//event.getX() // 當前的觸摸點到控件的左邊間距
? ? ? ? ? ? ? ?downX = event.getRawX(); // 當前的觸摸點到屏幕的左邊間距
? ? ? ? ? ? ? ?downY = event.getRawY();
? ? ? ? ? ? ? ?break;
? ? ? ? ? ?case MotionEvent.ACTION_MOVE: //移動
? ? ? ? ? ? ? ?moveX = event.getRawX(); // 當前的觸摸點到屏幕的左邊間距
? ? ? ? ? ? ? ?moveY = event.getRawY();
? ? ? ? ? ? ? ?//計算現(xiàn)在移動了多少距離 ?開始坐標(0 ,0】辗薄)〉钏ァ---> (10 盛泡,10)∶葡椤--->(20 ,“了小20】场)
? ? ? ? ? ? ? ?int dx = ?Math.round(moveX - downX );
? ? ? ? ? ? ? ?int dy = ?Math.round(moveY - downY );
? ? ? ? ? ? ? ?//更新位置 一邊移動,一邊重置它的x 和 ?y的坐標點
? ? ? ? ? ? ? ?mParams.x += dx;
? ? ? ? ? ? ? ?mParams.y += dy ;
? ? ? ? ? ? ? ?//根據(jù)最新的參數(shù)去重新確定view的位置
? ? ? ? ? ? ? ?mWM.updateViewLayout(mView, mParams);
? ? ? ? ? ? ? ?//重置downx downy
? ? ? ? ? ? ? ?downX = moveX;
? ? ? ? ? ? ? ?downY = moveY;
? ? ? ? ? ? ? ?break;
小火箭
##小火箭
* 火箭的火焰效果
> 分析得出: 使用的幀動畫實現(xiàn)
1. 在res文件夾里面生成一個文件夾 drawable , 定義一個xml文件
2. 播放動畫
? ? ? ?ImageView view = new ImageView(mContext);
? ? ? ?//設置背景資源
? ? ? ?view.setBackgroundResource(R.drawable.rocket_bg);
? ? ? ?//得到背景資源拴竹,其實就是得到早前的動畫對象
? ? ? ?AnimationDrawable ad = (AnimationDrawable) view.getBackground();
? ? ? ?//播放動畫
? ? ? ?ad.start();
1. 發(fā)射臺的顯示隱藏 與 ?火箭的顯示隱藏 處理邏輯是一樣
2. 火箭的移動 與 早前的號碼歸屬地移動時一樣的悟衩。
###對火箭的發(fā)射條件判斷
> 獲取某一個控件在屏幕中的坐標帶你。
? ? ? ?int [] location = new int[2];
? ? ? ?//獲取發(fā)射臺的x 和 y坐標
? ? ? ?mBottomView.getLocationOnScreen(location);
? ? ? ?int bootomX = location[0];
? ? ? ?int bottomY = location[1];
###對imageView的圖片切換
? ? ? iv.setBackgroundResource() ; // 設置背景資源
? ? ? ?iv.setImageResource (); ? //設置前景資源
###讓火箭往上升
> 核心點: 就是不斷的捕獲現(xiàn)在火箭想要到達的Y坐標栓拜。 ?380 ?---> 0
? ?//定義一個起始位置座泳,和一個結(jié)束位置,然后讓這個數(shù)字開始走幕与,走多久( 500 s)挑势,規(guī)定一個時間。
? ? ? ?ValueAnimator animator = ValueAnimator.ofInt(mRocketParams.y ?, 0 );
? ? ? ?animator.setDuration(600);
? ? ? ?animator.start();
? ? ? ?//對動畫執(zhí)行監(jiān)聽
? ? ? ?animator.addUpdateListener(new AnimatorUpdateListener() {
? ? ? ? ? ?@Override
? ? ? ? ? ?public void onAnimationUpdate(ValueAnimator animation) {
? ? ? ? ? ? ? ?//獲取到現(xiàn)在這個數(shù)字走到了哪里
? ? ? ? ? ? ? ?int value = (Integer) animation.getAnimatedValue();
? ? ? ? ? ? ? ?Log.d("vv", "value=="+value);
? ? ? ? ? ? ? ?mRocketParams.y =value ;
? ? ? ? ? ? ? ?mWM.updateViewLayout(mRocketView, mRocketParams);
? ? ? ? ? ?}
? ? ? ?});
往屏幕上添加一個控件啦鸣。
mWm.addView(view , params);
public class AddressToast implements OnTouchListener {
? ? ? ? ? WindowManager.LayoutParams mParams;
? ? ? ? ? WindowManager.LayoutParams mButtonParams;
? ? ? ? ? ?WindowManager mWM;
? ? ? ? ? ?Context mContext;
? ? ? ? ? private ImageView mView,mButtonView;
? ? public AddressToast(Context context){
? ? ? ? ? mWM = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
? ? ? ? ? this.mContext = context;
? ? ? ? ? initRocketParams();
? ? ? ? ? initButtonParams();
? ? }
? ? //底座的params
? ? private void initButtonParams() {
? ? ? ? ? mButtonParams = new WindowManager.LayoutParams();
? ? ? ? ? mButtonParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
? ? ? ? ? mButtonParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
? ? ? ? ? mButtonParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
? ? ? ? ? ? ? ?/* | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE*/
? ? ? ? ? ? ? ? | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
? ? ? ? ? mButtonParams.format = PixelFormat.TRANSLUCENT;
? ? ? ? ? //mParams.windowAnimations = com.android.internal.R.style.Animation_Toast;
? ? ? ? ? mButtonParams.type = WindowManager.LayoutParams.TYPE_PHONE; //此處要添加一個權(quán)限潮饱。 SYSTEM_ALERT_WINDOW
? ? ? ? ? mButtonParams.setTitle("Toast");
? ? ? ? ? mButtonParams.gravity=Gravity.BOTTOM | Gravity.CENTER;
? ? }
? ? //火箭的params
? ? private void initRocketParams() {
? ? ? ? ? mParams = new WindowManager.LayoutParams();
? ? ? ? ? mParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
? ? ? ? ? mParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
? ? ? ? ? mParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
? ? ? ? ? ? ? ?/* | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE*/
? ? ? ? ? ? ? ? | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
? ? ? ? ? mParams.format = PixelFormat.TRANSLUCENT;
? ? ? ? ? //mParams.windowAnimations = com.android.internal.R.style.Animation_Toast;
? ? ? ? ? mParams.type = WindowManager.LayoutParams.TYPE_PRIORITY_PHONE; //此處要添加一個權(quán)限。 SYSTEM_ALERT_WINDOW
? ? ? ? ? mParams.setTitle("Toast");
? ? ? ? ? mParams.gravity=Gravity.TOP | Gravity.LEFT;
? ? }
? ? public void show(){
? ? ? ? ? ?mView=new ImageView(mContext);
? ? ? ? ? ?mView.setBackgroundResource(R.drawable.rocket);
? ? ? ? ? ?AnimationDrawable background = (AnimationDrawable) mView.getBackground();
? ? ? ? ? ?background.start();
? ? ? ? ? ?mView.setOnTouchListener(this);
? ? ? ? ? mWM.addView(mView, mParams);
? ? }
? ? public void showButton(){
? ? ? ? ? mButtonView=new ImageView(mContext);
? ? ? ? ? ?mButtonView.setBackgroundResource(R.drawable.rocket1);
? ? ? ? ? ?AnimationDrawable ButtonView = (AnimationDrawable) mButtonView.getBackground();
? ? ? ? ? ?ButtonView.start();
? ? ? ? ? ?mWM.addView(mButtonView, mButtonParams);
? ? }
? ? public void hide(){
? ? ? ? ? ? if (mView != null) {
? ? ? ? ? ? ?if (mView.getParent() != null) {
? ? ? ? ? ? ? ? ?mWM.removeView(mView);
? ? ? ? ? ? ?}
? ? ? ? ? ? ?mView = null;
? ? ? ? ?}
? ? }
? ? public void hideButton(){
? ? ? ? ? ? if (mButtonView != null) {
? ? ? ? ? ?if (mButtonView.getParent() != null) {
? ? ? ? ? ? ? ?mWM.removeView(mButtonView);
? ? ? ? ? ?}
? ? ? ? ? ?mButtonView = null;
? ? ? ?}
? ? }
? ? float downX, downY,moveX,moveY;
? ? @Override
? ? public boolean onTouch(View v, MotionEvent event) {
? ? ? ? ? switch (event.getAction()) {
? ? ? ? ? case MotionEvent.ACTION_DOWN:
? ? ? ? ? ? ? ? downX = event.getRawX();
? ? ? ? ? ? ? ? downY = event.getRawY();
? ? ? ? ? ? ? ? showButton();
? ? ? ? ? ? ? ?break;
? ? ? ? ? case MotionEvent.ACTION_MOVE:
? ? ? ? ? ? ? ? moveX = event.getRawX();
? ? ? ? ? ? ? ? moveY = event.getRawY();
? ? ? ? ? ? ? ? int dx=Math.round(moveX-downX);
? ? ? ? ? ? ? ? int dy=Math.round(moveY-downY);
? ? ? ? ? ? ? ? mParams.x += dx;
? ? ? ? ? ? ? ? mParams.y += dy;
? ? ? ? ? ? ? ? //更新位置
? ? ? ? ? ? ? ? mWM.updateViewLayout(mView, mParams);
? ? ? ? ? ? ? ? downX=moveX;
? ? ? ? ? ? ? ? downY=moveY;
? ? ? ? ? ? ? ? //TODO 判斷是否滿足條件
? ? ? ? ? ? ? ? if(isReadyToFly()){
? ? ? ? ? ? ? ? ? ? ?System.out.println("滿足條件诫给。香拉。啦扬。");
? ? ? ? ? ? ? ? ? ? ?mButtonView.setBackgroundResource(R.drawable.desktop_bg_tips_3);
? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ? ?System.out.println("不滿足條件。凫碌。扑毡。。证鸥。僚楞。勤晚。");
? ? ? ? ? ? ? ? ? ? ?mButtonView.setBackgroundResource(R.drawable.rocket1);
? ? ? ? ? ? ? ? ? ? ?AnimationDrawable ButtonView = (AnimationDrawable) mButtonView.getBackground();
? ? ? ? ? ? ? ? ? ? ?ButtonView.start();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ?break;
? ? ? ? ? case MotionEvent.ACTION_UP:
? ? ? ? ? ? ? ?if(isReadyToFly()){
? ? ? ? ? ? ? ? ? ? //獲取屏幕寬度
? ? ? ? ? ? ? ? ? ? getWidth();
? ? ? ? ? ? ? ? ? ? //飛起
? ? ? ? ? ? ? ? ? ? fiy();
? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?hideButton();
? ? ? ? ? ? ? ?break;
? ? ? ? ? default:
? ? ? ? ? ? ? ?break;
? ? ? ? ? }
? ? ? ? ? return false;
? ? }
? ? private void fiy() {
? ? ? ? ? ValueAnimator animator=ValueAnimator.ofInt( mParams.y , 0);
? ? ? ? ? animator.setDuration(600);
? ? ? ? ? animator.start();
? ? ? ? ? animator.addUpdateListener(new AnimatorUpdateListener() {
? ? ? ? ? ? ? ?@Override
? ? ? ? ? ? ? ?public void onAnimationUpdate(ValueAnimator animation) {
? ? ? ? ? ? ? ? ? ? int value = (Integer) animation.getAnimatedValue();
? ? ? ? ? ? ? ? ? ? mParams.y=value;
? ? ? ? ? ? ? ? ? ? mWM.updateViewLayout(mView, mParams);
? ? ? ? ? ? ? ?}
? ? ? ? ? });
? ? ? ? ? Intent intent=new Intent(mContext,SmokeActivity.class);
? ? ? ? ? // 由于咱們這里啟動activity使用的上下文并不是activity的上下文枉层, 系統(tǒng)在啟動界面的時候
? ? ? ? ? //會對上下文進行檢驗操作。如果activity的對象赐写,那么需要添加一個標記鸟蜡,否則不允許啟動界面。
? ? ? ? ? intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
? ? ? ? ? mContext.startActivity(intent);
? ? }
? ? private void getWidth() {
? ? ? ? ? Display display = mWM.getDefaultDisplay();
? ? ? ? ? Point point=new Point();
? ? ? ? ? display.getSize(point);
? ? ? ? ? int width=point.x;
? ? ? ? ? mParams.x=width/2-mView.getWidth()/2;
? ? ? ? ? //TODO 屏幕中間
? ? ? ? ? mWM.updateViewLayout(mView, mParams);
? ? ? ? ? mWM.updateViewLayout(mButtonView, mButtonParams);
? ? }
? ? private boolean isReadyToFly() {
? ? ? ? ? //獲取發(fā)射臺的xy坐標
? ? ? ? ? int[] location=new int[2];
? ? ? ? ? mButtonView.getLocationOnScreen(location);
? ? ? ? ? int butx=location[0];
? ? ? ? ? int buty=location[1];
? ? ? ? ? mView.getLocationOnScreen(location);
? ? ? ? ? int viewx=location[0];
? ? ? ? ? int viewy=location[1];
? ? ? ? ? boolean isTop=buty-viewy<=mView.getHeight()/2;
? ? ? ? ? boolean isLeft=butx-viewx<=mView.getWidth()/2;
? ? ? ? ? boolean isRight=(mButtonView.getWidth()+butx)-viewx>=mView.getWidth()/2;
? ? ? ? ? return isTop && isLeft && isRight;
? ? }
}