【问题标题】:Placing confetti to fall from top instead of top left only将五彩纸屑从顶部而不是仅从左上角落下
【发布时间】:2020-05-27 23:11:24
【问题描述】:

我最近遇到了这个库。

https://github.com/DanielMartinus/Konfetti

我尝试在我的应用程序中实现它。我阅读了文档并尝试实现它。以下是我的代码:

JAVA 文件:

final KonfettiView konfettiView = findViewById(R.id.viewConfetti);
konfettiView.build()
                .addColors(Color.YELLOW, Color.GREEN, Color.MAGENTA)
                .setDirection(0.0, 359.0)
                .setSpeed(1f, 5f)
                .setFadeOutEnabled(true)
                .setTimeToLive(2000L)
                .addShapes(Shape.Square.INSTANCE, Shape.Circle.INSTANCE)
                .addSizes(new Size(12, 5))
                .setPosition(-50f, konfettiView.getWidth() + 50f, -50f, -50f)
                .streamFor(300, 1500L);

下面是我的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/folk"
    tools:context=".TrueActivity">

<nl.dionsegijn.konfetti.KonfettiView
        android:id="@+id/viewConfetti"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <TextView
        android:id="@+id/CongoText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/congratulations"
        android:visibility="invisible"
        android:textSize="35dp"
        android:textColor= "#FFF"
        android:gravity="center"
        android:layout_centerHorizontal="true"
        />
</RelativeLayout>

对我来说一切都很好。但五彩纸屑仅从左上角弹出。我试图将KonfettiView 对象更改为不同的尺寸(即尝试更改X-axis);但无济于事。我什至阅读了下面关于堆栈溢出的答案。

https://stackoverflow.com/a/46016959/13627957

但这似乎没有帮助。 有什么方法可以让它从主库readme file 中显示的完全顶部弹出,而不是只在左上角弹出。

谢谢。

【问题讨论】:

    标签: java android


    【解决方案1】:

    您可以使用DisplayMetrics 类的常量widthPixels 而不是konfettiView.getWidth() 方法来实现您的结果。

    说到这两者;您的 konfettiView.getWidth() 使用 Konfetti 宽度,而 DisplayMetrics.widthPixels 使用设备宽度。关于width pixels.的信息

    试试下面的代码:

    DisplayMetrics display = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(display);
    final KonfettiView konfettiView = findViewById(R.id.viewConfetti);
    konfettiView.build()
                    .addColors(Color.YELLOW, Color.GREEN, Color.MAGENTA)
                    .setDirection(0.0, 359.0)
                    .setSpeed(1f, 5f)
                    .setFadeOutEnabled(true)
                    .setTimeToLive(2000L)
                    .addShapes(Shape.Square.INSTANCE, Shape.Circle.INSTANCE)
                    .addSizes(new Size(12, 5))
                    .setPosition(-50f, display.widthPixels + 50f, -50f, -50f)
                    .streamFor(300, 1500L);
    
    

    【讨论】:

      猜你喜欢
      • 2016-06-05
      • 2018-02-11
      • 2016-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-22
      • 2015-09-12
      • 1970-01-01
      相关资源
      最近更新 更多