下面列出了android.content.res.TypedArray#recycle() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void initAttrs(Context context, AttributeSet attrs) {
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.RuleView);
bgColor = ta.getColor(R.styleable.RuleView_zjun_bgColor, Color.parseColor("#f5f8f5"));
gradationColor = ta.getColor(R.styleable.RuleView_zjun_gradationColor, Color.LTGRAY);
shortLineWidth = ta.getDimension(R.styleable.RuleView_gv_shortLineWidth, dp2px(1));
shortGradationLen = ta.getDimension(R.styleable.RuleView_gv_shortGradationLen, dp2px(16));
longGradationLen = ta.getDimension(R.styleable.RuleView_gv_longGradationLen, shortGradationLen * 2);
longLineWidth = ta.getDimension(R.styleable.RuleView_gv_longLineWidth, shortLineWidth * 2);
textColor = ta.getColor(R.styleable.RuleView_zjun_textColor, Color.BLACK);
textSize = ta.getDimension(R.styleable.RuleView_zjun_textSize, sp2px(14));
indicatorLineColor = ta.getColor(R.styleable.RuleView_zjun_indicatorLineColor, Color.parseColor("#48b975"));
indicatorLineWidth = ta.getDimension(R.styleable.RuleView_zjun_indicatorLineWidth, dp2px(3f));
indicatorLineLen = ta.getDimension(R.styleable.RuleView_gv_indicatorLineLen, dp2px(35f));
minValue = ta.getFloat(R.styleable.RuleView_gv_minValue, 0f);
maxValue = ta.getFloat(R.styleable.RuleView_gv_maxValue, 100f);
currentValue = ta.getFloat(R.styleable.RuleView_gv_currentValue, 50f);
gradationUnit = ta.getFloat(R.styleable.RuleView_gv_gradationUnit, .1f);
numberPerCount = ta.getInt(R.styleable.RuleView_gv_numberPerCount, 10);
gradationGap = ta.getDimension(R.styleable.RuleView_gv_gradationGap, dp2px(10));
gradationNumberGap = ta.getDimension(R.styleable.RuleView_gv_gradationNumberGap, dp2px(8));
ta.recycle();
}
public ActionBarContextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SherlockActionMode, defStyle, 0);
setBackgroundDrawable(a.getDrawable(
R.styleable.SherlockActionMode_background));
mTitleStyleRes = a.getResourceId(
R.styleable.SherlockActionMode_titleTextStyle, 0);
mSubtitleStyleRes = a.getResourceId(
R.styleable.SherlockActionMode_subtitleTextStyle, 0);
mContentHeight = a.getLayoutDimension(
R.styleable.SherlockActionMode_height, 0);
mSplitBackground = a.getDrawable(
R.styleable.SherlockActionMode_backgroundSplit);
a.recycle();
}
private void init(Context context, AttributeSet attributeSet) {
mAddButtonPlusColor = getColor(android.R.color.white);
mAddButtonColorNormal = getColor(android.R.color.holo_blue_dark);
mAddButtonColorPressed = getColor(android.R.color.holo_blue_light);
mButtonSpacing = (int) (getResources().getDimension(R.dimen.fab_actions_spacing) - getResources().getDimension(R.dimen.fab_shadow_radius) - getResources().getDimension(R.dimen.fab_shadow_offset));
if (attributeSet != null) {
TypedArray attr = context.obtainStyledAttributes(attributeSet, R.styleable.FloatingActionsMenu, 0, 0);
if (attr != null) {
try {
mAddButtonPlusColor = attr.getColor(R.styleable.FloatingActionsMenu_addButtonPlusIconColor, getColor(android.R.color.white));
mAddButtonColorNormal = attr.getColor(R.styleable.FloatingActionsMenu_addButtonColorNormal, getColor(android.R.color.holo_blue_dark));
mAddButtonColorPressed = attr.getColor(R.styleable.FloatingActionsMenu_addButtonColorPressed, getColor(android.R.color.holo_blue_light));
isHorizontal = attr.getBoolean(R.styleable.FloatingActionsMenu_addButtonIsHorizontal, false);
} finally {
attr.recycle();
}
}
}
createAddButton(context);
}
private void initialize(@NonNull Context context, AttributeSet attrs, int defStyleAttr) {
View rootView = inflate(getContext(), R.layout.bookmark_card, this);
TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
R.styleable.NoteCard,
defStyleAttr, 0);
try {
isShowBook = a.getBoolean(R.styleable.NoteCard_showBook, false);
isDhowAuthor = a.getBoolean(R.styleable.NoteCard_showAuthor, false);
isShowCollection = a.getBoolean(R.styleable.NoteCard_showCollection, false);
isShowCategory = a.getBoolean(R.styleable.NoteCard_showCategory, false);
} finally {
a.recycle();
}
parentTitleTextView = rootView.findViewById(R.id.toc_card_body);
bookInfoTextView = rootView.findViewById(R.id.book_info_text_view);
refreshBookInfo();
pageNumberTextView = rootView.findViewById(R.id.page_part_number);
dateTimeTextView = rootView.findViewById(R.id.date_time);
bookmarkIcon = rootView.findViewById(R.id.bookmark_icon);
}
@SuppressWarnings("ResourceType") // We're hacking `spacingMult = styledAttributes.getFloat`
SubtitlePainter(Context context) {
int[] viewAttr = {android.R.attr.lineSpacingExtra, android.R.attr.lineSpacingMultiplier};
TypedArray styledAttributes = context.obtainStyledAttributes(null, viewAttr, 0, 0);
spacingAdd = styledAttributes.getDimensionPixelSize(0, 0);
spacingMult = styledAttributes.getFloat(1, 1);
styledAttributes.recycle();
Resources resources = context.getResources();
DisplayMetrics displayMetrics = resources.getDisplayMetrics();
int twoDpInPx = Math.round((TWO_DP * displayMetrics.densityDpi) / DisplayMetrics.DENSITY_DEFAULT);
cornerRadius = twoDpInPx;
outlineWidth = twoDpInPx;
shadowRadius = twoDpInPx;
shadowOffset = twoDpInPx;
textPaint = new TextPaint();
textPaint.setAntiAlias(true);
textPaint.setSubpixelText(true);
paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Style.FILL);
}
public static ArrayList<Building> createList(Context ctx) {
Resources res = ctx.getResources();
String[] names = res.getStringArray(R.array.names);
String[] heights = res.getStringArray(R.array.heights);
TypedArray icons = res.obtainTypedArray(R.array.photos);
ArrayList<Building> items = new ArrayList<Building>(names.length);
for (int i = 0; i < names.length; ++i) {
items.add(new Building(names[i], heights[i], icons.getDrawable(i)));
}
icons.recycle();
return items;
}
public PreferenceFrameLayout(
Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
final TypedArray a = context.obtainStyledAttributes(attrs,
com.android.internal.R.styleable.PreferenceFrameLayout, defStyleAttr, defStyleRes);
float density = context.getResources().getDisplayMetrics().density;
int defaultBorderTop = (int) (density * DEFAULT_BORDER_TOP + 0.5f);
int defaultBottomPadding = (int) (density * DEFAULT_BORDER_BOTTOM + 0.5f);
int defaultLeftPadding = (int) (density * DEFAULT_BORDER_LEFT + 0.5f);
int defaultRightPadding = (int) (density * DEFAULT_BORDER_RIGHT + 0.5f);
mBorderTop = a.getDimensionPixelSize(
com.android.internal.R.styleable.PreferenceFrameLayout_borderTop,
defaultBorderTop);
mBorderBottom = a.getDimensionPixelSize(
com.android.internal.R.styleable.PreferenceFrameLayout_borderBottom,
defaultBottomPadding);
mBorderLeft = a.getDimensionPixelSize(
com.android.internal.R.styleable.PreferenceFrameLayout_borderLeft,
defaultLeftPadding);
mBorderRight = a.getDimensionPixelSize(
com.android.internal.R.styleable.PreferenceFrameLayout_borderRight,
defaultRightPadding);
a.recycle();
}
public static int getDrawableRes(Context context, @AttrRes int drawableAttr) {
if(attrMap.containsKey(drawableAttr)) {
return attrMap.get(drawableAttr);
} else {
int[] attrs = new int[]{drawableAttr};
TypedArray typedArray = context.obtainStyledAttributes(attrs);
@DrawableRes int drawableRes = typedArray.getResourceId(0, 0);
typedArray.recycle();
attrMap.put(drawableAttr, drawableRes);
return drawableRes;
}
}
public LayoutParams(Context c, AttributeSet attrs) {
super(c, attrs);
TypedArray a = c.obtainStyledAttributes(attrs,
R.styleable.CollapsingTitleBarLayout_Layout);
mCollapseMode = a.getInt(
R.styleable.CollapsingTitleBarLayout_Layout_collapsing_layout_collapseMode,
COLLAPSE_MODE_OFF);
setParallaxMultiplier(a.getFloat(
R.styleable.CollapsingTitleBarLayout_Layout_collapsing_layout_collapseParallaxMultiplier,
DEFAULT_PARALLAX_MULTIPLIER));
a.recycle();
}
public LayoutParams(Context context, AttributeSet attrs) {
super(context, attrs);
final TypedArray a = context.obtainStyledAttributes(attrs, LAYOUT_ATTRS);
gravity = a.getInteger(0, Gravity.TOP);
a.recycle();
}
private void setBackground() {
int[] attributes = new int[]{R.attr.conversation_list_item_background_read};
TypedArray drawables = context.obtainStyledAttributes(attributes);
setBackgroundDrawable(drawables.getDrawable(0));
drawables.recycle();
}
public LearnMorePreference(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.LearnMorePreference, 0, 0);
mHelpContext = a.getResourceId(R.styleable.LearnMorePreference_helpContext, 0);
a.recycle();
setTitle(R.string.learn_more);
}
private void init(@Nullable final AttributeSet attrs) {
if (attrs == null) {
emojiSize = (int) getTextSize();
} else {
final TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.emoji);
try {
emojiSize = (int) a.getDimension(R.styleable.emoji_emojiSize, getTextSize());
} finally {
a.recycle();
}
}
setText(getText());
}
public static Drawable[] getDrawable(Context context, int[] attrs)
{
TypedArray ta = context.obtainStyledAttributes(attrs);
Drawable[] drawables = new Drawable[attrs.length];
for (int i = 0; i < attrs.length; i++)
{
drawables[i] = ta.getDrawable(i);
}
ta.recycle();
return drawables;
}
/**
* Constructor for inflating from XML.
*/
public PaddedFrameLayout(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PaddedFrameLayout);
mMaxChildWidth = a.getDimensionPixelSize(
R.styleable.PaddedFrameLayout_maxChildWidth, NO_MAX_SIZE);
mMaxChildHeight = a.getDimensionPixelSize(
R.styleable.PaddedFrameLayout_maxChildHeight, NO_MAX_SIZE);
a.recycle();
}
public static int getThemeColor(Context context, int colorAttr) {
Resources.Theme theme = context.getTheme();
TypedArray arr = theme.obtainStyledAttributes(new int[]{colorAttr});
int colorValue = arr.getColor(0, -1);
arr.recycle();
return colorValue;
}
/**
* 读取提示形式 和 提示位置 和 播放延迟
*/
private void initView(AttributeSet attrs){
//这里要做移除,有时在使用中没有手动销毁,所以初始化时要remove
if(mViewPager!=null){
removeView(mViewPager);
}
//初始化自定义属性
TypedArray type = getContext().obtainStyledAttributes(attrs, R.styleable.BannerView);
hintMode = type.getInteger(R.styleable.BannerView_hint_mode, 0);
gravity = type.getInteger(R.styleable.BannerView_hint_gravity, 1);
delay = type.getInt(R.styleable.BannerView_play_delay, 0);
color = type.getColor(R.styleable.BannerView_hint_color, Color.BLACK);
alpha = type.getInt(R.styleable.BannerView_hint_alpha, 0);
paddingLeft = (int) type.getDimension(R.styleable.BannerView_hint_paddingLeft, 0);
paddingRight = (int) type.getDimension(R.styleable.BannerView_hint_paddingRight, 0);
paddingTop = (int) type.getDimension(R.styleable.BannerView_hint_paddingTop, 0);
paddingBottom = (int) type.getDimension(R.styleable.BannerView_hint_paddingBottom
, LibUtils.dip2px(getContext(),4));
type.recycle();
//创建ViewPager
mViewPager = new BannerViewPager(getContext());
mViewPager.setId(R.id.banner_inner);
mViewPager.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT
, LayoutParams.MATCH_PARENT));
addView(mViewPager);
if(hintMode== BannerConstant.HintMode.COLOR_POINT_HINT){
initHint(new ColorPointHintView(getContext(), Color.parseColor("#E3AC42")
, Color.parseColor("#88ffffff")));
}else if(hintMode== BannerConstant.HintMode.TEXT_HINT){
initHint(new TextHintView(getContext()));
}else {
initHint(new ColorPointHintView(getContext(), Color.parseColor("#E3AC42")
, Color.parseColor("#88ffffff")));
}
initGestureDetector();
}
public static void setThemedActionBarDrawable(Context context, ActionBar actionBar) {
TypedArray a = context.obtainStyledAttributes(null, R.styleable.BooksTheme, 0, 0);
actionBar.setBackgroundDrawable(a.getDrawable(0));
a.recycle();
}
public DividerItemDecoration(Context context, int orientation) {
final TypedArray a = context.obtainStyledAttributes(ATTRS);
mDivider = a.getDrawable(0);
a.recycle();
setOrientation(orientation);
}
public DividerItemDecoration(Context context, int orientation) {
final TypedArray a = context.obtainStyledAttributes(ATTRS);
mDivider = a.getDrawable(0);
a.recycle();
setOrientation(orientation);
}