Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在android中创建发光背景?_Java_Android_User Interface_Kotlin_Light - Fatal编程技术网

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

Java 如何在android中创建发光背景?,java,android,user-interface,kotlin,light,Java,Android,User Interface,Kotlin,Light,我正在开发一款应用程序,其背景应该如下: 但我真的找不到任何教程如何实现这种设计。也许我在谷歌搜索中使用了错误的关键词,因为大多数关键词都会导致我阅读instagram背景教程 有谁能帮我一下,或者告诉我从哪里开始吗?在专用于你的应用程序布局的xml文件中,只需将你的图像设置为背景。在搜索了很多之后,我在互联网上真的找不到任何东西。所以我试着从头开始画。它不是完美的,所以我会努力在将来做得更好。你需要做一些三角数学来画一些扇形。然后使用渐变创建淡出效果。 这是我的密码: import andr

我正在开发一款应用程序,其背景应该如下:

但我真的找不到任何教程如何实现这种设计。也许我在谷歌搜索中使用了错误的关键词,因为大多数关键词都会导致我阅读instagram背景教程


有谁能帮我一下,或者告诉我从哪里开始吗?

在专用于你的应用程序布局的xml文件中,只需将你的图像设置为背景。

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

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);
    }
}

输出如下所示:

简短解释 更改
专用双角度度=10
指定要在多少度后成为聚光灯。 然后我们在for循环中增加角度,并调用
private void drawSpotLightOn(Canvas Canvas,double theta)
方法。在第二行,有一个值
双r=50
。它告诉代码圆弧在端点处的宽度。更改这2项,然后查看结果以获得所需的输出。 为了产生淡出效果,我使用了
LinearGradient


代码的其余部分是直截了当的。您可以在任何android
Paint
CustomView
教程中找到它们。

谢谢。我想知道是否有可能使用java创建这种灯光效果。我不知道。不过,您可以将其创建为svg或xml。
<com.applications.customviews.demo.SpotLightsView
            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"/>