Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [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 android.widget; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.graphics.Canvas; |
| 21 | import android.util.AttributeSet; |
| 22 | import android.view.View; |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * Space is a lightweight View subclass that may be used to create gaps between components |
| 26 | * in general purpose layouts. |
| 27 | */ |
| 28 | public final class Space extends View { |
| 29 | /** |
| 30 | * {@inheritDoc} |
| 31 | */ |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 32 | public Space(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 33 | super(context, attrs, defStyleAttr, defStyleRes); |
Philip Milne | 4a145d7 | 2011-09-29 14:14:25 -0700 | [diff] [blame] | 34 | if (getVisibility() == VISIBLE) { |
| 35 | setVisibility(INVISIBLE); |
| 36 | } |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | /** |
| 40 | * {@inheritDoc} |
| 41 | */ |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 42 | public Space(Context context, AttributeSet attrs, int defStyleAttr) { |
| 43 | this(context, attrs, defStyleAttr, 0); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * {@inheritDoc} |
| 48 | */ |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 49 | public Space(Context context, AttributeSet attrs) { |
Philip Milne | 4a145d7 | 2011-09-29 14:14:25 -0700 | [diff] [blame] | 50 | this(context, attrs, 0); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | /** |
| 54 | * {@inheritDoc} |
| 55 | */ |
| 56 | public Space(Context context) { |
Philip Milne | 4a145d7 | 2011-09-29 14:14:25 -0700 | [diff] [blame] | 57 | //noinspection NullableProblems |
| 58 | this(context, null); |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Draw nothing. |
| 63 | * |
| 64 | * @param canvas an unused parameter. |
| 65 | */ |
| 66 | @Override |
| 67 | public void draw(Canvas canvas) { |
| 68 | } |
| 69 | |
| 70 | /** |
Philip Milne | a789caf | 2011-06-17 13:07:38 -0700 | [diff] [blame] | 71 | * Compare to: {@link View#getDefaultSize(int, int)} |
| 72 | * If mode is AT_MOST, return the child size instead of the parent size |
| 73 | * (unless it is too big). |
| 74 | */ |
| 75 | private static int getDefaultSize2(int size, int measureSpec) { |
| 76 | int result = size; |
| 77 | int specMode = MeasureSpec.getMode(measureSpec); |
| 78 | int specSize = MeasureSpec.getSize(measureSpec); |
| 79 | |
| 80 | switch (specMode) { |
| 81 | case MeasureSpec.UNSPECIFIED: |
| 82 | result = size; |
| 83 | break; |
| 84 | case MeasureSpec.AT_MOST: |
| 85 | result = Math.min(size, specSize); |
| 86 | break; |
| 87 | case MeasureSpec.EXACTLY: |
| 88 | result = specSize; |
| 89 | break; |
| 90 | } |
| 91 | return result; |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 96 | setMeasuredDimension( |
| 97 | getDefaultSize2(getSuggestedMinimumWidth(), widthMeasureSpec), |
| 98 | getDefaultSize2(getSuggestedMinimumHeight(), heightMeasureSpec)); |
| 99 | } |
Philip Milne | 3f8956d | 2011-05-13 17:29:00 +0100 | [diff] [blame] | 100 | } |