自定义View简单效果

自定义View简单效果

在这里插入图片描述

在这里插入图片描述

实现效果如上

代码如下

package com.example.myapplication5;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Region;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;

public class AttrsView extends View {

    int integer;
    int integer1;


    Paint paint;
    Paint paint2;
    Paint paint3;
    Paint paint4;

    Path path;
    Path path2;
    Path path3;
    Path path4;

    boolean p1=false;
    boolean p2=false;
    boolean p3=false;
    boolean p4=false;


    Context context;


    int width;
    int height;

    public AttrsView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context=context;
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.AttrsView);
        integer = typedArray.getInteger(R.styleable.AttrsView_myHeight, 0);
        integer1 = typedArray.getInteger(R.styleable.AttrsView_myWidth, 0);

        initPaint();
    }

    private void initPaint() {

        WindowManager manager= (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics metrics=new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(metrics);

        width=metrics.widthPixels;
        height=metrics.heightPixels;


        paint=new Paint();
        paint2=new Paint();
        paint3=new Paint();
        paint4=new Paint();

        path=new Path();
        path2=new Path();
        path3=new Path();
        path4=new Path();


        path.moveTo(0,0);
        path.lineTo(width/2,height/2);
        path.lineTo(width,0);

        path2.moveTo(0,0);
        path2.lineTo(width/2,height/2);
        path2.lineTo(0,height);


        path3.moveTo(width,0);
        path3.lineTo(width/2,height/2);
        path3.lineTo(width,height);


        path4.moveTo(0,height);
        path4.lineTo(width/2,height/2);
        path4.lineTo(width,height);


        paint.setAntiAlias(true);
        paint.setColor(Color.GREEN);
        paint.setStrokeWidth(5);
        paint.setStyle(Paint.Style.STROKE);



        paint2.setAntiAlias(true);
        paint2.setColor(Color.YELLOW);
        paint2.setStrokeWidth(5);
        paint2.setStyle(Paint.Style.STROKE);


        paint3.setAntiAlias(true);
        paint3.setColor(Color.RED);
        paint3.setStrokeWidth(5);
        paint3.setStyle(Paint.Style.STROKE);


        paint4.setAntiAlias(true);
        paint4.setColor(Color.BLUE);
        paint4.setStrokeWidth(5);
        paint4.setStyle(Paint.Style.STROKE);
    }


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


//        canvas.drawCircle(integer,integer1,200,paint);
        canvas.drawPath(path,paint);
        canvas.drawPath(path2,paint2);
        canvas.drawPath(path3,paint3);
        canvas.drawPath(path4,paint4);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();

        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:

                p1= isLight(x,y,path,paint,p1);
                p2=isLight(x,y,path2,paint2,p2);
                p3=isLight(x,y,path3,paint3,p3);
                p4=isLight(x,y,path4,paint4,p4);



                break;

        }
        invalidate();

        return true;


    }


    public boolean
    isLight(float x,float y,Path path,Paint paint,boolean b){
        RectF rectF=new RectF();
        path.computeBounds(rectF,true);
        Region region = new Region();
        region.setPath(path,new Region((int)rectF.left,(int)rectF.top,(int)rectF.right,(int)rectF.bottom));
        boolean contains = region.contains((int)x, (int)y);
        if(contains){
            if(b){

                paint.setStyle(Paint.Style.STROKE);
                return false;
            }else {
                paint.setStyle(Paint.Style.FILL);
                return true;
            }
        }
        return b;
    }

}


再通过布局文件标签使用即可

版权声明:本文为BugGodFather原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/BugGodFather/article/details/95384035