我已经设法创造了油漆,这创造了一个非常好的流畅的笔画周围的文字。但是我很难把它画到我需要它的地方,主要是我相信我是在扩展视图而不是TextView?
我的目标是能够在我想要的布局上对任何文本进行一次笔划。
DrawView.java
public class DrawView extends View{
Paint paint = new Paint();
String text = "Blank";
public DrawView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public DrawView(Context context) {
super(context);
}
public void setText(String text) {
this.text = text;
}
public String getText() {
return text;
}
public void draw(Canvas canvas ) {
Paint strokePaint = new Paint();
strokePaint.setARGB(255, 0, 0, 0);
strokePaint.setTextSize(28);
//strokePaint.setTypeface(tf);
strokePaint.setStyle(Paint.Style.STROKE);
strokePaint.setStrokeJoin(Paint.Join.ROUND);
strokePaint.setStrokeCap(Paint.Cap.ROUND);
strokePaint.setStrokeWidth(9);
strokePaint.setAntiAlias(true);
Paint textPaint = new Paint();
textPaint.setARGB(255, 255, 255, 255);
textPaint.setTextSize(28);
//textPaint.setTypeface(tf);
textPaint.setAntiAlias(true);
canvas.drawText(text, 99, 99, strokePaint);
canvas.drawText(text, 99, 101, strokePaint);
canvas.drawText(text, 101, 99, strokePaint);
canvas.drawText(text, 101, 101, strokePaint);
canvas.drawText(text, 100, 100, textPaint);
super.draw(canvas);
}
}xml
<com.shadow.pets.DrawView
android:id="@+id/energyText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>这个视图覆盖了我的整个LinearLayout,而我无法控制它的绘制位置?如果我扩展TextView,我根本无法让它工作!
对不起,如果这让人困惑的话,我知道我一定是疯了,但我已经尝试了几个小时了,找不到让它更清晰的词语!
发布于 2013-06-26 00:24:46
我认为现在的问题是,您的DrawView并不“知道”它有多大,所以Wrap_content占用了所有可用的空间。您可以通过重写onMeasure()来修复这个问题。您可能希望按照文本的大小包装如下:
@Override
protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec){
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int padding = 10;
Rect r = new Rect();
Paint p = new Paint();;
p.setTextSize(28);
p.getTextBounds(text, 0, text.length(), r);
this.setMeasuredDimension(r.right - r.left + padding,
r.bottom - r.top + padding);
}“填充”是为笔画边框添加额外的大小。然后,在绘图类中,将drawTexts更改为如下所示:
int padding = 5;
int x = this.getLeft() + padding;
int y = this.getBottom() - padding;
canvas.drawText(text, x - 1, y - 1, strokePaint);
canvas.drawText(text, x - 1, y + 1, strokePaint);
canvas.drawText(text, x + 1, y - 1, strokePaint);
canvas.drawText(text, x + 1, y + 1, strokePaint);
canvas.drawText(text, x, y, textPaint);从理论上讲,这是可行的。当我尝试它时,它看起来很棒,只要DrawView是LinearLayout中最左边的视图,但是当有TextView和DrawView时,它被部分覆盖。我不知道为什么:(但我希望这能帮助你开始!)
https://stackoverflow.com/questions/17308331
复制相似问题