Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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.android.launcher2; |
| 18 | |
| 19 | import android.content.Context; |
Winson Chung | 6205422 | 2011-11-03 13:12:50 -0700 | [diff] [blame] | 20 | import android.content.res.TypedArray; |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 21 | import android.graphics.Canvas; |
Winson Chung | 6205422 | 2011-11-03 13:12:50 -0700 | [diff] [blame] | 22 | import android.graphics.drawable.Drawable; |
| 23 | import android.graphics.drawable.StateListDrawable; |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 24 | import android.util.AttributeSet; |
Winson Chung | 6205422 | 2011-11-03 13:12:50 -0700 | [diff] [blame] | 25 | import android.widget.ImageView; |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 26 | import android.widget.LinearLayout; |
| 27 | |
Winson Chung | 6205422 | 2011-11-03 13:12:50 -0700 | [diff] [blame] | 28 | import com.android.launcher.R; |
| 29 | |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 30 | public class HolographicLinearLayout extends LinearLayout { |
| 31 | |
| 32 | private final HolographicViewHelper mHolographicHelper; |
Winson Chung | 6205422 | 2011-11-03 13:12:50 -0700 | [diff] [blame] | 33 | private ImageView mImageView; |
| 34 | private int mImageViewId; |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 35 | |
| 36 | public HolographicLinearLayout(Context context) { |
| 37 | this(context, null); |
| 38 | } |
| 39 | |
| 40 | public HolographicLinearLayout(Context context, AttributeSet attrs) { |
| 41 | this(context, attrs, 0); |
| 42 | } |
| 43 | |
| 44 | public HolographicLinearLayout(Context context, AttributeSet attrs, int defStyle) { |
| 45 | super(context, attrs, defStyle); |
| 46 | |
Winson Chung | 6205422 | 2011-11-03 13:12:50 -0700 | [diff] [blame] | 47 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.HolographicLinearLayout, |
| 48 | defStyle, 0); |
| 49 | mImageViewId = a.getResourceId(R.styleable.HolographicLinearLayout_sourceImageViewId, -1); |
| 50 | a.recycle(); |
| 51 | |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 52 | setWillNotDraw(false); |
| 53 | mHolographicHelper = new HolographicViewHelper(context); |
| 54 | } |
| 55 | |
| 56 | @Override |
Winson Chung | 6205422 | 2011-11-03 13:12:50 -0700 | [diff] [blame] | 57 | protected void drawableStateChanged() { |
| 58 | super.drawableStateChanged(); |
| 59 | |
| 60 | if (mImageView != null) { |
| 61 | Drawable d = mImageView.getDrawable(); |
| 62 | if (d instanceof StateListDrawable) { |
| 63 | StateListDrawable sld = (StateListDrawable) d; |
| 64 | sld.setState(getDrawableState()); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
Winson Chung | bb185bd | 2011-11-21 12:31:42 -0800 | [diff] [blame] | 69 | void invalidatePressedFocusedStates() { |
| 70 | mHolographicHelper.invalidatePressedFocusedStates(mImageView); |
Winson Chung | 7cc3c7f | 2012-06-07 15:18:57 -0700 | [diff] [blame] | 71 | invalidate(); |
Winson Chung | bb185bd | 2011-11-21 12:31:42 -0800 | [diff] [blame] | 72 | } |
| 73 | |
Winson Chung | 6205422 | 2011-11-03 13:12:50 -0700 | [diff] [blame] | 74 | @Override |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 75 | protected void onDraw(Canvas canvas) { |
| 76 | super.onDraw(canvas); |
| 77 | |
| 78 | // One time call to generate the pressed/focused state -- must be called after |
| 79 | // measure/layout |
Winson Chung | 6205422 | 2011-11-03 13:12:50 -0700 | [diff] [blame] | 80 | if (mImageView == null) { |
| 81 | mImageView = (ImageView) findViewById(mImageViewId); |
| 82 | } |
| 83 | mHolographicHelper.generatePressedFocusedStates(mImageView); |
Winson Chung | 7d3810d | 2011-10-07 13:11:56 -0700 | [diff] [blame] | 84 | } |
| 85 | } |