自定义view继承view

Myview类继承view

public class Myview extends View {
    Paint paint;//画笔
    Region region;//圆形区域
    Path path;//圆形路径
    int x;
    int y;
    Path fpath;//矩形路径
    Region fregson;//矩形区域
    Path ypath;//圆形路径
    Region yregion;//圆形区域
    onmyclick listener;

    public void Myview(onmyclick listener) {
        this.listener = listener;
    }

    /**
     * 一个参数
     *
     * @param
     */
    public Myview(Context context) {
        super(context);
        init();
    }
    private void init() {
        paint = new Paint();//初始化画笔

        region = new Region();//圆形的区域
        path = new Path();//圆形的路径

        fpath = new Path();//矩形路径
        fregson = new Region();//矩形区域

        ypath = new Path();//小圆形的路径
        yregion = new Region();//小圆形的区域
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            //获取点击坐标
            int x = (int) event.getX();
            int y = (int) event.getY();
            //判断点击区域
            //小园内
            if (yregion.contains(x, y)) {
                if (listener != null) {
                    listener.onint(this);
                }
                //大圆外
            } else if (fregson.contains(x, y)) {
                if (listener != null) {
                    listener.onout(this);
                }
                //小圆外
            } else if (region.contains(x, y)) {
                if (listener != null) {
                    listener.xy(this);
                }
                //空白
            } else {
                if (listener != null) {
                    listener.onkb(this);
                }
            }
        }

        return super.onTouchEvent(event);

    }

    //监听事件
    public interface onmyclick {
        //小圆内
        void onint(View v);

        //大圆外
        void onout(View v);

        //小圆外
        void xy(View v);

        //空白
        void onkb(View v);


    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        //绘制圆的路径
        x = w / 2;//圆心的横坐标
        y = h / 2;//圆心的纵坐标
        int r = 200;//圆的半径
        path.addCircle(x, y, r, Path.Direction.CW);
        fpath.addRect(x - r, y - r, x + r, y + r, Path.Direction.CW);


        int rr = 50;
        ypath.addCircle(x, y, rr, Path.Direction.CW);
        Region region1 = new Region(0, 0, w, h);
        region.setPath(path, region1);//圆形区域
        fregson.setPath(fpath, region1);//矩形区域
        yregion.setPath(ypath, region1);//小圆区域
    }

    /**
     * 绘制
     *
     * @param canvas
     */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Path ppath = fpath;
        paint.setColor(Color.RED);
        canvas.drawPath(ppath, paint);

        Path tpath = path; //圆的路径
        paint.setColor(Color.BLACK);//黑色画笔
        canvas.drawPath(tpath, paint);


        Path xpath = ypath;
        paint.setColor(Color.GREEN);
        canvas.drawPath(xpath, paint);
        //绘制文字
        paint.setColor(Color.YELLOW);
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setTextSize(20);//字体大小
        canvas.drawText("圆心", x, y, paint);
    }

    /**
     * 布局
     *
     * @param changed
     * @param left
     * @param top
     * @param right
     * @param bottom
     */
    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    /**
     * 测量
     *
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    /**
     * 两个参数
     *
     * @param context
     * @param attrs
     */
    public Myview(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }


}

在activity_main布局里设置自己的自定义view

 <com.example.myview.Myview
         android:id="@+id/myview"
         android:layout_width="match_parent"
         android:layout_height="match_parent" />

这个类里获取id和监听

public class MainActivity extends AppCompatActivity implements Myview.onmyclick{
Myview myView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myView=(Myview)findViewById(R.id.myview);
        myView.Myview(this);
    }

    @Override
    public void onint(View v) {
        Toast.makeText(this,"小园内",Toast.LENGTH_LONG).show();
    }

    @Override
    public void onout(View v) {
        Toast.makeText(this,"大园外",Toast.LENGTH_LONG).show();
    }

    @Override
    public void xy(View v) {
        Toast.makeText(this,"小园外",Toast.LENGTH_LONG).show();
    }

    @Override
    public void onkb(View v) {
        Toast.makeText(this,"空白",Toast.LENGTH_LONG).show();
    }
}

效果图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值