Skip to content

Instantly share code, notes, and snippets.

View PiotrPrus's full-sized avatar
🏠
Working from home

Piotr Prus PiotrPrus

🏠
Working from home
View GitHub Profile
private fun takeScreenshot() {
val fullScreen = requireActivity().window.decorView.rootView
bitmapBackground = Bitmap.createBitmap(fullScreen.width, fullScreen.height, Bitmap.Config.ARGB_8888)
canvasFullscreen = Canvas(bitmapBackground)
fullScreen.draw(canvasFullscreen)
bitmapBackgroundDrawable = BitmapDrawable(resources, bitmapBackground)
requireActivity().window.setBackgroundDrawable(bitmapBackgroundDrawable)
}
@PiotrPrus
PiotrPrus / MotionLayoutExample.kt
Created September 25, 2019 18:33
A gist that shows how transitionListener looks like
binding.motionLayoutRoot.setTransitionListener(object : MotionLayout.TransitionListener {
override fun onTransitionTrigger(p0: MotionLayout?, startId: Int, endId: Boolean, p3: Float) {}
override fun allowsTransition(p0: MotionScene.Transition?): Boolean {}
override fun onTransitionStarted(p0: MotionLayout?, startId: Int, endId: Int) {}
override fun onTransitionChange(p0: MotionLayout?, startId: Int, endId: Int, progress: Float) {}
override fun onTransitionCompleted(p0: MotionLayout?, endId: Int) {}
@PiotrPrus
PiotrPrus / transition_swipe.xml
Created September 26, 2019 20:46
Add animation configuration for swiping view
<Transition
motion:constraintSetEnd="@id/endSwipeScene"
motion:constraintSetStart="@id/endScene"
motion:duration="600">
<OnClick
motion:clickAction="toggle"
motion:targetId="@+id/sendButton" />
</Transition>
@PiotrPrus
PiotrPrus / endSwipeScene.xml
Created September 27, 2019 06:55
The end swipe scene xml description for motion layout
<ConstraintSet android:id="@+id/endSwipeScene">
<Constraint
android:id="@+id/rootCL"
motion:visibilityMode="ignore"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rotation="45"
android:scaleX="0.95"
android:scaleY="0.95"
android:translationX="600dp" />
@PiotrPrus
PiotrPrus / item_motion_scene
Created December 29, 2019 21:57
an item view layout file
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.MotionLayout
android:layout_width="match_parent"
android:layout_height="300dp"
app:layoutDescription="@xml/ml_scene">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent">
@PiotrPrus
PiotrPrus / rest_state.xml
Last active January 4, 2020 19:49
This file describes initial state for cardview in recyclerview
<?xml version="1.0" encoding="utf-8"?>
<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ConstraintSet android:id="@+id/rest">
<Constraint
android:id="@+id/cardView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="10dp"
@PiotrPrus
PiotrPrus / like_state.xml
Last active January 4, 2020 22:30
Like state of card in RV
<ConstraintSet
android:id="@+id/like"
app:deriveConstraintsFrom="@id/rest">
<Constraint
android:id="@+id/cardViewScene"
android:layout_marginStart="300dp"
android:rotation="45"
app:layout_constraintHeight_percent="0.9"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
@PiotrPrus
PiotrPrus / like_transition.xml
Last active January 4, 2020 22:20
Like transition for card animation in RV
<Transition
app:constraintSetEnd="@id/like"
app:constraintSetStart="@id/rest"
app:duration="100">
<OnSwipe
app:dragDirection="dragRight"
app:onTouchUp="autoCompleteToStart"
app:touchAnchorId="@id/cardView"
app:touchAnchorSide="right" />
@PiotrPrus
PiotrPrus / AnimationState.kt
Created January 7, 2020 22:12
Enum class that represents states of animations in motionLayout
enum class AnimationState {
STARTED, COMPLETED, IN_PROGRESS
}
@PiotrPrus
PiotrPrus / GridItemViewHolder.kt
Created January 7, 2020 22:50
ViewHolder for motionlayout item in grid recyclerView
class ViewHolder(private val mView: View) :
RecyclerView.ViewHolder(mView) {
fun bind(
item: MyItem
animationListener: (state: AnimationState) -> Unit
) {
(mView as MotionLayout).apply {
setTransitionListener(object : MotionLayout.TransitionListener {
override fun onTransitionTrigger(