Navigate back to the homepage

Konfetti migration guide v2.x.x

Dion Segijn
December 29th, 2021 · 2 min read

If you’re here then you probably want to migrate Konfetti from version 1.x.x to 2.x.x. With the latest release there are a lot of changes both to the API, animations and improvements under the hood. Here’s a list of the major things that changed:

  • Releases with support for compose
  • Easier API with the focus on reusable configuration classes
  • Decoupled core of the library and modularized it to make it reusable for the view-based (xml) and compose implementations
  • New animations making it look and feel more natural

Migration

Let’s continue with migrating from the old to new library. Previously you’ve included Konfetti using the following lines:

Installation

1dependencies {
2 implementation 'nl.dionsegijn:konfetti:1.3.2'
3}

The path has changed and depending on your setup you can now choose between compose, xml or both using:

Compose

1dependencies {
2 implementation 'nl.dionsegijn:konfetti-compose:2.0.1'
3}

View based (XML) project:

1dependencies {
2 implementation 'nl.dionsegijn:konfetti-xml:2.0.1'
3}

Check out latest version(s) here

Configuration

We’ll now continue focussing on migrating the view-based implementation of Konfetti since support for compose is new.

KonfettiView

The path to KonfettiView has changed. This one should be updated to: <nl.dionsegijn.konfetti.xml.KonfettiView

1<nl.dionsegijn.konfetti.xml.KonfettiView
2 android:id="@+id/konfettiView"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"/>

Set configuration

Previously you were able to run the konfetti animations using a builder function on KonfettiView, like this:

1viewKonfetti.build()
2 .addColors(Color.YELLOW, Color.GREEN, Color.MAGENTA)
3 .setDirection(0.0, 359.0)
4 .setSpeed(1f, 5f)
5 .setFadeOutEnabled(true)
6 .setTimeToLive(2000L)
7 .addShapes(Shape.Square, Shape.Circle)
8 .addSizes(Size(12))
9 .setPosition(-50f, viewKonfetti.width + 50f, -50f, -50f)
10 .streamFor(300, 5000L)

The new and improved API lets you create Party objects. This is a data class so that means you’re able to copy them and alter them how you want. Here’s an example how above example would translate to the new Party configuration:

1val party = Party(
2 colors = listOf(Color.YELLOW, Color.GREEN, Color.MAGENTA),
3 angle = 0,
4 spread = 360,
5 speed = 1f,
6 maxSpeed = 10f,
7 fadeOutEnabled = true,
8 timeToLive = 2000L,
9 shapes = listOf(Shape.Square, Shape.Circle),
10 size = listOf(Size(12)),
11 position = Position.Relative(0.0, 0.0).between(Position.Relative(1.0, 0.0)),
12 emitter = Emitter(duration = 5, TimeUnit.SECONDS).perSecond(300)
13)
14 viewKonfetti.start(party)

All the configurations have moved to the party configuration object. Most of it stayed the same but there are some changes and extra options. Let’s go through them:

  • Direction: Instead of setDirection(minAngle, maxAngle) you now set the angle which is the direction in degrees and you set the spread which is how wide the konfetti will shoot from 0 to 360 where 360 is a circle (all directions).
  • Speed: Instead of setSpeed(min, max) you now set speed and maxSpeed. If you set maxSpeed to -1 it will only take the value of speed and not pick a random speed inbetween.
  • Size: stays the same but you can now add a third option (massVariance) which is also set by default to 0.2 (20%). This will randomly add 20% of its mass to each konfetti for a natural look.
  • position: You can now use relative and absolute positions. In this example I replaced the position with an example using relative positions because it’s easier to use. Though if you prefer to use absolute x and y positions you can also use Position.Absolute(x, y). Chain the position when an between to randomize the position between two points.
  • emitter: Create an Emitter and set the duration then chain it with max or perSecond to create your desired effect.

There’s a lot more you can configure, see the Party object and its documentation for more or see the GitHub page.

More articles from Dion Segijn

Konfetti - Simple 3D animation on a 2D Android canvas

Implementing a 3D rotation effect on a 2D canvas on Android might be easier than you think. This article demonstrates the three things you need in order to achieve that.

October 22nd, 2020 · 2 min read

GitHub Actions - Detect silently added permissions part-2

In the second part we continue with the extracted permissions and use diff to detect any changes. The results will be posted to the Pull Request.

April 29th, 2020 · 2 min read
© 2020–2021 Dion Segijn
Link to $https://twitter.com/DionSegijnLink to $https://github.com/DanielMartinusLink to $https://www.linkedin.com/in/dionsegijn/Link to $https://www.instagram.com/dionsegijn/