Android 控件背景实现发光效果

主要实现的那种光晕效果:中间亮,四周逐渐变淡的。

这边有三种发光效果,先上效果图。

第一种、圆形发光体

实现代码:新建shape_light.xml,导入以下代码。使用时,直接给view设置为background。

XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <gradient
        android:centerColor="@color/transparent"
        android:centerX="0.5"
        android:centerY="0.5"
        android:gradientRadius="180dp"
        android:startColor="@color/yellow"
        android:type="radial" />
</shape>

第二种、矩形发光体

代码实现:通过自定义view实现。

Kotlin 复制代码
package com.fht.testproject

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View

/**
 * @author fenghaitao
 * @time 2023/11/1 16:40
 */
class RectLightView @JvmOverloads constructor(
    context: Context, attributeSet: AttributeSet, defStyleAttr: Int = 0
) : View(context, attributeSet, defStyleAttr) {
    private val paint: Paint = Paint()
    private val corner = 50f
    private val count = 200

    init {
        paint.isAntiAlias = false
        paint.style = Paint.Style.FILL
        paint.color = Color.YELLOW
    }

    @SuppressLint("DrawAllocation")
    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        val w = width / count
        val h = height / count
        for (i in 0..count) {
            paint.alpha = (255 / count) * i
            if (((width - 2 * (w * i)) > 0) && ((height - 2 * (h * i)) > 0)) {
                val rectF = RectF().apply {
                    left = (w * i).toFloat()
                    top = (h * i).toFloat()
                    right = (width - w * i).toFloat()
                    bottom = (height - h * i).toFloat()
                }
                canvas?.drawRoundRect(rectF, corner, corner, paint)
            }
        }
    }
}

第三种、矩形发光体,比上一种更透明

这种有点瑕疵,中间有一点空白,不过稍微修改一下代码也可以去掉,这里就不做修改了。

代码实现:通过自定义view实现。

Kotlin 复制代码
package com.fht.testproject

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View

/**
 * @author fenghaitao
 * @time 2023/11/1 16:40
 */
class LightView @JvmOverloads constructor(
    context: Context, attributeSet: AttributeSet, defStyleAttr: Int = 0
) : View(context, attributeSet, defStyleAttr) {
    private val paint: Paint = Paint()
    private val corner = 1f
    private val count = 100

    init {
        paint.isAntiAlias = false
        paint.style = Paint.Style.STROKE
        paint.color = Color.YELLOW
    }

    @SuppressLint("DrawAllocation")
    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        val w = width / count
        val h = height / count
        paint.strokeWidth = w.toFloat()
        for (i in 0..count) {
            paint.alpha = (255 / count) * i
            if (((width - 2 * (w * i)) > 0) && ((height - 2 * (w * i)) > 0)) {
                val rectF = RectF().apply {
                    left = (w * i).toFloat()
                    top = (w * i).toFloat()
                    right = (width - w * i).toFloat()
                    bottom = (height - w * i).toFloat()
                }
                canvas?.drawRect(rectF, paint)
            }
        }
    }
}
相关推荐
努力学习的小廉2 小时前
深入了解linux系统—— 操作系统的路径缓冲与链接机制
android·linux·服务器
清霜之辰2 小时前
安卓开发用到的设计模式(2)结构型模式
android·设计模式·结构型设计模式
sg_knight2 小时前
Flutter跨平台通信实战|3步打通Android原生能力,实现底层API调用!
android·前端·javascript·flutter·跨平台·web·双向通信
白嫖不白嫖2 小时前
MySQL 8.0 和 5.7 快速生成测试数据
android·数据库·mysql
每次的天空4 小时前
Android-OkHttp与Retrofit学习总结
android·okhttp·retrofit
tmacfrank6 小时前
Android 网络全栈攻略(四)—— 从 OkHttp 拦截器来看 HTTP 协议一
android·网络·okhttp
qwetyunk6 小时前
ai陪伴项目——Android app开发
android
君的名字8 小时前
怎么判断一个Android APP使用了flutter 这个跨端框架
android·flutter
淡淡的香烟8 小时前
Android12 launcher3修改App图标白边问题
android
limingade11 小时前
手机打电话时由对方DTMF响应切换多级IVR语音菜单(话术脚本与实战)
android·智能手机·语音识别·蓝牙电话·多级ivr导航·手机个人400电话·手机电话实现ivr语音导航