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)
            }
        }
    }
}
相关推荐
用户0273851840267 小时前
[Android]RecycleView的item用法
android
前行的小黑炭8 小时前
Android :为APK注入“脂肪”,论Android垃圾代码在安全加固中的作用
android·kotlin
帅得不敢出门9 小时前
Docker安装Ubuntu搭建Android SDK编译环境
android·ubuntu·docker
tangweiguo030519879 小时前
Android Kotlin 动态注册 Broadcast 的完整封装方案
android·kotlin
fatiaozhang95279 小时前
浪潮CD1000-移动云电脑-RK3528芯片-2+32G-安卓9-2种开启ADB ROOT刷机教程方法
android·网络·adb·电脑·电视盒子·刷机固件·机顶盒刷机
前行的小黑炭10 小时前
Android 不同构建模式下使用不同类的例子:如何在debug模式和release模式,让其使用不同的类呢?
android·kotlin·gradle
andyguo11 小时前
AI模型测评平台工程化实战十二讲(第一讲:从手工测试到系统化的觉醒)
android
2501_9159214311 小时前
小团队如何高效完成 uni-app iOS 上架,从分工到工具组合的实战经验
android·ios·小程序·uni-app·cocoa·iphone·webview
幂简集成11 小时前
通义灵码 AI 程序员低代码 API 课程实战教程
android·人工智能·深度学习·神经网络·低代码·rxjava
2501_9160088912 小时前
uni-app iOS 文件管理与 itools 配合实战,多工具协作的完整流程
android·ios·小程序·https·uni-app·iphone·webview