CoordinatorLayout的使用如此简单

https://www.jianshu.com/p/72d45d1f7d55

package com.example.test190803;


import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;




public class MainActivity extends AppCompatActivity {

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

package com.example.test190803;

import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import androidx.coordinatorlayout.widget.CoordinatorLayout;

/**
 * @author writing
 * @time 2019/8/3 15:15
 * @note ${TODO}
 */
public class MyBehavior extends CoordinatorLayout.Behavior
package com.example.test190803;

import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import androidx.coordinatorlayout.widget.CoordinatorLayout;

/**
 * @author writing
 * @time 2019/8/3 15:24
 * @note ${TODO}
 */
public class TempView extends View {
    private Context context;
    private int screenWidth;
    private int screenHeight;
    private int lastY;
    private int lastX;
    private ViewGroup.MarginLayoutParams layoutParams;
    private int left;
    private int top;

    public TempView(Context context) {
        this(context,null);
       this.context = context;
    }


    public TempView(Context context, AttributeSet attrs) {
        this(context, attrs,0);

    }

    public TempView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        DisplayMetrics displayMetrics =  getContext().getResources().getDisplayMetrics();
        int mWidth = displayMetrics.widthPixels;
        int mHeight = displayMetrics.heightPixels;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        screenWidth = getMeasuredWidth();
        screenHeight = getMeasuredHeight();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x = (int) event.getRawX();
        int y = (int) event.getRawY();
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                layoutParams = (CoordinatorLayout.MarginLayoutParams) getLayoutParams();
                left = layoutParams.leftMargin + x - lastX;
                top = layoutParams.topMargin + y - lastY;
                layoutParams.leftMargin = left;
                layoutParams.topMargin = top;
                setLayoutParams(layoutParams);


                Log.i("zhang_xin","x:"+x+",left:"+ left +",top:"+ top +",layoutParams.leftMargin:"+ layoutParams.leftMargin);
                break;
            case MotionEvent.ACTION_UP:

                break;
        }
        lastX = x;
        lastY = y;
        return true;
    }
}



    

你可能感兴趣的:(CoordinatorLayout的使用如此简单)