[JAVA] How to "hollow" View on Android

Various View shapes can be realized by using canvas provided by Android API level 1. Isn't the general usage such as generating a View such as a circle or a rectangle or cutting a Bitmap? So this time, I would like to introduce how to ** hollow out the View **.

First of all, how to draw View

public class CanvasView extends View {

    private Paint paint = new Paint();

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

    public CanvasView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public CanvasView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        paint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(getRootView().getWidth() / 2, getRootView().getHeight() / 2, 50, paint);
    }
}

How to hollow out View

public class CanvasView extends View {

    private Paint backgroundPaint = new Paint();
    private Paint paint = new Paint();

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

    public CanvasView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public CanvasView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        setLayerType(View.LAYER_TYPE_HARDWARE, null);

        //Draw the whole
        backgroundPaint.setColor(ContextCompat.getColor(getContext(), android.R.color.background_dark));
        canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), backgroundPaint);

        //Hollow out a circle from the whole background
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        canvas.drawCircle(getWidth() / 2, getHeight() / 2, 250, paint);
    }
}

Finally

I created a library using the method introduced here. If you like it, I would be grateful if you could give it a star.

https://github.com/TakuSemba/Spotlight

customTarget.gif

Recommended Posts

How to "hollow" View on Android
How to use OpenCV 4 on Android and view camera live view
How to detect microphone conflicts on Android
Customize list view on Android
How to deploy on heroku
How to deploy jQuery on Rails
Android view
How to deploy Laravel on CentOS 7
[Java] How to update Java on Windows
How to install ImageMagick on Windows 10
How to use Ruby on Rails
How to deploy Bootstrap on Rails
How to run JavaFX on Docker
How to use Bio-Formats on Ubuntu 20.04
How to install MariaDB 10.4 on CentOS 8
Rails on Tiles (how to write)
How to install WildFly on Ubuntu 18.04
How to build vim on Ubuntu 20.04
[Android] How to make Dialog Fragment
How to call with One Touch (without confirmation) on Android (Java)
How to check Java installed on Mac
A memorandum on how to use Eclipse
How to redo a deployment on Heroku
How to use binding.pry for view files
How to use Apache Derby on Eclipse
[Ruby on Rails] How to use CarrierWave
Sample to display (head-up) notifications on Android
[Android] How to deal with dark themes
How to install Eclipse (Photon) on Mac
How to publish an application on Heroku
How to install production Metabase on Ubuntu
How to switch Java versions on Mac
How to install beta php8.0 on CentOS8
How to change the timezone on Ubuntu
[Ruby on Rails] How to use redirect_to
[Ruby on Rails] How to use kaminari
How to install kafkacat on Amazon Linux2
How to send push notifications on AWS
How to use ExpandableListView in Android Studio
How to deploy
Scheduled to support Reiwa on Android 11 as standard
[Ruby on Rails] How to display error messages
How to deploy a container on AWS Lambda
[Android] How to convert a character string to resourceId
How to configure ubuntu to be used on GCP
How to add / remove Ruby on Rails columns
How to make rbenv recognize OpenSSL on WSL
How to install network drivers on standalone Ubuntu
[Android] How to detect volume change (= volume button press)
Browse an instance of Tab's View on Android
How to save images on Heroku to S3 on AWS
How to install NVIDIA driver on Ubuntu 18.04 (Note)
Notes on how to use each JUnit Rule
How to make Unity Native Plugin (Android version)
Note how to rollback Mysql deployed on Heroku
How to install multiple JDKs on Ubuntu 18.04 LTS
[Rails MySQL] How to reset DB on heroku
[Java] 4 steps to implement splash screen on Android
How to write React Native bridge ~ Android version ~
[Ruby on Rails] How to install Bootstrap in Rails
How to build a Pytorch environment on Ubuntu