close
The Wayback Machine - https://web.archive.org/web/20150905190742/https://developer.android.com/samples/SpeedTracker/Wearable/src/com.example.android.wearable.speedtracker/ui/SpeedPickerLayout.html
Show navigation Hide navigation
SpeedTracker / Wearable / src / com.example.android.wearable.speedtracker / ui /

SpeedPickerLayout.java

1
/*
2
 * Copyright (C) 2014 Google Inc. All Rights Reserved.
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.wearable.speedtracker.ui;
18
 
19
import android.content.Context;
20
import android.graphics.drawable.GradientDrawable;
21
import android.support.wearable.view.WearableListView;
22
import android.util.AttributeSet;
23
import android.widget.ImageView;
24
import android.widget.LinearLayout;
25
import android.widget.TextView;
26
 
27
import com.example.android.wearable.speedtracker.R;
28
 
29
/**
30
 * A simple extension of the {@link android.widget.LinearLayout} to represent a single item in a
31
 * {@link android.support.wearable.view.WearableListView}.
32
 */
33
public class SpeedPickerLayout extends LinearLayout
34
        implements WearableListView.OnCenterProximityListener {
35
 
36
    private final float mFadedTextAlpha;
37
    private final int mFadedCircleColor;
38
    private final int mChosenCircleColor;
39
    private ImageView mCircle;
40
    private TextView mName;
41
 
42
    public SpeedPickerLayout(Context context) {
43
        this(context, null);
44
    }
45
 
46
    public SpeedPickerLayout(Context context, AttributeSet attrs) {
47
        this(context, attrs, 0);
48
    }
49
 
50
    public SpeedPickerLayout(Context context, AttributeSet attrs,
51
            int defStyle) {
52
        super(context, attrs, defStyle);
53
        mFadedTextAlpha = getResources()
54
                .getInteger(R.integer.action_text_faded_alpha) / 100f;
55
        mFadedCircleColor = getResources().getColor(R.color.grey);
56
        mChosenCircleColor = getResources().getColor(R.color.blue);
57
    }
58
 
59
    // Get references to the icon and text in the item layout definiton
60
    @Override
61
    protected void onFinishInflate() {
62
        super.onFinishInflate();
63
        mCircle = (ImageView) findViewById(R.id.circle);
64
        mName = (TextView) findViewById(R.id.name);
65
    }
66
 
67
    @Override
68
    public void onCenterPosition(boolean animate) {
69
        mName.setAlpha(1f);
70
        ((GradientDrawable) mCircle.getDrawable()).setColor(mChosenCircleColor);
71
    }
72
 
73
    @Override
74
    public void onNonCenterPosition(boolean animate) {
75
        ((GradientDrawable) mCircle.getDrawable()).setColor(mFadedCircleColor);
76
        mName.setAlpha(mFadedTextAlpha);
77
 
78
    }
79
}