我正在尝试动态创建一个可绘制对象以用作自定义线性布局的背景。它需要有哈希标记等(没什么大不了的),但也有数字标记哈希标记是什么(如尺子)。我知道我可以创建文本元素并将它们放在线性布局中,并在可绘制对象中添加散列标记,但我希望将它们也包含在可绘制对象中,因此我不必进行两次测量计算。
5 回答
下面是一个简单的示例 a TextDrawable
,它的工作方式类似于普通的可绘制对象,但允许您将文本指定为唯一的构造函数变量:
public class TextDrawable extends Drawable {
private final String text;
private final Paint paint;
public TextDrawable(String text) {
this.text = text;
this.paint = new Paint();
paint.setColor(Color.WHITE);
paint.setTextSize(22f);
paint.setAntiAlias(true);
paint.setFakeBoldText(true);
paint.setShadowLayer(6f, 0, 0, Color.BLACK);
paint.setStyle(Paint.Style.FILL);
paint.setTextAlign(Paint.Align.LEFT);
}
@Override
public void draw(Canvas canvas) {
canvas.drawText(text, 0, 0, paint);
}
@Override
public void setAlpha(int alpha) {
paint.setAlpha(alpha);
}
@Override
public void setColorFilter(ColorFilter cf) {
paint.setColorFilter(cf);
}
@Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}
}
我读过“Professional Android 2 Application Development”一书(Reto Meier 着)。其中,它包含一个示例项目,您可以在其中创建一个简单的指南针应用程序,您可以在其中“绘制”文本、标记等。
简要说明是您创建了一个扩展android.view.View
该类并覆盖该onDraw(Canvas)
方法的类。
本书的所有源代码都可以在这里下载:http ://www.wrox.com/WileyCDA/WroxTitle/Professional-Android-2-Application-Development.productCd-0470565527,descCd-DOWNLOAD.html 。如果你下载代码并查看名为“Chapter 4 Compass”的项目,我相信你会找到你想要的:)
查看 Plowman 的答案并尝试根据我的需要进行调整,我在此链接中偶然发现了一个用于 Camera 的类
这是TextDrawable 类的代码。看起来与 Plowmans 非常相似,但对我来说效果更好:
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.TypedValue;
public class TextDrawable extends Drawable {
private static final int DEFAULT_COLOR = Color.WHITE;
private static final int DEFAULT_TEXTSIZE = 15;
private Paint mPaint;
private CharSequence mText;
private int mIntrinsicWidth;
private int mIntrinsicHeight;
public TextDrawable(Resources res, CharSequence text) {
mText = text;
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setColor(DEFAULT_COLOR);
mPaint.setTextAlign(Align.CENTER);
float textSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
DEFAULT_TEXTSIZE, res.getDisplayMetrics());
mPaint.setTextSize(textSize);
mIntrinsicWidth = (int) (mPaint.measureText(mText, 0, mText.length()) + .5);
mIntrinsicHeight = mPaint.getFontMetricsInt(null);
}
@Override
public void draw(Canvas canvas) {
Rect bounds = getBounds();
canvas.drawText(mText, 0, mText.length(),
bounds.centerX(), bounds.centerY(), mPaint);
}
@Override
public int getOpacity() {
return mPaint.getAlpha();
}
@Override
public int getIntrinsicWidth() {
return mIntrinsicWidth;
}
@Override
public int getIntrinsicHeight() {
return mIntrinsicHeight;
}
@Override
public void setAlpha(int alpha) {
mPaint.setAlpha(alpha);
}
@Override
public void setColorFilter(ColorFilter filter) {
mPaint.setColorFilter(filter);
}
}
要回答上述关于如何使文本居中的评论:
mPaint.textAlign = Align.CENTER
...
// Centering for mixed case letters
canvas.drawText(mText, 0, mText.length,
bounds.centerX().toFloat(), bounds.centerY().toFloat() - ((mPaint.descent() + mPaint.ascent()) / 2), mPaint)
// Centering for all uppercase letters
canvas.drawText(mText, 0, mText.length,
bounds.centerX().toFloat(), bounds.centerY().toFloat() - mPaint.ascent() / 2, mPaint)
这允许您将任何 View 放入 Drawable,包括 TextView。您甚至可以在 XML 布局中使用样式。
public class ViewDrawable extends Drawable {
final View mView;
public ViewDrawable(final Context context, final @LayoutRes int layoutId) {
this(LayoutInflater.from(context).inflate(layoutId, null));
}
public ViewDrawable(final @NonNull View view) {
mView = view;
}
public View getView() {
return mView;
}
@Override
public void setBounds(int left, int top, int right, int bottom) {
super.setBounds(left, top, right, bottom);
final int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(right - left, View.MeasureSpec.EXACTLY);
final int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(bottom - top, View.MeasureSpec.EXACTLY);
mView.measure(widthMeasureSpec, heightMeasureSpec);
mView.layout(left, top, right, bottom);
}
@Override
public void draw(@NonNull Canvas canvas) {
mView.draw(canvas);
}
@Override
public void setAlpha(int alpha) {
mView.setAlpha(alpha/255f);
}
@Override
public void setColorFilter(@Nullable ColorFilter colorFilter) {
}
@Override
public int getOpacity() {
return PixelFormat.UNKNOWN;
}
}