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.wearable.view.DefaultOffsettingHelper; 19 import android.support.wearable.view.WearableRecyclerView; 20 import android.view.View; 21 22 /** 23 * Customizes all items (children) in a {@link WearableRecyclerView} to align to left side of 24 * surface/watch and shrinks each item (child) as you scroll away from it. 25 */ 26 public class ScalingOffsettingHelper extends DefaultOffsettingHelper { 27 28 // Max we scale the child View 29 private static final float MAX_CHILD_SCALE = 0.65f; 30 31 private float mProgressToCenter; 32 ScalingOffsettingHelper()33 public ScalingOffsettingHelper() {} 34 35 // Shrinks icons/text and you scroll away 36 @Override updateChild(View child, WearableRecyclerView parent)37 public void updateChild(View child, WearableRecyclerView parent) { 38 super.updateChild(child, parent); 39 40 // Figure out % progress from top to bottom 41 float centerOffset = ((float) child.getHeight() / 2.0f) / (float) parent.getHeight(); 42 float yRelativeToCenterOffset = (child.getY() / parent.getHeight()) + centerOffset; 43 44 // Normalize for center 45 mProgressToCenter = Math.abs(0.5f - yRelativeToCenterOffset); 46 47 // Adjust to the maximum scale 48 mProgressToCenter = Math.min(mProgressToCenter, MAX_CHILD_SCALE); 49 50 child.setScaleX(1 - mProgressToCenter); 51 child.setScaleY(1 - mProgressToCenter); 52 } 53 }