Skip to content

Instantly share code, notes, and snippets.

View riggaroo's full-sized avatar
🌍

Rebecca Franks riggaroo

🌍
View GitHub Profile
@riggaroo
riggaroo / MotionSensorMainActivity.kt
Created October 21, 2017 10:33
Motion sensing camera
package za.co.riggaroo.motionsense
import android.app.Activity
import android.graphics.Bitmap
import android.os.Bundle
import android.os.Handler
import android.util.Log
import android.widget.ImageView
import com.google.android.things.pio.Gpio
import com.google.android.things.pio.GpioCallback
@riggaroo
riggaroo / ProjectEditorFragment.kt
Last active February 3, 2019 10:15
ProjectEditorFragment showing how to use ViewModels to restore state.
class ProjectEditorFragment : Fragment {
@Inject
lateinit var viewModelFactory: ViewModelProvider.Factory
private lateinit var viewModel: ProjectEditorViewModel
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_editor_initial, container, false)
@riggaroo
riggaroo / ProjectEditorViewModel.kt
Created February 2, 2019 19:08
ProjectEditorViewModel using AndroidViewModel, exposing UI state
class ProjectEditorViewModel : ViewModel() {
private val _state = MutableLiveData<EditorState>()
val state: LiveData<EditorState>
get() = _state
}
@riggaroo
riggaroo / PULL_REQUEST_TEMPLATE
Created February 3, 2019 14:34
Pull request template format. Add this file to your .github folder
<!--- Provide a general summary of your changes in the Title above -->
<!--- If there is no changelog entry, label this PR as trivial to bypass the Danger warning -->
## Description
<!--- Describe your changes in detail -->
## Motivation and Context
<!--- Why is this change required? What problem does it solve? -->
@riggaroo
riggaroo / CustomView-Snippet1.kt
Last active March 21, 2019 15:25
Drawing to Canvas without KTX
canvas.save()
canvas.translate(200f, 300f)
canvas.drawCircle(...) // drawn on the translated canvas
canvas.restore()
@riggaroo
riggaroo / CustomView-Snippet2.kt
Last active March 21, 2019 15:24
Custom Drawing on Canvas without KTX
val translateCheckpoint = canvas.save()
canvas.translate(200f, 300f)
canvas.drawCircle(150f, 150f, RADIUS, circlePaint) // drawn on the translated canvas
val rotateCheckpoint = canvas.save()
canvas.rotate(45f)
canvas.drawRect(rect, rectPaint) // drawn on the translated and rotated canvas
canvas.restoreToCount(rotateCheckpoint)
canvas.restoreToCount(translateCheckpoint)
@riggaroo
riggaroo / CustomView.kt
Last active March 21, 2019 15:36
Custom Canvas Drawing with KTX
canvas.withTranslation(200f, 300f) {
drawCircle(150f, 150f, RADIUS, circlePaint)
withRotation(45f) {
drawRect(rect, rectPaint)
}
}
@riggaroo
riggaroo / CanvasExt.kt
Created March 21, 2019 15:26
Sample of Canvas extension function from Android KTX
/**
* Wrap the specified [block] in calls to [Canvas.save]/[Canvas.translate]
* and [Canvas.restoreToCount].
*/
inline fun Canvas.withTranslation(
x: Float = 0.0f,
y: Float = 0.0f,
block: Canvas.() -> Unit
) {
val checkpoint = save()
@riggaroo
riggaroo / CustomView-Snippet5.kt
Created March 21, 2019 15:28
Valid with function inside parentheses
canvas.withTranslate(200f, 300f, {
drawCircle(...)
})
@riggaroo
riggaroo / CustomView-Snippet6.kt
Created March 21, 2019 15:29
Using extension function to draw to canvas.
canvas.withTranslate(200f, 300f) {
drawCircle(...)
}