Skip to content

Instantly share code, notes, and snippets.

@RequiresApi(Build.VERSION_CODES.S)
val metaBallRenderEffect =
RenderEffect.createChainEffect(
RenderEffect.createColorFilterEffect(
ColorMatrixColorFilter(
ColorMatrix(
floatArrayOf(
1f, 0f, 0f, 0f, 0f,
0f, 1f, 0f, 0f, 0f,
0f, 0f, 1f, 0f, 0f,
//const val ASPECT_RATIO = 4/3f you can play with it
const val ASPECT_RATIO = 1.55f
@Composable
fun ScrollPositionListItem(
itemUi: ListItemUi,
listState: LazyListState,
index: Int,
modifier: Modifier = Modifier,
viewportHeight: Float
@Composable
fun ParallaxRoute(
viewModel: ParallaxVewModel = hiltViewModel(),
) {
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
ParallaxScreen(uiState)
}
@Composable
fun ParallaxScreen(
@yuriyskulskiy
yuriyskulskiy / AnimatedLayout.java
Created August 19, 2020 20:19
AnimatedLayout part 5: update LinearLayout with parallaxValue
public void applyParallax(float translateValue) {
mBackgroundImg.setTranslationY(translateValue - this.getHeight() / 8f);
mTitleTV.setTranslationY(+translateValue * 2);
mProfileCircleIV.setTranslationY(translateValue * 2);
mWeatherIcon.setTranslationY(translateValue * 2);
}
@yuriyskulskiy
yuriyskulskiy / CustomLinearLayout.java
Created August 19, 2020 20:04
AnimatedLayout part 5: update LinearLayout with parallaxValue
@Override
public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler,
RecyclerView.State state) {
...
if (parentLayout != null) {
animatedLayout = parentLayout.findViewById(R.id.transformItem);
float translateValue = computeCurrentParallaxOffset(parentLayout);
animatedLayout.applyParallax(translateValue);
}
...
@yuriyskulskiy
yuriyskulskiy / AnimatedLayout.java
Created August 17, 2020 19:39
AnimatedLayout part 4: fix incorrect text width bug
public class AnimatedLayout extends ConstraintLayout {
...
private void applySummerTitleTV() {
mTitleTV.setBackgroundResource(R.drawable.summer_text_background);
mTitleTV.setTextColor(Color.BLACK);
mTitleTV.setText("WINTER IS COMING...");
// fix bug: text view with wrap_content layout param
// does not resize its width and
// last word of new text will not displayed if new string is longer
@yuriyskulskiy
yuriyskulskiy / AnimatedLayout.java
Last active August 17, 2020 18:57
AnimatedLayout part 4: update AnimatedLayout.java
...
private int mIdleState = SUMMER_STATE; // set initial state as SUMMER_STATE
...
public void animateBy(int dy) {
float newOffset;
if (dy > 0) {
newOffset = (mOffsetValue - dy) < 0 ? 0 : mOffsetValue - dy;
} else {
//scroll to the left
@yuriyskulskiy
yuriyskulskiy / CustomLinearLayout.java
Last active August 17, 2020 18:48
AnimatedLayout part 4: implement CustomLinearLayout
public class CustomLinearLayoutManager extends LinearLayoutManager {
public CustomLinearLayoutManager(Context context) {
super(context);
}
public CustomLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
super(context, orientation, reverseLayout);
}
public CustomLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
@yuriyskulskiy
yuriyskulskiy / AnimatedLayout.java
Last active August 17, 2020 03:02
AnimatedLayout part 3: implement fling end listener
private void init() {
...
mFling.addEndListener(new DynamicAnimation.OnAnimationEndListener() {
@Override
public void onAnimationEnd(DynamicAnimation animation, boolean canceled, float value, float velocity) {
if (!canceled) {
if (mOffsetValue == 0 || mOffsetValue == getWidth()) {
//correct ending
mCurrentAnimation = IDLE_ANIMATION_STATE;
if (mOffsetValue == 0) {
@yuriyskulskiy
yuriyskulskiy / AnimatedLayout.java
Created August 17, 2020 02:35
AnimatedLayout part 3: update onTouch() method
@Override
public boolean onTouchEvent(MotionEvent event) {
isTouching = true;
mFling.cancel();
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
mPreviousTouchX = event.getX();
if (mVelocityTracker == null) {
// Retrieve a new VelocityTracker object to watch the velocity
// of a motion.