我的TextViews xml文件
<com.example.blabla.HateTextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/hate"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Text" />
下一个代码
public class HateTextView extends android.support.v7.widget.AppCompatTextView {
Paint mTxtPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private String str = " ";
HateTextView(Context context, AttributeSet attrs){
super(context, attrs);
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.HateTextView);
a.recycle();
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint.FontMetrics fm = new Paint.FontMetrics();
mTxtPaint.setColor(Color.parseColor("#d00000"));
mTxtPaint.setTextSize(20 * getResources().getDisplayMetrics().density);
mTxtPaint.getFontMetrics(fm);
mTxtPaint.isAntiAlias();
int margin = 5;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
canvas.drawRoundRect(90 - margin, 100 + fm.top - margin,
115 + mTxtPaint.measureText(str) + margin, 100 + fm.bottom + margin,35,35, mTxtPaint);
}
mTxtPaint.setColor(Color.WHITE);
canvas.drawText(str, 100, 100, mTxtPaint);
}
public void setTextHate(String hate) {
str = hate;
}
}
我创造了这样的东西:
HateTextView h = itemView.findViewById(R.id.hate);
h.setTextHate("some text..");
h.setRotation(number);
只是因为轮换这么多问题。或者更确切地说是因为抗疟疾。简单地从xml getPaint.AntiAlia ..起作用。
我在这里放了&#34; match_parent&#34;,如果我把&#34;内容&#34;我所画的文字根本不可见。如何插入&#34; Text&#34;而是我的文字,我画的。
答案 0 :(得分:0)
public void setTextHate(String hate) {
str = hate;
}
设置str = hate后,您必须致电invalidate();
再次致电onDraw(Canvas canvas)
。