Textview在画布的中心

时间:2016-04-16 10:57:56

标签: android android-canvas textview

我需要创建一个画布,我在其中添加两个图像作为BITMAP,并在右下角添加一个带有多行的TEXTVIEW。

这是我试过的代码 -

drawnBitmap = Bitmap.createBitmap(1000, 1500, Bitmap.Config.RGB_565);

    Canvas canvas = new Canvas(drawnBitmap);

    canvas.drawColor(getResources().getColor(R.color.creamColor));
    // JUST CHANGE TO DIFFERENT Bitmaps and coordinates .

    canvas.drawBitmap(b, 0, 0, null);
    canvas.drawBitmap(b2, 630, 250, null);
    //for more images :
    // canvas.drawBitmap(b3, 0, 0, null);
    // canvas.drawBitmap(b4, 0, 0, null);

      /*Draw text*/
    //TextPaint paintText=new TextPaint(TextPaint.ANTI_ALIAS_FLAG);
    Paint paintText = new Paint(Paint.ANTI_ALIAS_FLAG);
    paintText.setColor(Color.BLACK);
    paintText.setTextAlign(Paint.Align.CENTER);
    paintText.setTextSize(50);
    Typeface tf;
    tf = Typeface.createFromAsset(getAssets(), "HelveticaNeueLTStd-Lt.otf");
    paintText.setTypeface(tf);

    RelativeLayout layout = new RelativeLayout(this);

    layout.setBackgroundColor(Color.GREEN);
    layout.setDrawingCacheEnabled(true);


    TextView textView = new TextView(this);
    textView.setVisibility(View.VISIBLE);
    textView.setText(INNERTEXT);
    textView.setTextSize(30);
    textView.setWidth(300);

    textView.setTextColor(Color.BLACK);
    textView.setTypeface(tf);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.MATCH_PARENT);
    params.addRule(RelativeLayout.CENTER_IN_PARENT);

    layout.setLayoutParams(params);

    textView.setGravity(Gravity.CENTER);
    layout.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
    layout.addView(textView);


    layout.measure(canvas.getWidth(), canvas.getHeight());
    layout.layout(0, 0, canvas.getWidth(), canvas.getHeight());

    // To place the text view somewhere specific:
    // canvas.translate(500, 750);

    canvas.drawBitmap(layout.getDrawingCache(), 500, 750, null);
    // layout.draw(canvas);

我将画布划分为4个相等的部分,但4.部分内的textview不居中。

0 个答案:

没有答案
相关问题