下面列出了怎么用android.support.annotation.Dimension的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
float limit = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 25, getResources().getDisplayMetrics());
// Handle item selection
switch (item.getItemId()) {
case R.id.menu_action_refresh:
if (ratingBar1.getStarsSeparation() <= limit) {
ratingBar1.setStarsSeparation(ratingBar1.getStarsSeparation(Dimension.DP) + 4, Dimension.DP);
ratingBar2.setStarsSeparation(ratingBar2.getStarsSeparation(Dimension.DP) + 4, Dimension.DP);
ratingBar3.setStarsSeparation(ratingBar3.getStarsSeparation(Dimension.DP) + 4, Dimension.DP);
ratingBar4.setStarsSeparation(ratingBar4.getStarsSeparation(Dimension.DP) + 4, Dimension.DP);
ratingBar5.setStarsSeparation(ratingBar5.getStarsSeparation(Dimension.DP) + 4, Dimension.DP);
} else {
ratingBar1.setStarsSeparation(4, Dimension.DP);
ratingBar2.setStarsSeparation(6, Dimension.DP);
ratingBar3.setStarsSeparation(8, Dimension.DP);
ratingBar4.setStarsSeparation(10, Dimension.DP);
ratingBar5.setStarsSeparation(12, Dimension.DP);
}
return false;
default:
return super.onOptionsItemSelected(item);
}
}
/**
* Set the padding between the target bounds and the rectangle edge.
*
* @param padding The distance from the target edge to the rectangle edge.
* @return This prompt focal.
*/
@NonNull
public RectanglePromptFocal setTargetPadding(@Dimension final float padding)
{
mPadding = padding;
return this;
}
public void setDotNormalSize(@Dimension int dotNormalSize) {
mDotNormalSize = dotNormalSize;
for (int i = 0; i < sDotCount; i++) {
for (int j = 0; j < sDotCount; j++) {
mDotStates[i][j] = new DotState();
mDotStates[i][j].mSize = mDotNormalSize;
}
}
invalidate();
}
/**
* How much of the button must the user swipe to trigger the OnSwipeListener successfully
*
* @param swipeDistance float from 0.0 to 1.0 where 1.0 means user must swipe the button fully from end to end. Default is 0.85.
*/
public void setSwipeDistance(@Dimension float swipeDistance) {
if (swipeDistance > 1.0f) {
swipeDistance = 1.0f;
}
if (swipeDistance < 0.0f) {
swipeDistance = 0.0f;
}
this.swipeDistance = swipeDistance;
}
/**
* Converts dps to pixels nicely.
*
* @param context the Context for getting the resources
* @param dp dimension in dps
* @return dimension in pixels
*/
protected static int dpToPixel(@NonNull Context context, @Dimension(unit = DP) float dp) {
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
try {
return (int) (dp * metrics.density);
} catch (NoSuchFieldError ignored) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, metrics);
}
}
public DefaultItemDecoration(@ColorInt int backgroundColor,
@ColorInt int dividerColor,
@Dimension int middlePaddingLeft) {
mBackgroundPaint = new Paint();
mBackgroundPaint.setColor(backgroundColor);
mDividerPaint = new Paint();
mDividerPaint.setColor(dividerColor);
mPaddingLeft = middlePaddingLeft;
}
/**
* 设置viewpager间距
* @param viewPagerMargin 单位dp
*/
public void setViewPagerMargin(@Dimension int viewPagerMargin) {
this.mViewPagerMargin = viewPagerMargin;
if (mViewPager != null) {
mViewPager.setPageMargin(XBannerUtils.dp2px(getContext(), viewPagerMargin));
}
}
/**
* Parses attributes defined in XML.
*/
private void parseAttrs(AttributeSet attrs) {
TypedArray arr = getContext().obtainStyledAttributes(attrs, R.styleable.SimpleRatingBar);
borderColor = arr.getColor(R.styleable.SimpleRatingBar_srb_borderColor, getResources().getColor(R.color.srb_golden_stars));
fillColor = arr.getColor(R.styleable.SimpleRatingBar_srb_fillColor, borderColor);
starBackgroundColor = arr.getColor(R.styleable.SimpleRatingBar_srb_starBackgroundColor, Color.TRANSPARENT);
backgroundColor = arr.getColor(R.styleable.SimpleRatingBar_srb_backgroundColor, Color.TRANSPARENT);
pressedBorderColor = arr.getColor(R.styleable.SimpleRatingBar_srb_pressedBorderColor, borderColor);
pressedFillColor = arr.getColor(R.styleable.SimpleRatingBar_srb_pressedFillColor, fillColor);
pressedStarBackgroundColor = arr.getColor(R.styleable.SimpleRatingBar_srb_pressedStarBackgroundColor, starBackgroundColor);
pressedBackgroundColor = arr.getColor(R.styleable.SimpleRatingBar_srb_pressedBackgroundColor, backgroundColor);
numberOfStars = arr.getInteger(R.styleable.SimpleRatingBar_srb_numberOfStars, 5);
starsSeparation = arr.getDimensionPixelSize(R.styleable.SimpleRatingBar_srb_starsSeparation, (int)valueToPixels(4, Dimension.DP));
maxStarSize = arr.getDimensionPixelSize(R.styleable.SimpleRatingBar_srb_maxStarSize, Integer.MAX_VALUE);
desiredStarSize = arr.getDimensionPixelSize(R.styleable.SimpleRatingBar_srb_starSize, Integer.MAX_VALUE);
stepSize = arr.getFloat(R.styleable.SimpleRatingBar_srb_stepSize, 0.1f);
starBorderWidth = arr.getFloat(R.styleable.SimpleRatingBar_srb_starBorderWidth, 5f);
starCornerRadius = arr.getFloat(R.styleable.SimpleRatingBar_srb_starCornerRadius, 6f);
rating = normalizeRating(arr.getFloat(R.styleable.SimpleRatingBar_srb_rating, 0f));
isIndicator = arr.getBoolean(R.styleable.SimpleRatingBar_srb_isIndicator, false);
drawBorderEnabled = arr.getBoolean(R.styleable.SimpleRatingBar_srb_drawBorderEnabled, true);
gravity = Gravity.fromId(arr.getInt(R.styleable.SimpleRatingBar_srb_gravity, Gravity.Left.id));
arr.recycle();
validateAttrs();
}
/**
* Convenience method to convert a value in the given dimension to pixels.
* @param value
* @param dimen
* @return
*/
private float valueToPixels(float value, @Dimension int dimen) {
switch (dimen) {
case Dimension.DP:
return applyDimension(COMPLEX_UNIT_DIP, value, getResources().getDisplayMetrics());
case Dimension.SP:
return applyDimension(COMPLEX_UNIT_SP, value, getResources().getDisplayMetrics());
default:
return value;
}
}
/**
* Convenience method to convert a value from pixels to the given dimension.
* @param value
* @param dimen
* @return
*/
private float valueFromPixels(float value, @Dimension int dimen) {
switch (dimen) {
case Dimension.DP:
return value / getResources().getDisplayMetrics().density;
case Dimension.SP:
return value / getResources().getDisplayMetrics().scaledDensity;
default:
return value;
}
}
private void addTag(String tagName, int id, ViewGroup column, int color) {
ToggleButton tagButton = new ToggleButton(this);
if(color != -1) {
LinearLayout colorBox = new LinearLayout(this);
// Convert the dps to pixels, based on density scale
final int tagPaddingPx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
1, getResources().getDisplayMetrics());
final int tagPaddingPxBottom = (int) TypedValue.applyDimension(TypedValue
.COMPLEX_UNIT_DIP,
3, getResources().getDisplayMetrics());
//use a GradientDrawable with only one color set, to make it a solid color
colorBox.setBackgroundResource(R.drawable.tag_round_corner);
GradientDrawable gradientDrawable = (GradientDrawable) colorBox.getBackground();
gradientDrawable.setColor(color);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams
.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params.setMargins(tagPaddingPx,tagPaddingPx,tagPaddingPx,tagPaddingPxBottom);
colorBox.setLayoutParams(params);
colorBox.addView(tagButton);
column.addView(colorBox);
} else {
column.addView(tagButton);
}
tagButton.setTextOff(tagName);
tagButton.setTextOn(tagName);
boolean isChecked = checkedTags.contains(id);
tagButton.setChecked(isChecked);
if(isChecked) {
tagButton.setTextColor(selectedColor);
}
tagButton.setOnCheckedChangeListener(new TagStateListener(id, checkedTags));
tagButton.setMinHeight(0);
tagButton.setMinimumHeight(0);
tagButton.setTextSize(Dimension.SP, 12);
tagButton.setEnabled(record == null || record.getUploadId().isEmpty());
tagButton.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
tagButton.invalidate();
}
@Dimension
public float getTextSize() {
return mTextPaint.getTextSize();
}
public void setTextSize(@Dimension float size) {
mTextPaint.setTextSize(size);
updateTextBounds();
invalidate();
}
@Dimension
public float getStrokeWidth() {
return mStrokePaint.getStrokeWidth();
}
public void setStrokeWidth(@Dimension float width) {
mStrokePaint.setStrokeWidth(width);
invalidate();
}
public void setPathWidth(@Dimension int pathWidth) {
mPathWidth = pathWidth;
initView();
invalidate();
}
public void setDotSelectedSize(@Dimension int dotSelectedSize) {
mDotSelectedSize = dotSelectedSize;
}
@NonNull
public CirclePatternCell.Builder setRadius(@Dimension final float radius) {
mRadius = radius;
return this;
}
@NonNull
public CirclePatternCell.Builder setStrokeWidth(@Dimension final float strokeWidth) {
mStrokeWidth = strokeWidth;
return this;
}
@NonNull
public DotPatternCell.Builder setRadius(@Dimension final float radius) {
mRadius = radius;
return this;
}
@Dimension
public float getFingerPrintStatusTextSize() {
return mBoxFingerprint.getStatusTextSize();
}
public void setFingerPrintStatusTextSize(@Dimension final float statusTextSize) {
mBoxFingerprint.setStatusTextSize(statusTextSize);
invalidate();
}
@Dimension
public abstract float getIndicatorWidth();
@NonNull
public CircleIndicator.Builder setIndicatorRadius(@Dimension final float indicatorRadius) {
mIndicatorRadius = indicatorRadius;
return this;
}
@NonNull
public CircleIndicator.Builder setIndicatorStrokeWidth(@Dimension final float indicatorStrokeWidth) {
mIndicatorStrokeWidth = indicatorStrokeWidth;
return this;
}
@Dimension
@Override
public float getIndicatorWidth() {
return mIndicatorRadius;
}
@NonNull
public DotIndicator.Builder setIndicatorRadius(@Dimension final float indicatorRadius) {
mIndicatorRadius = indicatorRadius;
return this;
}
@Dimension
@Override
public float getIndicatorWidth() {
return mIndicatorRadius;
}
@NonNull
public Builder setKeyPadding(@Dimension final float keyPadding) {
mKeyPadding = keyPadding;
return this;
}
@NonNull
public RoundKey.Builder setKeyPadding(@Dimension final float keyPadding) {
mKeyPadding = keyPadding;
return this;
}