onWindowFocusChanged了解

在Activity的生命周期中,onCreate()--onStart()--onResume()都不是窗体Visible的时间点,真正的窗体完成初始化可见获取焦点可交互是在onWindowFocusChanged()方法被执行时,而这之前,对用户的操作需要做一点限制。
比如我们在做OTT项目时候,我们就是在这onWindowFocusChanged来获取主按键的具体位置和宽高的,而在其他标准生命周期的接口中调用都是获取不到的,比如在onResume,onStart中都获取不到信息。
这个onWindowFocusChanged指的是这个Activity得到或者失去焦点的时候 就会call。
也就是说 如果你想要做一个Activity一加载完毕,就触发什么的话 完全可以用这个!!!
使用一个view的getWidth() getHeight() 方法来获取该view的宽和高,返回的值却为0。

如果这个view的长宽很确定不为0的话,那很可能是你过早的调用这些方法,也就是说在这个view被加入到rootview之前你就调用了这些方法,返回的值自然为0.

下面附一个,随手指移动的图片在抬起后贴边的例子:

package com.example.administrator.myapplication;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.kuxuan.allence.recyclerview.BaseActivity;

/**
 * Created by Administrator on 2017/11/15 0015.
 */

public class First_Activity1 extends BaseActivity {

//    @BindView(R.id.recyclerview)
//    RecyclerView recyclerView;
//
//    @BindView(R.id.smartfreshlayout)
//    SmartRefreshLayout smartRefreshLayout;
//
//    private List<String> mDatas;

    ProgressDialog mLoadingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sttingview1);

        initView();

        inItData();


    }
    ImageView image;
    int startX;
    int startY;
    float lastX, lastY;
    private int containerWidth;
    private int containerHeight;
    int height;
    int width;
    int statusBarHeight1 = -1;
    int top1;
    int left1;
    int right1;
    int bottom1;
    int x1=0;
    ObjectAnimator animator;
    boolean flag=true;
    LinearLayout.LayoutParams params;
    LinearLayout lin;
    private void initView() {

        lin = (LinearLayout) findViewById(R.id.lin);

        image = (ImageView) findViewById(R.id.image);
        params = (LinearLayout.LayoutParams) image.getLayoutParams();

        WindowManager wm = getWindowManager();

        height = wm.getDefaultDisplay().getHeight();

        width = wm.getDefaultDisplay().getWidth();


        //获取status_bar_height资源的ID
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            statusBarHeight1 = getResources().getDimensionPixelSize(resourceId);
        }


        image.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getActionMasked()) {
                    case MotionEvent.ACTION_DOWN:
                        lastX =  event.getRawX();
                        lastY =  event.getRawY();
                        return true;
                    case MotionEvent.ACTION_MOVE:
                        //  不要直接用getXgetY,这两个获取的数据已经是经过处理的,容易出现图片抖动的情况
                        float distanceX = lastX - event.getRawX();
                        float distanceY = lastY - event.getRawY();

                        float nextY = image.getY() - distanceY;
                        float nextX = image.getX() - distanceX;

                        // 不能移出屏幕
                        if (nextY < 0) {
                            nextY = 0;
                        } else if (nextY > containerHeight - image.getHeight()) {
                            nextY = containerHeight - image.getHeight();
                        }
                        if (nextX < 0)
                            nextX = 0;
                        else if (nextX > containerWidth - image.getWidth())
                            nextX = containerWidth - image.getWidth();

                        // 属性动画移动
                        ObjectAnimator y = ObjectAnimator.ofFloat(image, "y", image.getY(), nextY);
                        ObjectAnimator x = ObjectAnimator.ofFloat(image, "x", image.getX(), nextX);

                        AnimatorSet animatorSet = new AnimatorSet();
                        animatorSet.playTogether(x, y);
                        animatorSet.setDuration(0);
                        animatorSet.start();


                        lastX = event.getRawX();
                        lastY = event.getRawY();

                        break;

                    case MotionEvent.ACTION_UP:
                        Log.e("my","getTop:"+image.getTop()+"**"+"getLeft:"+image.getLeft()+"**"+"getRight:"+image.getRight()+"**"+"getBottom:"+image.getBottom());
                        Log.e("my","getY:"+image.getY()+"**"+"getX"+image.getX());
                        x1 =(containerWidth - image.getWidth());
                        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(image, "x", image.getX(), x1);

                        objectAnimator.addListener(new Animator.AnimatorListener() {
                            @Override
                            public void onAnimationStart(Animator animation) {

                            }

                            @Override
                            public void onAnimationEnd(Animator animation) {

                                Log.e("my","getTop:"+image.getTop()+"**"+"getLeft:"+image.getLeft()+"**"+"getRight:"+image.getRight()+"**"+"getBottom:"+image.getBottom());
                                Log.e("my","getY:"+image.getY()+"**"+"getX"+image.getX());

                            }

                            @Override
                            public void onAnimationCancel(Animator animation) {

                            }

                            @Override
                            public void onAnimationRepeat(Animator animation) {

                            }
                        });

                        objectAnimator.setDuration(1000);
                        objectAnimator.start();
                        Log.e("my","getTop:"+image.getTop()+"**"+"getLeft:"+image.getLeft()+"**"+"getRight:"+image.getRight()+"**"+"getBottom:"+image.getBottom());
                        Log.e("my","getY:"+image.getY()+"**"+"getX"+image.getX());
                        break;


                }
                return false;
            }
        });

    }



    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        // 这里来获取容器的宽和高
        if (hasFocus) {
            containerHeight = lin.getHeight();
            containerWidth = lin.getWidth();
        }
    }




    /**
     * 属性动画
     * 平移
     */
    private void setAnimate1(View view, final int x){
        //      imageView中凡是有getset方法的属性,都可以通过属性动画操作
        //      创建属性动画对象,并设置移动的方向和偏移量
        //      translationXimageview的平移属性
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(image, "x", image.getX(), x);
        objectAnimator.setDuration(1000);
        objectAnimator.start();


    }




    private void inItData() {



    }



}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值