自定义View探究



//自定义一个类


public class MyView extends View {


    private Paint paint;
    private int mWidth;
    private int mHeight;


    private int RX;
    private int RY;
    private int one;
    private int two;
    private int three;
    private int rect;




    public MyView(Context context) {
        super(context);
    }


    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);


        paint = new Paint();


    }


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


    @Override


    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
//获取控件的宽度
        mWidth = w;
//获取控件的高度


 mHeight = h; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas);

        paint = new Paint();
//距离控件有一定的距离
        rect = mWidth * 3 / 4;
        int R = rect * 8 / 25;
        int width = R * 3 / 8;


        //小圆的半径
        int r = rect / 2 - width * 2;
//圆点坐标
        RX = mWidth / 2;
        RY = mHeight / 2;

        one = r;


        two = r + width;
        three = r + width * 2;




        int left = mWidth / 2 - rect / 2;
        int right = left + rect;
        int top = mHeight / 2 - rect / 2;
        int bottom = mHeight - top;
        paint.setStrokeWidth(width);
        paint.setAntiAlias(true);
        paint.setColor(Color.GREEN);
//矩形


        canvas.drawRect(left, top, right, bottom, paint);
//圆
        paint.setColor(Color.WHITE);
        canvas.drawCircle(RX, RY, r, paint);


        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.STROKE);


        canvas.drawCircle(RX, RY, R, paint);


        paint.setColor(Color.BLUE);


        canvas.drawCircle(RX, RY, R + width, paint);
    }




    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();
        int action = event.getAction();
        if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_MOVE) {
            if (x + getLeft() < getRight() && y + getTop() < getBottom()) {
                int type = 0;


                int distance = getDistance(x, y);
                if (distance <= one * one) {
                    type = 0;
                } else if (distance > one && distance <= two * two) {
                    type = 1;
                } else if (distance > two && distance <= three * three) {
                    type = 2;
                } else if (x >= RX - rect / 2 && x <= RX + rect / 2 && y >= RY - rect / 2 && y <= RY + rect / 2) {
                    type = 3;
                } else {
                    type = 4;
                }


                if (onWhatListener != null) {
                    onWhatListener.onWhat(type);
                }
            }
        }


        return true;




    }


    private int getDistance(int x, int y) {
        return (x - RX) * (x - RX) + (y - RY) * (y - RY);
    }


    public onWhatListener onWhatListener;


    public interface onWhatListener {
        void onWhat(int type);
    }


    public void setOnWhatListener(MyView.onWhatListener onWhatListener) {
        this.onWhatListener = onWhatListener;
    }


}
主Activity


public class MainActivity extends AppCompatActivity {


    private TextView my_tv;


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


    private void initView() {
        MyView myView = (MyView) findViewById(R.id.myview);
        my_tv = (TextView) findViewById(R.id.my_tv);
        myView.setOnWhatListener(new MyView.onWhatListener() {
            @Override
            public void onWhat(int type) {
                switch (type) {
                    case 0:
                        my_tv.setText("白色");
                        break;
                    case 1:


                        my_tv.setText("红色");
                        break;
                    case 2:
                        my_tv.setText("蓝色");
                        break;
                    case 3:
                        my_tv.setText("绿色");
                        break;
                    case 4:
                        my_tv.setText("外太空");
                        break;


                }
            }
        });
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值