Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 28 additions & 28 deletions app/src/main/java/app/rive/runtime/example/AndroidPlayerActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,34 +46,6 @@ class AndroidPlayerActivity : AppCompatActivity() {
fun loadResource(index: Int) {
animationView.artboardName
animationView.setRiveResource(animationResources[index], artboardName = null)
val that = this
val events = findViewById<LinearLayout>(R.id.events)
val listener = object : Listener {
override fun notifyPlay(animation: LinearAnimationInstance) {
val text = TextView(that)
text.setText("Play ${animation.animation.name}")
events.addView(text, 0)
}

override fun notifyPause(animation: LinearAnimationInstance) {
val text = TextView(that)
text.setText("Pause ${animation.animation.name}")
events.addView(text, 0)
}

override fun notifyStop(animation: LinearAnimationInstance) {
val text = TextView(that)
text.setText("Stop ${animation.animation.name}")
events.addView(text, 0)
}

override fun notifyLoop(animation: LinearAnimationInstance) {
val text = TextView(that)
text.setText("Loop ${animation.animation.name}")
events.addView(text, 0)
}
}
animationView.registerListener(listener)
setSpinner()
animationView.drawable.file?.firstArtboard?.name?.let {
loadArtboard(it)
Expand Down Expand Up @@ -218,7 +190,35 @@ class AndroidPlayerActivity : AppCompatActivity() {
setContentView(R.layout.android_player)
setResourceSpinner()
loadResource(0)
val that = this
val events = findViewById<LinearLayout>(R.id.events)
val listener = object : Listener {
override fun notifyPlay(animation: LinearAnimationInstance) {
val text = TextView(that)
text.setText("Play ${animation.animation.name}")
events.addView(text, 0)
}

override fun notifyPause(animation: LinearAnimationInstance) {
val text = TextView(that)
text.setText("Pause ${animation.animation.name}")
events.addView(text, 0)
}

override fun notifyStop(animation: LinearAnimationInstance) {
val text = TextView(that)
text.setText("Stop ${animation.animation.name}")
events.addView(text, 0)
}

override fun notifyLoop(animation: LinearAnimationInstance) {
val text = TextView(that)
text.setText("Loop ${animation.animation.name}")
events.addView(text, 0)
}
}

animationView.registerListener(listener)

}

Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/app/rive/runtime/example/HttpActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class HttpActivity : AppCompatActivity() {
Rive.init()
setContentView(R.layout.activity_http)

// Hides the app/action bar
supportActionBar?.hide();

// Load the Rive data asynchronously
httpViewModel.byteLiveData.observe(
this,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class LowLevelActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_low_level)

// Hides the app/action bar
supportActionBar?.hide();

// Load the Rive from a raw resource
val file = File(resources.openRawResource(R.raw.basketball).readBytes())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ class RiveViewTest {
}
}

@Test(expected = RiveException::class)
@Test
fun viewSetResourceLoadArtboardArtboardGotcha() {
UiThreadStatement.runOnUiThread {
val appContext = initTests()
Expand Down
78 changes: 34 additions & 44 deletions kotlin/src/main/java/app/rive/runtime/kotlin/RiveAnimationView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,10 @@ import java.util.*
*/
class RiveAnimationView(context: Context, attrs: AttributeSet? = null) : View(context, attrs),
Observable<RiveDrawable.Listener> {
// TODO: stop creating an empty drawable to begin with.
private var _drawable: RiveDrawable = RiveDrawable();
private var resourceId: Int? = null;

var drawable: RiveDrawable
get() = _drawable
private set(value) {
_drawable = value
}
// There's always just one drawable associated with an animation view
val drawable = RiveDrawable()

private var resourceId: Int? = null

var fit: Fit
get() = drawable.fit
Expand Down Expand Up @@ -117,12 +112,12 @@ class RiveAnimationView(context: Context, attrs: AttributeSet? = null) : View(co
animationName = animationName,
)
} else {
drawable.alignment = Alignment.values()[alignmentIndex];
drawable.fit = Fit.values()[fitIndex];
drawable.loop = Loop.values()[loopIndex];
drawable.autoplay = autoplay;
drawable.artboardName = artboardName;
drawable.animationName = animationName;
drawable.alignment = Alignment.values()[alignmentIndex]
drawable.fit = Fit.values()[fitIndex]
drawable.loop = Loop.values()[loopIndex]
drawable.autoplay = autoplay
drawable.artboardName = artboardName
drawable.animationName = animationName
}

} finally {
Expand Down Expand Up @@ -295,12 +290,12 @@ class RiveAnimationView(context: Context, attrs: AttributeSet? = null) : View(co
*/
fun setRiveResource(
@RawRes resId: Int,
artboardName: String? = drawable.artboardName,
animationName: String? = drawable.animationName,
artboardName: String? = null,
animationName: String? = null,
autoplay: Boolean = drawable.autoplay,
fit: Fit = drawable.fit,
alignment: Alignment = drawable.alignment,
loop: Loop = drawable.loop,
fit: Fit = Fit.CONTAIN,
alignment: Alignment = Alignment.CENTER,
loop: Loop = Loop.NONE,
) {
resourceId = resId
val bytes = resources.openRawResource(resId).readBytes()
Expand Down Expand Up @@ -329,12 +324,12 @@ class RiveAnimationView(context: Context, attrs: AttributeSet? = null) : View(co
*/
fun setRiveBytes(
bytes: ByteArray,
artboardName: String? = drawable.artboardName,
animationName: String? = drawable.animationName,
artboardName: String? = null,
animationName: String? = null,
autoplay: Boolean = drawable.autoplay,
fit: Fit = drawable.fit,
alignment: Alignment = drawable.alignment,
loop: Loop = drawable.loop,
fit: Fit = Fit.CONTAIN,
alignment: Alignment = Alignment.CENTER,
loop: Loop = Loop.NONE,
) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure the way these initialize make sense; if we're setting the Rive file multiple times, it'll reuse the first one's artboard and animation names if they're not specified, which will probably cause new files to explode. Better to have those reset to null so defaults can be chosen?

Same wit the other parameters; best to have everything reset to defaults?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah i think so

val file = File(bytes)
setRiveFile(
Expand All @@ -360,27 +355,22 @@ class RiveAnimationView(context: Context, attrs: AttributeSet? = null) : View(co
*
* @throws [RiveException] if [artboardName] or [animationName] are set and do not exist in the file.
*/
fun setRiveFile(
private fun setRiveFile(
file: File,
artboardName: String? = drawable.artboardName,
animationName: String? = drawable.animationName,
autoplay: Boolean = drawable.autoplay,
fit: Fit = drawable.fit,
alignment: Alignment = drawable.alignment,
loop: Loop = drawable.loop,
artboardName: String?,
animationName: String?,
autoplay: Boolean,
fit: Fit,
alignment: Alignment,
loop: Loop,
) {
drawable.destroy()

// TODO: we maybe not be cleaning something up here,
// as we shouldnt have create a new drawable
drawable = RiveDrawable(
fit = fit,
alignment = alignment,
loop = loop,
autoplay = autoplay,
animationName = animationName,
artboardName = artboardName,
)
drawable.clear()
drawable.fit = fit
drawable.alignment = alignment
drawable.loop = loop
drawable.autoplay = autoplay
drawable.animationName = animationName
drawable.artboardName = artboardName

drawable.setRiveFile(file)
background = drawable
Expand Down
7 changes: 6 additions & 1 deletion kotlin/src/main/java/app/rive/runtime/kotlin/RiveDrawable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,16 @@ class RiveDrawable(
return output;
}

fun reset() {
fun clear(){
animator.cancel()
animator.currentPlayTime = 0
playingAnimations.clear()
animations.clear()

}

fun reset() {
clear()
file?.let {
setRiveFile(it)
}
Expand Down