1 /*
2  * Copyright (C) 2013 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 
17 package com.example.android.slidingfragments;
18 
19 import android.content.Context;
20 import android.util.AttributeSet;
21 import android.widget.LinearLayout;
22 
23 /**
24  * In order to animate the fragment containing text on/off the screen,
25  * it is required that we know the height of the device being used. However,
26  * this can only be determined at runtime, so we cannot specify the required
27  * translation in an xml file. Since FragmentTransaction's setCustomAnimations
28  * method requires an ID of an animation defined via an xml file, this linear
29  * layout was built as a workaround. This custom linear layout is created to specify
30  * the location of the fragment's layout as a fraction of the device's height. By
31  * animating yFraction from 0 to 1, we can animate the fragment from the top of
32  * the screen to the bottom of the screen, regardless of the device's specific size.
33  */
34 public class FractionalLinearLayout extends LinearLayout {
35 
36     private float mYFraction;
37     private int mScreenHeight;
38 
FractionalLinearLayout(Context context)39     public FractionalLinearLayout(Context context) {
40         super(context);
41     }
42 
FractionalLinearLayout(Context context, AttributeSet attrs)43     public FractionalLinearLayout(Context context, AttributeSet attrs) {
44         super(context, attrs);
45     }
46 
47     @Override
onSizeChanged(int w, int h, int oldw, int oldh)48     protected void onSizeChanged (int w, int h, int oldw, int oldh) {
49         super.onSizeChanged(w, h, oldw, oldh);
50         mScreenHeight = h;
51         setY(mScreenHeight);
52     }
53 
getYFraction()54     public float getYFraction() {
55         return mYFraction;
56     }
57 
setYFraction(float yFraction)58     public void setYFraction(float yFraction) {
59         mYFraction = yFraction;
60         setY((mScreenHeight > 0) ? (mScreenHeight - yFraction * mScreenHeight) : 0);
61     }
62 }
63