android发光背景,如何在android中创建发光背景?

本文介绍如何在安卓应用中创建一个自定义视图`SpotLightsView`,利用三角数学和线性渐变实现聚光灯效果。通过调整`angleDegree`变量可以改变聚光灯的数量和形状,而`doubler`变量控制弧形末端的宽度。代码详细展示了`onDraw`方法中绘制每个聚光灯的逻辑,包括路径、渐变和填充。此实现适用于任何安卓系统,适合对自定义UI感兴趣的开发者。
摘要由CSDN通过智能技术生成

在搜索了很多之后,我真的在网上找不到任何东西。所以我试着从头开始画。这并不完美,所以我以后会努力让它变得更好。你需要做一些三角数学来画一些扇区。然后使用渐变创建淡出效果。

这是我的代码:

import android.content.Context;

import android.graphics.Canvas;

import android.graphics.LinearGradient;

import android.graphics.Paint;

import android.graphics.Path;

import android.util.AttributeSet;

import android.view.View;

import androidx.annotation.Nullable;

public class SpotLightsView extends View {

private static final String TAG = SpotLightsView.class.getSimpleName();

private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

private double angleDegree = 10;

private double angleRadian = Math.toRadians(angleDegree);

private final double dTheta = angleRadian;

private double WIDTH = 100;

private double HEIGHT = 100;

private double RADIUS = (1.4142*WIDTH/2); // r = a*sqrt(2);

private double cx = WIDTH/2;

private double cy = HEIGHT/2;

public SpotLightsView(Context context) {

super(context);

}

public SpotLightsView(Context context, @Nullable AttributeSet attrs) {

super(context, attrs);

}

public SpotLightsView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

}

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

WIDTH = canvas.getWidth();

HEIGHT = canvas.getHeight();

cx = WIDTH/2;

cy = HEIGHT/2;

RADIUS = cy*1.4142; // r = a*sqrt(2);

drawSpotLights(canvas);

}

private void drawSpotLights(Canvas canvas) {

double theta = 0;

for(int i=0; i<360/angleDegree; i++){

drawSpotLightOn(canvas, theta);

theta+=dTheta;

}

}

private void drawSpotLightOn(Canvas canvas, double theta){

double x1, y1, x2, y2, x3, y3, sinTheta, cosTheta;

double r = 50;

sinTheta = Math.sin(theta);

cosTheta = Math.cos(theta);

x1 = cx + RADIUS*cosTheta;

y1 = cy + RADIUS*sinTheta;

x2 = x1 + r*sinTheta;

y2 = y1 - r*cosTheta;

x3 = x1 - r*sinTheta;

y3 = y1 + r*cosTheta;

LinearGradient linearGradient = new LinearGradient((float)cx,(float)cy,(float)x1,(float)y1, 0x88ffffff,

0x00000000, android.graphics.Shader.TileMode.CLAMP);

mPaint.setDither(true);

mPaint.setShader(linearGradient);

mPaint.setStyle(Paint.Style.FILL);

Path path = new Path();

path.moveTo((float)cx, (float)cy);

path.lineTo((float)x3, (float)y3);

path.quadTo((float)x1, (float)y1, (float)x2, (float)y2);

path.lineTo((float)cx, (float)cy);

canvas.drawPath(path, mPaint);

}

}

然后像这样在你的

some_layout.xml

文件:

app:layout_constraintTop_toTopOf="parent"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintEnd_toEndOf="parent"

android:layout_width="match_parent"

android:layout_height="match_parent"/>

输出如下:

6c072eebdeadc60f4dc853aea6e96b76.png

简短的解释

改变

private double angleDegree = 10;

来说明你想要聚光灯的度数。

然后我们在for循环中增加角度并调用

private void drawSpotLightOn(Canvas canvas, double theta)

方法。在第二行,有一个值

double r=50

. 它告诉代码弧在终点处的宽度。更改这2个并查看结果以获得所需的输出。

为了使淡出效果,我用

LinearGradient

其余的代码都是直截了当的。你可以在任何安卓系统中找到它们

Paint

,

CustomView

教程。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值