Skip to content

Instantly share code, notes, and snippets.

Avatar
💜

Rebecca Franks riggaroo

💜
View GitHub Profile
@riggaroo
riggaroo / CustomView-Snippet5.kt
Created Mar 21, 2019
Valid with function inside parentheses
View CustomView-Snippet5.kt
canvas.withTranslate(200f, 300f, {
drawCircle(...)
})
@riggaroo
riggaroo / CanvasExt.kt
Created Mar 21, 2019
Sample of Canvas extension function from Android KTX
View CanvasExt.kt
/**
* 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.kt
Last active Mar 21, 2019
Custom Canvas Drawing with KTX
View CustomView.kt
canvas.withTranslation(200f, 300f) {
drawCircle(150f, 150f, RADIUS, circlePaint)
withRotation(45f) {
drawRect(rect, rectPaint)
}
}
@riggaroo
riggaroo / CustomView-Snippet2.kt
Last active Mar 21, 2019
Custom Drawing on Canvas without KTX
View CustomView-Snippet2.kt
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-Snippet1.kt
Last active Mar 21, 2019
Drawing to Canvas without KTX
View CustomView-Snippet1.kt
canvas.save()
canvas.translate(200f, 300f)
canvas.drawCircle(...) // drawn on the translated canvas
canvas.restore()
@riggaroo
riggaroo / PULL_REQUEST_TEMPLATE
Created Feb 3, 2019
Pull request template format. Add this file to your .github folder
View PULL_REQUEST_TEMPLATE
<!--- 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 / ProjectEditorViewModel.kt
Created Feb 2, 2019
ProjectEditorViewModel using AndroidViewModel, exposing UI state
View ProjectEditorViewModel.kt
class ProjectEditorViewModel : ViewModel() {
private val _state = MutableLiveData<EditorState>()
val state: LiveData<EditorState>
get() = _state
}
@riggaroo
riggaroo / ProjectEditorFragment.kt
Last active Feb 3, 2019
ProjectEditorFragment showing how to use ViewModels to restore state.
View ProjectEditorFragment.kt
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)
View MotionSensorMainActivity.kt
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
View CustomCamera.kt
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.ImageFormat
import android.graphics.Matrix
import android.hardware.camera2.*
import android.hardware.camera2.CameraAccessException.CAMERA_ERROR
import android.media.ImageReader
import android.os.Handler
import android.util.Log