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
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<activity android:name=".LowLevelActivity" />
<activity android:name=".SimpleStateMachineActivity" />
<activity android:name=".HttpActivity"></activity>
<activity android:name=".ButtonActivity" />
</application>

</manifest>
37 changes: 37 additions & 0 deletions app/src/main/java/app/rive/runtime/example/ButtonActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package app.rive.runtime.example

import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import app.rive.runtime.example.utils.RiveButton
import app.rive.runtime.example.utils.RiveSwitch


class ButtonActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.button)

var button = findViewById<RiveButton>(R.id.myButton)
button.setOnClickListener {
var textView = findViewById<TextView>(R.id.myButtonCounter)
textView.text = (textView.text.toString().toInt()+1).toString()
}

var switch = findViewById<RiveSwitch>(R.id.mySwitch)
switch.setOnCheckedChangeListener { _, checked ->

var textView = findViewById<TextView>(R.id.mySwitchLabel)
textView.text = checked.toString()
}


var stateSwitch = findViewById<RiveSwitch>(R.id.myStateSwitch)
stateSwitch.setOnCheckedChangeListener { _, checked ->

var textView = findViewById<TextView>(R.id.myStateSwitchLabel)
textView.text = checked.toString()
}

}
}
1 change: 0 additions & 1 deletion app/src/main/java/app/rive/runtime/example/HttpActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import app.rive.runtime.kotlin.RiveAnimationView
import app.rive.runtime.kotlin.core.Fit
import app.rive.runtime.kotlin.core.Rive
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/java/app/rive/runtime/example/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,11 @@ class MainActivity : AppCompatActivity() {
Intent(this, SimpleStateMachineActivity::class.java)
)
}

findViewById<Button>(R.id.go_button).setOnClickListener {
startActivity(
Intent(this, ButtonActivity::class.java)
)
}
}
}
49 changes: 49 additions & 0 deletions app/src/main/java/app/rive/runtime/example/utils/RiveButton.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package app.rive.runtime.example.utils

import android.content.Context
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatImageButton
import app.rive.runtime.kotlin.R
import app.rive.runtime.kotlin.RiveDrawable
import app.rive.runtime.kotlin.core.File

class RiveButton(context: Context, attrs: AttributeSet? = null) : AppCompatImageButton(context, attrs) {
var riveDrawable:RiveDrawable;
var pressAnimation: String?;
init {
context.theme.obtainStyledAttributes(
attrs,
app.rive.runtime.example.R.styleable.RiveButton,
0, 0
).apply {
try {
val resourceId = getResourceId(app.rive.runtime.example.R.styleable.RiveButton_riveResource, -1)
pressAnimation = getString(app.rive.runtime.example.R.styleable.RiveButton_rivePressAnimation)

var resourceBytes = resources.openRawResource(resourceId).readBytes()
var riveFile = File(resourceBytes)
riveDrawable = RiveDrawable(autoplay = false)
riveDrawable.setRiveFile(riveFile)
background = riveDrawable

} finally {
recycle()
}
}

}

override fun performClick(): Boolean {
pressAnimation?.let{
riveDrawable.stop()
riveDrawable.play(it)
return true
} ?:run {
riveDrawable.stop()
riveDrawable.play()
}
return super.performClick()
}


}
117 changes: 117 additions & 0 deletions app/src/main/java/app/rive/runtime/example/utils/RiveSwitch.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package app.rive.runtime.example.utils

import android.content.Context
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatToggleButton
import app.rive.runtime.kotlin.RiveDrawable
import app.rive.runtime.kotlin.core.File

class RiveSwitch(context: Context, attrs: AttributeSet? = null) :

AppCompatToggleButton(context, attrs) {

var riveDrawable: RiveDrawable;
var onAnimation: String;
var offAnimation: String;
var stateMachineName: String?;
var booleanStateInput: String;

private fun defaultedString(value: String?, default: String): String {
value?.let {
return it
}
return default
}


init {
context.theme.obtainStyledAttributes(
attrs,
app.rive.runtime.example.R.styleable.RiveSwitch,
0, 0
).apply {
try {
val resourceId = getResourceId(
app.rive.runtime.example.R.styleable.RiveSwitch_riveResource,
-1
)
stateMachineName = getString(app.rive.runtime.example.R.styleable.RiveSwitch_riveStateMachine);

onAnimation = defaultedString(
getString(app.rive.runtime.example.R.styleable.RiveSwitch_riveOnAnimation),
"on"
)
offAnimation = defaultedString(
getString(app.rive.runtime.example.R.styleable.RiveSwitch_riveOffAnimation),
"off"
)

booleanStateInput = defaultedString(
getString(app.rive.runtime.example.R.styleable.RiveSwitch_riveBooleanStateInput),
"toggle"
)

var resourceBytes = resources.openRawResource(resourceId).readBytes()
var riveFile = File(resourceBytes)
riveDrawable = RiveDrawable(autoplay = false)
riveDrawable.setRiveFile(riveFile)
stateMachineName?.let{
riveDrawable.play(it, isStateMachine = true)
riveDrawable.setBooleanState(it, booleanStateInput, isChecked)
}
background = riveDrawable
} finally {
recycle()
}
}
}

private fun setCheckedAnimation(checked: Boolean){
riveDrawable?.let{
it.stop()
if (checked) {
it.play(onAnimation)
} else {
it.play(offAnimation)
}
}
}

private fun setStateMachine(checked: Boolean){
riveDrawable?.let{ drawable ->
stateMachineName?.let { stateMachine ->
drawable.setBooleanState(stateMachine, booleanStateInput, checked)
}
}
}

override fun setChecked(checked: Boolean) {
var output = super.setChecked(checked)

if (stateMachineName == null){
setCheckedAnimation(checked)
}
else {
setStateMachine(checked)
}

return output
}


override fun getTextOn(): CharSequence {
super.getTextOn()?.let{
return it
}
return ""
}
override fun getTextOff(): CharSequence {
super.getTextOn()?.let{
return it
}
return ""
}



}
103 changes: 103 additions & 0 deletions app/src/main/res/layout/button.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Simple button." />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Clicks Counter: " />

<TextView
android:id="@+id/myButtonCounter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0" />
</LinearLayout>

<app.rive.runtime.example.utils.RiveButton
android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fill"
app:riveResource="@raw/button" />

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Simple Switch." />

<TextView
android:id="@+id/mySwitchLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="false" />
</LinearLayout>

<app.rive.runtime.example.utils.RiveSwitch
android:id="@+id/mySwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fill"
android:textOn=""
android:textOff=""
app:riveOffAnimation="Off"
app:riveOnAnimation="On"
app:riveResource="@raw/switch_animation" />

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Simple Switch (state machine)." />

<TextView
android:id="@+id/myStateSwitchLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="true" />
</LinearLayout>

<app.rive.runtime.example.utils.RiveSwitch
android:id="@+id/myStateSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn=""
android:textOff=""
android:checked="true"
app:riveBooleanStateInput="Toggle"
app:riveResource="@raw/switch_animation"
app:riveStateMachine="Remember Start SM"
/>
</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
6 changes: 6 additions & 0 deletions app/src/main/res/layout/example_selection.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@
android:layout_height="wrap_content"
android:text="State Machine" />

<Button
android:id="@+id/go_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Buttons / Switches" />

</LinearLayout>

</ScrollView>
Expand Down
Binary file added app/src/main/res/raw/button.riv
Binary file not shown.
Binary file added app/src/main/res/raw/switch_animation.riv
Binary file not shown.
23 changes: 23 additions & 0 deletions app/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="riveResource" format="reference"/>
<attr name="riveArtboard" format="string"/>
<attr name="riveAnimation" format="string"/>
<attr name="riveStateMachine" format="string"/>
<declare-styleable name="RiveButton">
<attr name="riveResource" />
<attr name="riveArtboard"/>
<attr name="riveAnimation"/>
<attr name="riveStateMachine"/>
<attr name="rivePressAnimation" format="string"/>
</declare-styleable>
<declare-styleable name="RiveSwitch">
<attr name="riveResource" />
<attr name="riveArtboard"/>
<attr name="riveAnimation"/>
<attr name="riveStateMachine"/>
<attr name="riveOnAnimation" format="string"/>
<attr name="riveOffAnimation" format="string"/>
<attr name="riveBooleanStateInput" format="string"/>
</declare-styleable>
</resources>