コードを実行したところ、すぐにLint
エラーが発生しました。init()
あなたはあなたをに設定しmTextPaint
ていますandroid.R.color.black
。int
これは静的な値であるため、その変数の実際の値はであることがすぐにわかりました。0x0106000c
これはほぼ完全に透過的です。getResources().getColor(android.R.color.black)
またはプレーンol'を使用する必要がありますColor.BLACK
。
textSize
12のaは非常に小さいことに注意してください。このコードは12を示しています(非常に小さいですが)。
public class MyImageView extends ImageView {
public MyImageView(Context context, AttributeSet attributeSet, int defStyle) {
super(context, attributeSet, defStyle);
init();
}
public MyImageView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
init();
}
public MyImageView(Context context) {
super(context);
init();
}
Paint mTextPaint;
private void init() {
mTextPaint = new Paint();
mTextPaint.setColor(Color.BLACK);
mTextPaint.setTextSize(12);
mTextPaint.setTextAlign(Paint.Align.LEFT);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int imgWidth = getMeasuredWidth();
int imgHeight = getMeasuredHeight();
float txtWidth = mTextPaint.measureText("your text");
int x = Math.round(imgWidth/2 - txtWidth/2);
int y = imgHeight/2 - 6;
canvas.drawText("12", x, y, mTextPaint);
}
}
xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.mytest.MyImageView
android:layout_width="100dp"
android:layout_height="100dp"/>
</RelativeLayout>
コピー/貼り付け、問題が解決しない場合は、ログ記録を開始します。繰り返しますが、このコードは機能12
します。画面に表示されます。