[Android]【安卓】TypedArray的getDimension()方法

[Android]【安卓】TypedArray的getDimension()方法
本篇博客已收录到我的安卓开发小结中——点击【安卓开发小结】

  参考资料:http://blog.csdn.net/joe_storm/article/details/38978595
  当你自定义view里的values的xml里定义的attr的format为dimension,可用以下三种方法获取布局xml设置的尺寸值:

  • getDimension()
  • getDimensionPixelOffset()
  • getDimensionPixelSize()

这三个方法都是根据DisplayMetrics获取相应的值,不同在于方法1直接保存float型数据,方法2直接对float取整,方法3对float小数先四舍五入后取整。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Android 的 TextView 上实现描边效果,你可以使用以下方法之一: 方法一:使用 SpannableString 你可以使用 SpannableString 和 TextPaint 来自定义 TextView 的绘制效果。首先,创建一个 SpannableString 对象,然后使用 TextPaint 设置描边效果,最后将 SpannableString 应用到 TextView 上。 示例代码如下: ```java SpannableString spannableString = new SpannableString("Your text"); spannableString.setSpan(new CustomOutlineSpan(strokeWidth, strokeColor), 0, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); textView.setText(spannableString); ``` 其中,CustomOutlineSpan 是一个自定义的 Span,用于设置描边效果。你可以根据需要调整 strokeWidth(描边宽度)和 strokeColor(描边颜色)。 ```java class CustomOutlineSpan extends ReplacementSpan { private float strokeWidth; private int strokeColor; public CustomOutlineSpan(float strokeWidth, int strokeColor) { this.strokeWidth = strokeWidth; this.strokeColor = strokeColor; } @Override public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) { return (int) paint.measureText(text, start, end); } @Override public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) { // 保存原始的 paint 设置 Paint.Style style = paint.getStyle(); float textSize = paint.getTextSize(); int color = paint.getColor(); // 设置描边效果 paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(strokeWidth); paint.setColor(strokeColor); // 绘制描边文字 canvas.drawText(text, start, end, x, y, paint); // 恢复原始的 paint 设置 paint.setStyle(style); paint.setTextSize(textSize); paint.setColor(color); } } ``` 方法二:使用自定义 TextView 你也可以创建一个自定义的 TextView,并在其 onDraw() 方法中实现描边效果。在绘制文本之前先绘制描边效果,然后再绘制文本内容。 示例代码如下: ```java public class OutlinedTextView extends AppCompatTextView { private float strokeWidth; private int strokeColor; public OutlinedTextView(Context context) { super(context); } public OutlinedTextView(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.OutlinedTextView); strokeWidth = a.getDimension(R.styleable.OutlinedTextView_strokeWidth, 0); strokeColor = a.getColor(R.styleable.OutlinedTextView_strokeColor, Color.BLACK); a.recycle(); } @Override protected void onDraw(Canvas canvas) { // 保存原始的 paint 设置 Paint.Style style = getPaint().getStyle(); float textSize = getTextSize(); int color = getCurrentTextColor(); // 设置描边效果 getPaint().setStyle(Paint.Style.STROKE); getPaint().setStrokeWidth(strokeWidth); getPaint().setColor(strokeColor); // 绘制描边文字 super.onDraw(canvas); // 恢复原始的 paint 设置 getPaint().setStyle(style); setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize); setTextColor(color); // 绘制文本内容 super.onDraw(canvas); } } ``` 使用时,在布局文件中使用 OutlinedTextView 替代 TextView,并设置 strokeWidth(描边宽度)和 strokeColor(描边颜色)属性。 ```xml <com.example.OutlinedTextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Your text" app:strokeWidth="2dp" app:strokeColor="#FF0000" /> ``` 这样就可以在 Android 的 TextView 上实现描边效果了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值