|
| 1 | +package per.goweii.actionbarex.common; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.content.res.TypedArray; |
| 5 | +import android.graphics.Bitmap; |
| 6 | +import android.graphics.drawable.Drawable; |
| 7 | +import android.support.annotation.ColorInt; |
| 8 | +import android.support.annotation.ColorRes; |
| 9 | +import android.support.annotation.DrawableRes; |
| 10 | +import android.support.annotation.NonNull; |
| 11 | +import android.support.annotation.StringRes; |
| 12 | +import android.support.v4.content.ContextCompat; |
| 13 | +import android.text.TextUtils; |
| 14 | +import android.util.AttributeSet; |
| 15 | +import android.util.TypedValue; |
| 16 | +import android.view.Gravity; |
| 17 | +import android.widget.FrameLayout; |
| 18 | +import android.widget.ImageView; |
| 19 | + |
| 20 | +public final class ActionView extends FrameLayout { |
| 21 | + |
| 22 | + private String text; |
| 23 | + private float textSize; |
| 24 | + private int textColor; |
| 25 | + private int[] textPadding = {0, 0, 0, 0}; |
| 26 | + private int[] textMargin = {0, 0, 0, 0}; |
| 27 | + private int iconRes; |
| 28 | + private int iconColor; |
| 29 | + private int[] iconPadding = {0, 0, 0, 0}; |
| 30 | + private int[] iconMargin = {0, 0, 0, 0}; |
| 31 | + |
| 32 | + private ActionIconView iconView; |
| 33 | + private ActionTextView textView; |
| 34 | + |
| 35 | + public ActionView(Context context) { |
| 36 | + this(context, null); |
| 37 | + } |
| 38 | + |
| 39 | + public ActionView(Context context, AttributeSet attrs) { |
| 40 | + this(context, attrs, 0); |
| 41 | + } |
| 42 | + |
| 43 | + public ActionView(Context context, AttributeSet attrs, int defStyleAttr) { |
| 44 | + super(context, attrs, defStyleAttr); |
| 45 | + initAttrs(attrs); |
| 46 | + initView(); |
| 47 | + } |
| 48 | + |
| 49 | + private void initAttrs(AttributeSet attrs) { |
| 50 | + final float textSizeDef = getContext().getResources().getDimension(R.dimen.title_bar_text_size_def); |
| 51 | + final int textColorDef = ContextCompat.getColor(getContext(), R.color.text_color_def); |
| 52 | + final float textPaddingLeftDef = getContext().getResources().getDimension(R.dimen.title_bar_text_padding_left_def); |
| 53 | + final float textPaddingRightDef = getContext().getResources().getDimension(R.dimen.title_bar_text_padding_right_def); |
| 54 | + final int iconColorDef = ContextCompat.getColor(getContext(), R.color.icon_color_def); |
| 55 | + final float iconPaddingDef = getContext().getResources().getDimension(R.dimen.title_bar_icon_padding_def); |
| 56 | + |
| 57 | + TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.ActionView); |
| 58 | + text = typedArray.getString(R.styleable.ActionView_av_text); |
| 59 | + textSize = typedArray.getDimension(R.styleable.ActionView_av_textSize, textSizeDef); |
| 60 | + textColor = typedArray.getColor(R.styleable.ActionView_av_textSize, textColorDef); |
| 61 | + int textPaddingTemp = (int) typedArray.getDimension(R.styleable.ActionView_av_textPadding, -1); |
| 62 | + textPadding[0] = (int) typedArray.getDimension(R.styleable.ActionView_av_textPaddingLeft, textPaddingTemp >= 0 ? textPaddingTemp : textPaddingLeftDef); |
| 63 | + textPadding[1] = (int) typedArray.getDimension(R.styleable.ActionView_av_textPaddingTop, textPaddingTemp >= 0 ? textPaddingTemp : 0); |
| 64 | + textPadding[2] = (int) typedArray.getDimension(R.styleable.ActionView_av_textPaddingRight, textPaddingTemp >= 0 ? textPaddingTemp : textPaddingRightDef); |
| 65 | + textPadding[3] = (int) typedArray.getDimension(R.styleable.ActionView_av_textPaddingBottom, textPaddingTemp >= 0 ? textPaddingTemp : 0); |
| 66 | + int textMarginTemp = (int) typedArray.getDimension(R.styleable.ActionView_av_textMargin, -1); |
| 67 | + textMargin[0] = (int) typedArray.getDimension(R.styleable.ActionView_av_textMarginLeft, textMarginTemp >= 0 ? textMarginTemp : 0); |
| 68 | + textMargin[1] = (int) typedArray.getDimension(R.styleable.ActionView_av_textMarginTop, textMarginTemp >= 0 ? textMarginTemp : 0); |
| 69 | + textMargin[2] = (int) typedArray.getDimension(R.styleable.ActionView_av_textMarginRight, textMarginTemp >= 0 ? textMarginTemp : 0); |
| 70 | + textMargin[3] = (int) typedArray.getDimension(R.styleable.ActionView_av_textMarginBottom, textMarginTemp >= 0 ? textMarginTemp : 0); |
| 71 | + iconRes = typedArray.getResourceId(R.styleable.ActionView_av_icon, 0); |
| 72 | + iconColor = typedArray.getColor(R.styleable.ActionView_av_iconColor, iconColorDef); |
| 73 | + int iconPaddingTemp = (int) typedArray.getDimension(R.styleable.ActionView_av_iconPadding, -1); |
| 74 | + iconPadding[0] = (int) typedArray.getDimension(R.styleable.ActionView_av_iconPaddingLeft, iconPaddingTemp >= 0 ? iconPaddingTemp : iconPaddingDef); |
| 75 | + iconPadding[1] = (int) typedArray.getDimension(R.styleable.ActionView_av_iconPaddingTop, iconPaddingTemp >= 0 ? iconPaddingTemp : iconPaddingDef); |
| 76 | + iconPadding[2] = (int) typedArray.getDimension(R.styleable.ActionView_av_iconPaddingRight, iconPaddingTemp >= 0 ? iconPaddingTemp : iconPaddingDef); |
| 77 | + iconPadding[3] = (int) typedArray.getDimension(R.styleable.ActionView_av_iconPaddingBottom, iconPaddingTemp >= 0 ? iconPaddingTemp : iconPaddingDef); |
| 78 | + int iconMarginTemp = (int) typedArray.getDimension(R.styleable.ActionView_av_iconMargin, -1); |
| 79 | + iconMargin[0] = (int) typedArray.getDimension(R.styleable.ActionView_av_iconMarginLeft, iconMarginTemp >= 0 ? iconMarginTemp : 0); |
| 80 | + iconMargin[1] = (int) typedArray.getDimension(R.styleable.ActionView_av_iconMarginTop, iconMarginTemp >= 0 ? iconMarginTemp : 0); |
| 81 | + iconMargin[2] = (int) typedArray.getDimension(R.styleable.ActionView_av_iconMarginRight, iconMarginTemp >= 0 ? iconMarginTemp : 0); |
| 82 | + iconMargin[3] = (int) typedArray.getDimension(R.styleable.ActionView_av_iconMarginBottom, iconMarginTemp >= 0 ? iconMarginTemp : 0); |
| 83 | + typedArray.recycle(); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 88 | + super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 89 | + } |
| 90 | + |
| 91 | + private void initView() { |
| 92 | + iconView = createIconView(); |
| 93 | + textView = createTextView(); |
| 94 | + addViewInLayout(iconView, getChildCount(), iconView.getLayoutParams()); |
| 95 | + addViewInLayout(textView, getChildCount(), textView.getLayoutParams()); |
| 96 | + setTextColor(textColor); |
| 97 | + setTextSizePx(textSize); |
| 98 | + setIconColorInt(iconColor); |
| 99 | + if (iconRes > 0) { |
| 100 | + setIcon(iconRes); |
| 101 | + } else if (!TextUtils.isEmpty(text)) { |
| 102 | + setText(text); |
| 103 | + } else { |
| 104 | + toggleToGone(); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private ActionIconView createIconView() { |
| 109 | + ActionIconView iconView = new ActionIconView(getContext()); |
| 110 | + iconView.setScaleType(ImageView.ScaleType.FIT_CENTER); |
| 111 | + LayoutParams layoutParams = new LayoutParams(0, LayoutParams.MATCH_PARENT); |
| 112 | + layoutParams.leftMargin = iconMargin[0]; |
| 113 | + layoutParams.topMargin = iconMargin[1]; |
| 114 | + layoutParams.rightMargin = iconMargin[2]; |
| 115 | + layoutParams.bottomMargin = iconMargin[3]; |
| 116 | + iconView.setLayoutParams(layoutParams); |
| 117 | + iconView.setPadding(iconPadding[0], iconPadding[1], iconPadding[2], iconPadding[3]); |
| 118 | + iconView.setVisibility(GONE); |
| 119 | + return iconView; |
| 120 | + } |
| 121 | + |
| 122 | + private ActionTextView createTextView() { |
| 123 | + ActionTextView textView = new ActionTextView(getContext()); |
| 124 | + textView.setGravity(Gravity.CENTER); |
| 125 | + textView.setSingleLine(true); |
| 126 | + LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT); |
| 127 | + layoutParams.leftMargin = textMargin[0]; |
| 128 | + layoutParams.topMargin = textMargin[1]; |
| 129 | + layoutParams.rightMargin = textMargin[2]; |
| 130 | + layoutParams.bottomMargin = textMargin[3]; |
| 131 | + textView.setLayoutParams(layoutParams); |
| 132 | + textView.setPadding(textPadding[0], textPadding[1], textPadding[2], textPadding[3]); |
| 133 | + textView.setVisibility(GONE); |
| 134 | + return textView; |
| 135 | + } |
| 136 | + |
| 137 | + public void toggleToText() { |
| 138 | + iconView.setVisibility(GONE); |
| 139 | + textView.setVisibility(VISIBLE); |
| 140 | + setVisibility(VISIBLE); |
| 141 | + } |
| 142 | + |
| 143 | + public void toggleToIcon() { |
| 144 | + textView.setVisibility(GONE); |
| 145 | + iconView.setVisibility(VISIBLE); |
| 146 | + setVisibility(VISIBLE); |
| 147 | + } |
| 148 | + |
| 149 | + public void toggleToGone() { |
| 150 | + textView.setVisibility(GONE); |
| 151 | + iconView.setVisibility(GONE); |
| 152 | + setVisibility(GONE); |
| 153 | + } |
| 154 | + |
| 155 | + public void setText(@NonNull CharSequence text) { |
| 156 | + textView.setText(text); |
| 157 | + toggleToText(); |
| 158 | + } |
| 159 | + |
| 160 | + public void setText(@StringRes int textId) { |
| 161 | + textView.setText(textId); |
| 162 | + toggleToText(); |
| 163 | + } |
| 164 | + |
| 165 | + public void setTextSize(float sp) { |
| 166 | + textView.setTextSize(sp); |
| 167 | + } |
| 168 | + |
| 169 | + public void setTextSizePx(float px) { |
| 170 | + textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, px); |
| 171 | + } |
| 172 | + |
| 173 | + public void setTextColor(@ColorInt int colorInt) { |
| 174 | + textView.setTextColor(colorInt); |
| 175 | + } |
| 176 | + |
| 177 | + public void setTextColorRes(@ColorRes int colorRes) { |
| 178 | + textView.setTextColor(ContextCompat.getColor(getContext(), colorRes)); |
| 179 | + } |
| 180 | + |
| 181 | + public void setTextPadding(int left, int top, int right, int bottom){ |
| 182 | + textView.setPadding(left, top, right, bottom); |
| 183 | + } |
| 184 | + |
| 185 | + public void setTextMargin(int left, int top, int right, int bottom){ |
| 186 | + LayoutParams layoutParams = (LayoutParams) textView.getLayoutParams(); |
| 187 | + layoutParams.leftMargin = left; |
| 188 | + layoutParams.topMargin = top; |
| 189 | + layoutParams.rightMargin = right; |
| 190 | + layoutParams.bottomMargin = bottom; |
| 191 | + textView.setLayoutParams(layoutParams); |
| 192 | + } |
| 193 | + |
| 194 | + public void setIcon(@DrawableRes int iconId) { |
| 195 | + iconView.setImageResource(iconId); |
| 196 | + toggleToIcon(); |
| 197 | + } |
| 198 | + |
| 199 | + public void setIcon(Drawable icon) { |
| 200 | + iconView.setImageDrawable(icon); |
| 201 | + toggleToIcon(); |
| 202 | + } |
| 203 | + |
| 204 | + public void setIcon(Bitmap icon) { |
| 205 | + iconView.setImageBitmap(icon); |
| 206 | + toggleToIcon(); |
| 207 | + } |
| 208 | + |
| 209 | + public void setIconColorInt(@ColorInt int colorInt) { |
| 210 | + iconView.setColorFilter(colorInt); |
| 211 | + } |
| 212 | + |
| 213 | + public void setIconColorRes(@ColorRes int colorRes) { |
| 214 | + iconView.setColorFilter(ContextCompat.getColor(getContext(), colorRes)); |
| 215 | + } |
| 216 | + |
| 217 | + public void setIconPadding(int left, int top, int right, int bottom){ |
| 218 | + iconView.setPadding(left, top, right, bottom); |
| 219 | + } |
| 220 | + |
| 221 | + public void setIconMargin(int left, int top, int right, int bottom){ |
| 222 | + LayoutParams layoutParams = (LayoutParams) iconView.getLayoutParams(); |
| 223 | + layoutParams.leftMargin = left; |
| 224 | + layoutParams.topMargin = top; |
| 225 | + layoutParams.rightMargin = right; |
| 226 | + layoutParams.bottomMargin = bottom; |
| 227 | + iconView.setLayoutParams(layoutParams); |
| 228 | + } |
| 229 | + |
| 230 | +} |
0 commit comments