1

写真でわかるように、次のことをしようとしていますが、その黒い四角の代わりに、白い四角が必要です。

ここに画像の説明を入力

これまでの私のコードは次のとおりです。

public class SmallWhiteThing extends View {

Context context;

Paint paint = new Paint();

// CONSTRUCTOR
public SmallWhiteThing(Context context) {
    super(context);
    setFocusable(true);
}

public SmallWhiteThing(Context context, AttributeSet attrs)
{
    super(context, attrs);
    this.context = context;
}

public SmallWhiteThing(Context context, AttributeSet attrs, int defStyle)
{
    super(context, attrs, defStyle);
    this.context = context;
}

@Override
protected void onDraw(Canvas canvas) {

    Paint paint = new Paint();
    BitmapFactory.Options options = new BitmapFactory.Options();  
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;

    Bitmap b = Bitmap.createBitmap(120, 120, Bitmap.Config.ALPHA_8);
    Canvas c = new Canvas(b);   
    c.drawColor(Color.WHITE);

    paint.setStrokeWidth(0);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    paint.setTextSize(40);
    paint.setAntiAlias(true);

    c.drawText("Hello", 30, 30, paint);

    canvas.drawBitmap(b, 140, 270, paint);
}

}

ご覧のとおり、試してみましたが c.drawColor(Color.WHITE); 、運がありませんでした。

ヒントは本当にありがたいです。

私は何か他のことを試みています、そして私はこれを得ています: ここに画像の説明を入力

コード:

Bitmap b = Bitmap.createBitmap(120, 120, Bitmap.Config.ALPHA_8);

    Canvas c = new Canvas(b);   
    c.drawColor(Color.WHITE);
    paint.setColor(Color.WHITE);
    paint.setStrokeWidth(0f);
    c.drawRect(0, 0, 150, 150, paint);

    canvas.drawBitmap(b, 100, 100, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
    paint.setTextSize(40);
    paint.setAntiAlias(true);
    paint.setColor(Color.WHITE);
    canvas.drawText("Helloo", 100, 200, paint);
4

1 に答える 1

3

それを必要とする人のために。これは機能しています:

現在の使用状況 (画面のイメージビューを埋めます):

//Params: Text, textSize
createBlabla("Text to show", 35);


public void createBlabla(String text, int fontSize){        
    int paddingRight = 10;
    int paddingLeft = 5;
    int paddingBottom = 5;

    //Paint config
    Paint paint = new Paint();
    paint.setTextSize(fontSize);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));
    paint.setAntiAlias(true);

    Bitmap largeWhiteBitmap = Bitmap.createBitmap((int) paint.measureText(text) + paddingRight, fontSize + paddingRight, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(largeWhiteBitmap);
    canvas.drawColor(Color.WHITE); 

    canvas.drawText(text, paddingLeft, fontSize, paint);

    ImageView imv = (ImageView)MainActivity.this.findViewById(R.id.imageView1);
    imv.setImageBitmap(largeWhiteBitmap);
}

ここに画像の説明を入力

画面に表示されるのは、中央に imageView がある相対的なレイアウト全体であることに注意してください。このイメージビューは、前のコードでイメージとしてビットマップを取得します。relativeLayout の背景は緑色です。

于 2013-11-11T21:24:16.497 に答える