下面列出了android.content.res.TypedArray#length() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* xmlで定義したboolean arrayを読み込む
* @param res
* @param arrayId
* @param defaultValue
* @return
*/
@NonNull
public static boolean[] readArray(@NonNull final Resources res,
@ArrayRes final int arrayId, final boolean defaultValue) {
boolean[] result;
final TypedArray a = res.obtainTypedArray(arrayId);
try {
final int n = a.length();
result = new boolean[n];
for (int i = 0; i < n; i++) {
result[i] = a.getBoolean(i, defaultValue);
}
} finally {
a.recycle();
}
return result;
}
private void init(AttributeSet attrs, int defStyle) {
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.ImageListPreference, defStyle, 0);
try {
Resources res = getContext().getResources();
int images = a.getResourceId(R.styleable.ImageListPreference_pics, 0);
if (images <= 0) throw new IllegalArgumentException(getClass().getSimpleName() + " must set `pics` attribute.");
TypedArray imageRes = res.obtainTypedArray(images);
int[] imageResIds = new int[imageRes.length()];
for (int i = 0; i < imageResIds.length; ++i)
imageResIds[i] = imageRes.getResourceId(i, 0);
imageRes.recycle();
mImageResources = imageResIds;
int layout = a.getResourceId(R.styleable.ImageListPreference_listItemLayout, 0);
if (layout <= 0) throw new IllegalArgumentException(getClass().getSimpleName() + " must set `layout` attribute.");
mLayout = layout;
} finally {
a.recycle();
}
}
public static Sound[] getSounds(Context context) {
Resources res = context.getApplicationContext().getResources();
TypedArray labels = res.obtainTypedArray(R.array.labels);
TypedArray ids = res.obtainTypedArray(R.array.ids);
Sound[] sounds = new Sound[labels.length()];
for (int i = 0; i < sounds.length; i++) {
sounds[i] = new Sound(labels.getString(i), ids.getResourceId(i, -1));
}
labels.recycle();
ids.recycle();
return sounds;
}
public ServicesRecyclerViewAdapter(Context context, ServiceItemCallback listener) {
super();
this.listener = listener;
mValues = new ArrayList<>();
TypedArray ta = context.getResources().obtainTypedArray(R.array.bots);
for (int i = 0; i < ta.length(); i++) {
int resIdBot = ta.getResourceId(i, -1);
if (resIdBot >= 0) {
TypedArray botArray = context.getResources().obtainTypedArray(resIdBot);
int resIdName = botArray.getResourceId(0, 0);
String nickname = botArray.getString(0);
String jid = botArray.getString(1);
int resIdDescription = botArray.getResourceId(2, 0);
int resIdImage = botArray.getResourceId(3, 0);
BotEntry botEntry = new BotEntry(resIdName, nickname, jid, resIdDescription, resIdImage);
mValues.add(botEntry);
botArray.recycle();
}
}
ta.recycle();
}
public ColorChooserPreferenceX(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
final TypedArray ta = context.getResources().obtainTypedArray(R.array.colorChooserDialog_colors);
mColors = new int[ta.length()];
for (int i = 0; i < ta.length(); i++) {
mColors[i] = ta.getColor(i, 0);
}
ta.recycle();
setWidgetLayoutResource(R.layout.widget_color_chooser_preference); // resource na layout custom preference - TextView-ImageView
setPositiveButtonText(null);
}
public static int[] getResourceIds(Context c, @ArrayRes int array) {
final TypedArray typedArray = c.getResources().obtainTypedArray(array);
final int[] resourceIds = new int[typedArray.length()];
for (int i = 0; i < typedArray.length(); i++) {
resourceIds[i] = typedArray.getResourceId(i, 0);
}
typedArray.recycle();
return resourceIds;
}
public CustomProgressBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomProgressBar, 0, 0);
TypedArray colorsArray = context.getResources().obtainTypedArray(R.array.gradient_colors);
backgroundProgressThickness = typedArray.getInteger(R.styleable.CustomProgressBar_background_progress_thickness, backgroundProgressThickness);
foregroundProgressThickness = typedArray.getInteger(R.styleable.CustomProgressBar_foreground_progress_thickness, foregroundProgressThickness);
progress = typedArray.getFloat(R.styleable.CustomProgressBar_progress, progress);
foregroundProgressColor = typedArray.getInt(R.styleable.CustomProgressBar_foreground_progress_color, foregroundProgressColor);
backgroundProgressColor = typedArray.getColor(R.styleable.CustomProgressBar_background_progress_color, backgroundProgressColor);
roundedCorner = typedArray.getBoolean(R.styleable.CustomProgressBar_rounded_corner, roundedCorner);
min = typedArray.getInt(R.styleable.CustomProgressBar_min, min);
max = typedArray.getInt(R.styleable.CustomProgressBar_max, max);
foregroundBarGradientColors = new int[colorsArray.length()];
for (int i = 0; i < colorsArray.length(); i++) {
foregroundBarGradientColors[i] = colorsArray.getColor(i, 0);
}
typedArray.recycle();
init();
}
private static float[] getFloatArray(TypedArray array) {
final int N = array.length();
float[] vals = new float[N];
for (int i = 0; i < N; i++) {
vals[i] = array.getFloat(i, -1.0f);
}
array.recycle();
return vals;
}
public static int[] getColorArray(@NonNull Context context, @ArrayRes int array) {
if (array == 0) return null;
TypedArray ta = context.getResources().obtainTypedArray(array);
int[] colors = new int[ta.length()];
for (int i = 0; i < ta.length(); i++)
colors[i] = ta.getColor(i, 0);
ta.recycle();
return colors;
}
/***
* 测试数据
* @return List<Integer>
*/
public List<Integer> getDatas() {
TypedArray ar = getResources().obtainTypedArray(R.array.test_arr);
final int[] resIds = new int[ar.length()];
for (int i = 0; i < ar.length(); i++) {
resIds[i] = ar.getResourceId(i, 0);
}
ar.recycle();
List<Integer> tDatas = new ArrayList<>();
for (int resId : resIds) {
tDatas.add(resId);
}
return tDatas;
}
public static int[] getColorArray(@ArrayRes int array) {
if (array == 0)
return null;
TypedArray typedArray = Base.getResources().obtainTypedArray(array);
int[] colors = new int[typedArray.length()];
for (int i = 0; i < typedArray.length(); i++)
colors[i] = typedArray.getColor(i, 0);
typedArray.recycle();
return colors;
}
/**
* Gets the shutter icon res id for a specific mode.
*
* @param modeIndex index of the mode
* @param context current context
* @return mode shutter icon id if the index is valid, otherwise 0.
*/
public static int getCameraShutterIconId(int modeIndex, Context context)
{
// Find the camera mode icon using id
TypedArray shutterIcons = context.getResources()
.obtainTypedArray(R.array.camera_mode_shutter_icon);
if (modeIndex < 0 || modeIndex >= shutterIcons.length())
{
Log.e(TAG, "Invalid mode index: " + modeIndex);
throw new IllegalStateException("Invalid mode index: " + modeIndex);
}
return shutterIcons.getResourceId(modeIndex, 0);
}
/**
* Set layer colors from array resource
*
* @param arrayId array resource
*/
public T setLayerColors(@ArrayRes int arrayId) {
TypedArray colorsArray = context.getResources().obtainTypedArray(arrayId);
int[] colors = new int[colorsArray.length()];
for (int i = 0; i < colorsArray.length(); i++) {
colors[i] = colorsArray.getColor(i, Color.TRANSPARENT);
}
colorsArray.recycle();
return setLayerColors(colors);
}
public ContentAdapter(Context context) {
Resources resources = context.getResources();
mPlaces = resources.getStringArray(R.array.places);
mPlaceDesc = resources.getStringArray(R.array.place_desc);
TypedArray a = resources.obtainTypedArray(R.array.place_avator);
mPlaceAvators = new Drawable[a.length()];
for (int i = 0; i < mPlaceAvators.length; i++) {
mPlaceAvators[i] = a.getDrawable(i);
}
a.recycle();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fading_actionbar_activity_navigation_drawer);
mTitle = mDrawerTitle = getTitle();
mCityNames = getResources().getStringArray(R.array.drawer_items);
TypedArray typedArray = getResources().obtainTypedArray(R.array.city_images);
mCityImages = new int [typedArray.length()];
for (int i = 0; i < typedArray.length(); ++i) {
mCityImages[i] = typedArray.getResourceId(i, 0);
}
typedArray.recycle();
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
// set a custom shadow that overlays the main content when the drawer opens
mDrawerLayout.setDrawerShadow(R.drawable.fading_actionbar_drawer_shadow, GravityCompat.START);
// set up the drawer's list view with items and click listener
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.fading_actionbar_drawer_list_item, mCityNames));
mDrawerList.setOnItemClickListener(this);
// enable ActionBar app icon to behave as action to toggle nav drawer
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
// ActionBarDrawerToggle ties together the the proper interactions
// between the sliding drawer and the action bar app icon
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.fading_actionbar_ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_close /* "close drawer" description for accessibility */
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
selectItem(0);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mRootView = (ViewGroup) inflater.inflate(R.layout.fragment_follow, container, false);
mCircle = mRootView.findViewById(R.id.circle);
FrameLayout.LayoutParams leaderParams = (FrameLayout.LayoutParams) mCircle
.getLayoutParams();
mFollowers = new View[4];
float diameter = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, DIAMETER,
getResources().getDisplayMetrics());
TypedArray circles = getResources().obtainTypedArray(R.array.circles);
// create the circle views
int colorIndex = 1;
for (int i = 0; i < mFollowers.length; i++) {
mFollowers[i] = new View(getActivity());
FrameLayout.LayoutParams params =
new FrameLayout.LayoutParams((int) diameter, (int) diameter);
params.gravity = leaderParams.gravity;
mFollowers[i].setLayoutParams(params);
mFollowers[i].setBackgroundDrawable(getResources().getDrawable(
circles.getResourceId(colorIndex, -1)));
colorIndex++;
if (colorIndex >= circles.length()) {
colorIndex = 0;
}
mRootView.addView(mFollowers[i]);
}
circles.recycle();
/* Animation code */
final SpringSystem springSystem = SpringSystem.create();
// create the springs that control movement
final Spring springX = springSystem.createSpring();
final Spring springY = springSystem.createSpring();
// bind circle movement to events
new Actor.Builder(springSystem, mCircle).addMotion(springX, MotionProperty.X)
.addMotion(springY, MotionProperty.Y).build();
// add springs to connect between the views
final Spring[] followsX = new Spring[mFollowers.length];
final Spring[] followsY = new Spring[mFollowers.length];
for (int i = 0; i < mFollowers.length; i++) {
// create spring to bind views
followsX[i] = springSystem.createSpring();
followsY[i] = springSystem.createSpring();
followsX[i].addListener(new Performer(mFollowers[i], View.TRANSLATION_X));
followsY[i].addListener(new Performer(mFollowers[i], View.TRANSLATION_Y));
// imitates another character
final SpringImitator followX = new SpringImitator(followsX[i]);
final SpringImitator followY = new SpringImitator(followsY[i]);
// imitate the previous character
if (i == 0) {
springX.addListener(followX);
springY.addListener(followY);
} else {
followsX[i - 1].addListener(followX);
followsY[i - 1].addListener(followY);
}
}
return mRootView;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mRootView = (RelativeLayout) inflater.inflate(R.layout.fragment_bloom, container, false);
mCircles = new View[6];
float diameter = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, DIAMETER,
getResources().getDisplayMetrics());
final TypedArray circles = getResources().obtainTypedArray(R.array.circles);
// layout params
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((int) diameter,
(int) diameter);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
// create the circle views
int colorIndex = 0;
for (int i = 0; i < mCircles.length; i++) {
mCircles[i] = new View(getActivity());
mCircles[i].setLayoutParams(params);
mCircles[i].setBackgroundDrawable(getResources().getDrawable(circles
.getResourceId(colorIndex, -1)));
colorIndex++;
if (colorIndex >= circles.length()) {
colorIndex = 0;
}
mRootView.addView(mCircles[i]);
}
circles.recycle();
/* Animations! */
final SpringSystem springSystem = SpringSystem.create();
// create spring
final Spring spring = springSystem.createSpring();
// add listeners along arc
double arc = 2 * Math.PI / mCircles.length;
for (int i = 0; i < mCircles.length; i++) {
View view = mCircles[i];
// map spring to a line segment from the center to the edge of the ring
spring.addListener(new MapPerformer(view, View.TRANSLATION_X, 0, 1,
0, (float) (RING_DIAMETER * Math.cos(i * arc))));
spring.addListener(new MapPerformer(view, View.TRANSLATION_Y, 0, 1,
0, (float) (RING_DIAMETER * Math.sin(i * arc))));
spring.setEndValue(CLOSED);
}
mRootView.setOnTouchListener(new ToggleImitator(spring, CLOSED, OPEN));
return mRootView;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation_drawer);
mTitle = mDrawerTitle = getTitle();
mCityNames = getResources().getStringArray(R.array.drawer_items);
TypedArray typedArray = getResources().obtainTypedArray(R.array.city_images);
mCityImages = new int [typedArray.length()];
for (int i = 0; i < typedArray.length(); ++i) {
mCityImages[i] = typedArray.getResourceId(i, 0);
}
typedArray.recycle();
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
// set a custom shadow that overlays the main content when the drawer opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
// set up the drawer's list view with items and click listener
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, mCityNames));
mDrawerList.setOnItemClickListener(this);
// enable ActionBar app icon to behave as action to toggle nav drawer
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
// ActionBarDrawerToggle ties together the the proper interactions
// between the sliding drawer and the action bar app icon
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_close /* "close drawer" description for accessibility */
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
selectItem(0);
}
}
final void initBaseXMLAttrs(Context context, AttributeSet attrs) {
final TypedArray arr = context.obtainStyledAttributes(attrs, R.styleable.EmojiReactionView);
// Log.i("point 152", "attrs" );
final int N = arr.getIndexCount();
for (int i = 0; i < N; ++i) {
int attr = arr.getIndex(i);
if (attr == R.styleable.EmojiReactionView_emojis) {
final TypedArray resourceArray = context.getResources().obtainTypedArray(arr.getResourceId(R.styleable.EmojiReactionView_emojis, 0));
// Get emojis id from attribute and store it in arraylist
for (int j = 0; j < resourceArray.length(); j++) {
emojiId.add(resourceArray.getResourceId(j, 0));
}
numberOfEmojis = emojiId.size();
resourceArray.recycle();
} else if (attr == R.styleable.EmojiReactionView_set_emoji) {
clickedEmojiNumber = arr.getInt(attr, clickedEmojiNumber);
} else if (attr == R.styleable.EmojiReactionView_home_Center_X) {
homeCenter[0] = arr.getDimensionPixelSize(attr, homeCenter[0]);
} else if (attr == R.styleable.EmojiReactionView_home_Center_Y) {
homeCenterYGiven = arr.getDimensionPixelSize(attr, -1);
} else if (attr == R.styleable.EmojiReactionView_home_side) {
homeSide = arr.getDimensionPixelSize(attr, homeSide);
} else if (attr == R.styleable.EmojiReactionView_panel_center_X) {
if (arr.peekValue(attr).type == TYPE_DIMENSION)
panelCentreGiven[0] = arr.getDimensionPixelSize(attr, -2);
else {
panelCentreGiven[0] = checkFraction(arr.getFraction(attr, -1, -1, -2));
}
} else if (attr == R.styleable.EmojiReactionView_panel_center_Y) {
if (arr.peekValue(attr).type == TYPE_DIMENSION)
panelCentreGiven[1] = arr.getDimensionPixelSize(attr, -2);
else
panelCentreGiven[1] = checkFraction(arr.getFraction(attr, -1, -1, -2));
} else if (attr == R.styleable.EmojiReactionView_panel_radius) {
panelRadiusGiven = arr.getDimensionPixelSize(attr, -1);
} else if (attr == R.styleable.EmojiReactionView_panel_emoji_side) {
panelEmojiSide = arr.getDimensionPixelSize(attr, panelEmojiSide);
} else if (attr == R.styleable.EmojiReactionView_emojis_rising_height) {
emojisRisingHeightGiven = checkFraction(arr.getFraction(attr, -1, -1, -2));
} else if (attr == R.styleable.EmojiReactionView_emojis_rising_speed) {
emojisRisingSpeed = arr.getDimensionPixelSize(attr, -1);
} else if (attr == R.styleable.EmojiReactionView_emojis_rising_number) {
numberOfRisers = arr.getInt(attr, numberOfRisers);
}
}
if (clickedEmojiNumber >= numberOfEmojis || clickedEmojiNumber < -1) {
throw new IllegalArgumentException("set_emoji can't be more than number of emojis!");
}
arr.recycle();
numberOfEmojis = emojiId.size();
emojiBitmap = new Bitmap[numberOfEmojis];
}
private void init(AttributeSet attrs, int defStyleAttr, int defStyleRes) {
final Context context = getContext();
final Resources resource = getResources();
if (attrs == null) {
// default we init a circle style loading drawable
setProgressStyle(STYLE_CIRCLE);
return;
}
final float density = resource.getDisplayMetrics().density;
// default size 2dp
final int baseSize = (int) (density * 2);
// Load attributes
final TypedArray a = context.obtainStyledAttributes(
attrs, R.styleable.Loading, defStyleAttr, defStyleRes);
int bgLineSize = a.getDimensionPixelOffset(R.styleable.Loading_gBackgroundLineSize, baseSize);
int fgLineSize = a.getDimensionPixelOffset(R.styleable.Loading_gForegroundLineSize, baseSize);
int bgColor = 0;// transparent color
ColorStateList colorStateList = a.getColorStateList(R.styleable.Loading_gBackgroundColor);
if (colorStateList != null)
bgColor = colorStateList.getDefaultColor();
int fgColor = Color.BLACK;
int[] fgColorArray = null;
try {
fgColor = a.getColor(R.styleable.Loading_gForegroundColor, 0);
} catch (Exception ignored) {
int fgColorId = a.getResourceId(R.styleable.Loading_gForegroundColor, R.array.g_default_loading_fg);
// Check for IDE preview render
if (!isInEditMode()) {
TypedArray taColor = resource.obtainTypedArray(fgColorId);
int length = taColor.length();
if (length > 0) {
fgColorArray = new int[length];
for (int i = 0; i < length; i++) {
fgColorArray[i] = taColor.getColor(i, Color.BLACK);
}
} else {
String type = resource.getResourceTypeName(fgColorId);
try {
switch (type) {
case "color":
fgColor = resource.getColor(fgColorId);
break;
case "array":
fgColorArray = resource.getIntArray(fgColorId);
break;
default:
fgColorArray = resource.getIntArray(R.array.g_default_loading_fg);
break;
}
} catch (Exception e) {
fgColorArray = resource.getIntArray(R.array.g_default_loading_fg);
}
}
taColor.recycle();
}
}
int style = a.getInt(R.styleable.Loading_gProgressStyle, 1);
boolean autoRun = a.getBoolean(R.styleable.Loading_gAutoRun, true);
float progress = a.getFloat(R.styleable.Loading_gProgressFloat, 0);
a.recycle();
setProgressStyle(style);
setAutoRun(autoRun);
setProgress(progress);
setBackgroundLineSize(bgLineSize);
setForegroundLineSize(fgLineSize);
setBackgroundColor(bgColor);
if (fgColorArray == null) {
setForegroundColor(fgColor);
} else {
setForegroundColor(fgColorArray);
}
}