1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.example.android.wearable.wear.wearnotifications; 17 18 import android.support.v7.widget.RecyclerView; 19 import android.support.wear.widget.WearableLinearLayoutManager; 20 import android.support.wear.widget.WearableRecyclerView; 21 import android.view.View; 22 23 /** 24 * Shrinks items (children) farther away from the center in a {@link WearableRecyclerView}. The UX 25 * makes scrolling more readable. 26 */ 27 public class ScalingScrollLayoutCallback extends WearableLinearLayoutManager.LayoutCallback { 28 29 // Max we scale the child View. 30 private static final float MAX_CHILD_SCALE = 0.65f; 31 32 private float mProgressToCenter; 33 34 /* 35 * Scales the item's icons and text the farther away they are from center allowing the main 36 * item to be more readable to the user on small devices like Wear. 37 */ 38 @Override 39 public void onLayoutFinished(View child, RecyclerView parent) { 40 41 // Figure out % progress from top to bottom. 42 float centerOffset = ((float) child.getHeight() / 2.0f) / (float) parent.getHeight(); 43 float yRelativeToCenterOffset = (child.getY() / parent.getHeight()) + centerOffset; 44 45 // Normalizes for center. 46 mProgressToCenter = Math.abs(0.5f - yRelativeToCenterOffset); 47 48 // Adjusts to the maximum scale. 49 mProgressToCenter = Math.min(mProgressToCenter, MAX_CHILD_SCALE); 50 51 child.setScaleX(1 - mProgressToCenter); 52 child.setScaleY(1 - mProgressToCenter); 53 } 54 }