GradientDrawable 的 setStroke
GradientDrawable's setStroke
我第一次在Whosebug提问,现在我对GradientDrawable的笔画宽度感到困惑,当我使用代码设置TextView背景时,如下
GradientDrawable gd = new GradientDrawable();
gd.setShape(GradientDrawable.RECTANGLE);
gd.setColor(fillColor);
gd.setCornerRadius(roundRadius);
int strokeColor = Color.parseColor(getColor(Subject.list.get(0).color));
gd.setStroke(strokeWidth, strokeColor);
tvSpecial1Title.setText(Subject.list.get(0).title);
tvSpecial1Title.setTextColor(strokeColor);
tvSpecial1Title.setBackgroundDrawable(gd);
tvSpecial1Intro.setText(Subject.list.get(0).intro);
该方法的结果是TextView的笔划宽度比使用XML细,XML定义如下
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#00ffffff" />
<stroke
android:width="0.5dp"
android:color="#ff8200" />
<corners android:radius="2dp" />
<padding
android:bottom="1dp"
android:left="3dp"
android:right="3dp"
android:top="1dp" />
</shape>
使用 XML 如下
TextView t = new TextView(getContext());
t.setBackgroundResource(R.drawable.plugin_movie_item_icon_orange_selector);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params.rightMargin = CommonUtils.dip2px(5);
t.setLayoutParams(params);
t.setGravity(Gravity.CENTER);
t.setIncludeFontPadding(false);
t.setSingleLine();
t.setTextColor(Color.parseColor("#ff8200"));
t.setTextSize(10);`
这两种设置笔划宽度的方法在视觉上导致图像不同,前者的笔划宽度比后者更细,请问有大神帮忙解决一下吗,在此多谢。
将您的 applyDemensions
方法包装在 Math.round()
.
中
Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, strokeWidth, getResources().getDisplayMetrics()));
我第一次在Whosebug提问,现在我对GradientDrawable的笔画宽度感到困惑,当我使用代码设置TextView背景时,如下
GradientDrawable gd = new GradientDrawable();
gd.setShape(GradientDrawable.RECTANGLE);
gd.setColor(fillColor);
gd.setCornerRadius(roundRadius);
int strokeColor = Color.parseColor(getColor(Subject.list.get(0).color));
gd.setStroke(strokeWidth, strokeColor);
tvSpecial1Title.setText(Subject.list.get(0).title);
tvSpecial1Title.setTextColor(strokeColor);
tvSpecial1Title.setBackgroundDrawable(gd);
tvSpecial1Intro.setText(Subject.list.get(0).intro);
该方法的结果是TextView的笔划宽度比使用XML细,XML定义如下
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#00ffffff" />
<stroke
android:width="0.5dp"
android:color="#ff8200" />
<corners android:radius="2dp" />
<padding
android:bottom="1dp"
android:left="3dp"
android:right="3dp"
android:top="1dp" />
</shape>
使用 XML 如下
TextView t = new TextView(getContext());
t.setBackgroundResource(R.drawable.plugin_movie_item_icon_orange_selector);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params.rightMargin = CommonUtils.dip2px(5);
t.setLayoutParams(params);
t.setGravity(Gravity.CENTER);
t.setIncludeFontPadding(false);
t.setSingleLine();
t.setTextColor(Color.parseColor("#ff8200"));
t.setTextSize(10);`
这两种设置笔划宽度的方法在视觉上导致图像不同,前者的笔划宽度比后者更细,请问有大神帮忙解决一下吗,在此多谢。
将您的 applyDemensions
方法包装在 Math.round()
.
Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, strokeWidth, getResources().getDisplayMetrics()));