编程人 cdmana.com

Android business display conference flat panel spotlight effect implementation

Effect picture of spotlight
 

Code
DrawView.java
 

package com.rp.rpspotlight;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

public class DrawView extends View {
    // Initialize the position of the circle 
    public float currentX=500;
    public float currentY=500;
    public DrawView(Context context) {
        this(context,null);
    }
 
    public DrawView(Context context, @Nullable AttributeSet attrs) {
        this(context,null,0);
    }
 
    public DrawView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
 
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // Create a brush 
        canvas.drawColor(Color.BLACK); // Canvas Color 
        Paint paint=new Paint();
        // Fill color 
        setWillNotDraw(false);
        setLayerType(LAYER_TYPE_HARDWARE, null);
        paint.setAntiAlias(true);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); // Make the circle transparent 
        //paint.setColor(Color.BLACK);
        // Start drawing circles ,float cx Start x The location of , float cy Start Y The location of , float radius The radius of the circle , @NonNull Paint paint paint brush 
        canvas.drawCircle(currentX,currentY,80,paint);
    }
 
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //getX Get clicks x Location ,getY Get clicks Y The location of 
        currentX= event.getX();
        currentY=event.getY();
        // Redraw yourself 
        invalidate();
        // return true Self consumption 
        return true;
    }
}

quote
 

  <com.rp.rpspotlight.DrawView
      android:layout_width="match_parent"
      android:layout_height="match_parent"/>

take Activity The background is set to transparent

    <style name="TransparentTheme" parent="Theme.AppCompat.NoActionBar">
        <!-- Not set up activity Enter and exit animation styles -->
        <item name="android:windowAnimationStyle">@null</item>
        <!-- Set the background of the window to transparent , To set transparent background, you must set this -->
        <item name="android:windowBackground">@android:color/transparent</item>
        <!-- Set whether the background of the window is translucent , To set transparent background, you must set this -->
        <item name="android:windowIsTranslucent">true</item>
        <!-- Set the background of the status bar to translucent -->
        <item name="android:windowTranslucentStatus">true</item>
    </style>

The above is enough .

Scroll to Top