自定义view随手指移动的小球

——————————————————————————————————————————————————————————————————————————————

自定义view

package www.bawei.com.daythree;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by dell on 2017/12/28.
 */

public class Defind_User extends View {

    private Paint mpaint;
    private int x = 0;
    private int y = 0;
    private Object obj;

    public Defind_User(Context context) {

        this(context, null);

    }

    public Defind_User(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public Defind_User(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mpaint = new Paint();
        mpaint.setColor(Color.RED);
        mpaint.setAntiAlias(true);
        mpaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mpaint.setStrokeWidth(1);

    }

    int width;
    int height;


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthmode = MeasureSpec.getMode(widthMeasureSpec);
        int hightmode = MeasureSpec.getMode(heightMeasureSpec);
        widthmode = MeasureSpec.EXACTLY;
        hightmode = MeasureSpec.EXACTLY;
        width = MeasureSpec.getSize(widthMeasureSpec);
        height = MeasureSpec.getSize(heightMeasureSpec);
//作者:纪昌杰
//链接:https://www.zhihu.com/question/46385405/answer/101468827
//来源:知乎
//著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


        final int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        final int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
        final int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
        final int heightSpecSize = MeasureSpec.getMode(heightMeasureSpec);
        if (widthSpecMode == MeasureSpec.AT_MOST && heightSpecMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(width, height);
        } else if (widthSpecMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(width, heightSpecSize);
        } else if (heightSpecMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(widthSpecSize, height);
        }


        setMeasuredDimension(width, height);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawCircle(x, y, 10, mpaint);


    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {


        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                x = (int) event.getRawX();
                y = (int) event.getRawY();

                break;
            case MotionEvent.ACTION_MOVE:
                x = (int) event.getRawX();
                y = (int) event.getRawY();

                int left = getLeft();
                int right = getRight();
                int top = getTop();
                int bottom = getBottom();
//                left=left+x;
//                right=right+x;
//                top=top+y;
//                top=top+y;
//                layout(left,top,right,bottom);
                break;
            case MotionEvent.ACTION_UP:
                width = 0;
                height = 0;
                x = 0;
                y = 0;
                break;
        }
//        width=x;
//        hight=y;
        invalidate();
        return true;

    }
}
——————————————————————————————————————————————————

取消标题栏

 View decorView = getWindow().getDecorView();
        int option = View.SYSTEM_UI_FLAG_FULLSCREEN;
        decorView.setSystemUiVisibility(option);
        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();

——————————————————————————————————————————————————

我们在Activity布局直接调用这个就好了

猜你喜欢

转载自blog.csdn.net/sj_lpl_sb/article/details/78934696