下面列出了android.util.TypedValue#applyDimension ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public ColorPicker(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mFillPaint = new Paint();
mSaturationGradientPaint = new Paint();
mSaturationGradientPaint.setDither(true);
mValueGradientPaint = new Paint();
mValueGradientPaint.setDither(true);
mCirclePaint = new Paint();
mCirclePaint.setStyle(Paint.Style.STROKE);
mCirclePaint.setStrokeWidth(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
3.f, getResources().getDisplayMetrics()));
mCircleInnerPaint = new Paint();
mCircleSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
8.f, getResources().getDisplayMetrics());
mCircleTouchSize = mCircleSize * 2.5f;
mTouchTapMaxDist = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
8.f, getResources().getDisplayMetrics());
mRadius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
2.f, getResources().getDisplayMetrics());
}
public FlowerAnimation(Context paramContext)
{
super(paramContext);
int[] arrayOfInt = new int[3];
// arrayOfInt[0] = ContextCompat.getColor(getContext(), 2131624049);
// arrayOfInt[1] = ContextCompat.getColor(getContext(), 2131624050);
// arrayOfInt[2] = ContextCompat.getColor(getContext(), 2131624051);
this.a = arrayOfInt;
this.m = 8;
this.n = 0;
this.o = 0;
this.p = 15;
this.q = 0.2F;
this.r = 28;
this.s = 35;
this.t = (int)TypedValue.applyDimension(1, 1.0F, getResources().getDisplayMetrics());
this.v = null;
this.w = TypedValue.applyDimension(1, 60.0F, getResources().getDisplayMetrics());
this.x = getClass().getSimpleName();
a(paramContext);
}
public GridAutofitLayoutManager(Context context, int columnWidth) {
/* Initially set spanCount to 1, will be changed automatically later. */
super(context, 1);
// interprete as dip
columnWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, columnWidth,
context.getResources().getDisplayMetrics());
setColumnWidth(checkedColumnWidth(context, columnWidth));
}
public TitleItemDecoration(Context context, List<T> datas, String topTag) {
super();
mDatas = datas;
mTopTag = topTag;
mPaint = new Paint();
mBounds = new Rect();
mTitleHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 30, context.getResources().getDisplayMetrics());
int titleFontSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16,
context.getResources().getDisplayMetrics());
mPaint.setTextSize(titleFontSize);
mPaint.setAntiAlias(true);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
if (widthMode == MeasureSpec.UNSPECIFIED || widthMode == MeasureSpec.AT_MOST){
widthSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, BTN_WIDTH, r.getDisplayMetrics());
widthMeasureSpec = MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.EXACTLY);
}
if (heightMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.AT_MOST) {heightSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, BTN_HEIGHT,r.getDisplayMetrics());
heightMeasureSpec = MeasureSpec.makeMeasureSpec(heightSize, MeasureSpec.EXACTLY);
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
private static
int getDp(float pixels)
{
float floatDp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, pixels, s_displayMetrics);
return Math.round(floatDp);
}
public static float getPixels(int unit, float size){
DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
return TypedValue.applyDimension(unit, size, metrics);
}
public static float dipToPx(Context context, float value) {
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, metrics);
}
public static int sp2px(Context context, float sp) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, context.getResources().getDisplayMetrics());
}
public MouseCursor(GameActivity activity, Osc osc) {
this.osc = osc;
Resources r = activity.getResources();
int height = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 30, r.getDisplayMetrics());
int width = (int) Math.round(height / 1.5);
cursor = new FixedSizeImageView(activity, width, height);
cursor.setImageResource(R.drawable.pointer_arrow);
cursor.setLayoutParams(new RelativeLayout.LayoutParams(width, height));
layout = activity.getLayout();
layout.addView(cursor);
choreographer = Choreographer.getInstance();
choreographer.postFrameCallback(this);
}
private int dpToPx(int dp) {
Resources r = getResources();
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics());
}
/**
* Helper method to convert dips to pixels.
*/
private int dipsToPix(float dps) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dps,
getResources().getDisplayMetrics());
}
private float dpToPx(float dp){
return TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
}
static void update1x1(Context context, AppWidgetManager appWidgetManager, int widgetId) {
Resources r = context.getResources();
float dp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, r.getDisplayMetrics());
LocalDateTime now = LocalDateTime.now();
LocalDate today = now.toLocalDate();
Theme theme = WidgetUtils.getTheme(widgetId);
Times times = WidgetUtils.getTimes(widgetId);
if (times == null) {
WidgetUtils.showNoCityWidget(context, appWidgetManager, widgetId);
return;
}
WidgetUtils.Size size = WidgetUtils.getSize(context, appWidgetManager, widgetId, 1f);
int s = size.width;
if (s <= 0)
return;
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.vakit_widget);
int next = times.getNextTime();
String left = LocaleUtils.formatPeriod(now, times.getTime(today, next), false);
if (Preferences.VAKIT_INDICATOR_TYPE.get().equals("next"))
next = next + 1;
remoteViews.setOnClickPendingIntent(R.id.widget, TimesFragment.getPendingIntent(times));
Bitmap bmp = Bitmap.createBitmap(s, s, Bitmap.Config.ARGB_4444);
Canvas canvas = new Canvas(bmp);
canvas.scale(0.99f, 0.99f, s / 2f, s / 2f);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setDither(true);
paint.setFilterBitmap(true);
paint.setStyle(Paint.Style.FILL);
paint.setColor(theme.bgcolor);
canvas.drawRect(0, 0, s, s, paint);
paint.setColor(theme.textcolor);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
paint.setAntiAlias(true);
paint.setSubpixelText(true);
paint.setColor(theme.hovercolor);
String city = times.getName();
paint.setColor(theme.textcolor);
float cs = s / 5f;
float ts = (s * 35) / 100f;
int vs = s / 4;
paint.setTextSize(cs);
cs = (cs * s * 0.9f) / paint.measureText(city);
cs = (cs > vs) ? vs : cs;
paint.setTextSize(vs);
paint.setTextAlign(Paint.Align.CENTER);
canvas.drawText(Vakit.getByIndex(next).prevTime().getString(), s / 2f, (s * 22) / 80f, paint);
paint.setTextSize(ts);
paint.setTextAlign(Paint.Align.CENTER);
canvas.drawText(left, s / 2f, (s / 2f) + ((ts * 1) / 3), paint);
paint.setTextSize(cs);
paint.setTextAlign(Paint.Align.CENTER);
canvas.drawText(city, s / 2f, ((s * 3) / 4f) + ((cs * 2) / 3), paint);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(dp);
paint.setColor(theme.strokecolor);
canvas.drawRect(0, 0, s, s, paint);
remoteViews.setImageViewBitmap(R.id.widget, bmp);
try {
appWidgetManager.updateAppWidget(widgetId, remoteViews);
} catch (RuntimeException e) {
if (!e.getMessage().contains("exceeds maximum bitmap memory usage")) {
Crashlytics.logException(e);
}
}
}
public static float getDip(Context context) {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, context.getResources().getDisplayMetrics());
}
public TitleChanger(TextView title) {
this.title = title;
Resources res = title.getResources();
animDelay = DEFAULT_ANIMATION_DELAY;
animDuration = res.getInteger(android.R.integer.config_shortAnimTime) / 2;
yTranslate = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, DEFAULT_Y_TRANSLATION_DP, res.getDisplayMetrics()
);
}
/**
* Converts the given device independent pixels (DIP) value into the corresponding pixels
* value for the current screen.
*
* @param context Context instance
* @param dip The DIP value to convert
* @return The pixels value for the current screen of the given DIP value.
*/
public static int convertDIPToPixels(Context context, int dip) {
if (context == null) return 0;
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, displayMetrics);
}
/**
* dp 2 px
*
* @param context
* @param dpVal
* @return
*/
protected int dp2px(Context context, int dpVal) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
dpVal, context.getResources().getDisplayMetrics());
}
/**
* dp转换px
*
* @param dp dp值
* @return 转换后的px值
*/
protected static float dp2px(float dp) {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, Resources.getSystem().getDisplayMetrics());
}
/**
* dp转px
*
* @param context
* 上下文
* @param dpVal
* dp值
* @return px值
*
* */
public static int dp2px(Context context, float dpVal)
{
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
dpVal, context.getResources().getDisplayMetrics());
}