0

when i draw the DrawingCache of a TextView to another View's Canvas, the Gravity of the TextView has no effect in vertical direction.

Here the class drawing the TextViews canvas to own canvas:

public class GravityDecorator extends View{
    private View view;
    private Paint paint= new Paint();

    public GravityDecorator(View view,Context context) {
        super(context);
        this.view = view;
        view.setDrawingCacheEnabled(true);
        view.layout(0, 0,600,500);
        this.layout(0, 0,600,500);
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) { 
        super.onDraw(canvas);
        view.buildDrawingCache();       
        canvas.drawBitmap(view.getDrawingCache(), 0, 0, paint);     
        view.destroyDrawingCache();
    }

}

Here is the code to test it (onCreate):

    ViewGroup root = (ViewGroup) findViewById(R.id.root); // is a linear_layout - width and height is match_parent
    TextView tv = new TextView(getApplicationContext());
    tv.setText("Hello World!");
    tv.setTextSize(40.0f);      
    tv.setLayoutParams(new LinearLayout.LayoutParams(300,200));     
    tv.setTextColor(Color.WHITE);
    tv.setBackgroundColor(Color.parseColor("#3131c5"));
    tv.setGravity(Gravity.CENTER);

    GravityDecorator gd = new GravityDecorator(tv, getApplicationContext());
    root.addView(gd);

As you see, the Gravity of the TextViews content only takes effect in the horizontal direction.

What is the reason and how to work around this, if its a bug ?

Thank you

steffka
  • 5
  • 2

1 Answers1

1
root = (ViewGroup) findViewById(R.id.root); // is a linear_layout - width and height is match_parent
tv = new TextView(getApplicationContext());
tv.setText("Hello World!");
tv.setTextSize(40.0f);      
tv.setLayoutParams(new LinearLayout.LayoutParams(300,200));     
tv.setTextColor(Color.WHITE);
tv.setBackgroundColor(Color.parseColor("#3131c5"));
tv.setGravity(Gravity.CENTER);
tv.invalidate();
root.addView(tv);
GravityDecorator gd = new GravityDecorator(tv, getApplicationContext());
root.addView(gd);

Might be because the layout param is not getting set for the TextView Initially. Try to add the view to the Parent and then get the drawingCache.

public class GravityDecorator extends View{
    private View view;
    private Paint paint= new Paint();

    public GravityDecorator(View view,Context context) {
        super(context);
        this.view = view;
        view.setDrawingCacheEnabled(true);
        view.layout(0, 0,600,500);
        this.layout(0, 0,600,500);
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) { 
        super.onDraw(canvas);
        view.buildDrawingCache();      

        Bitmap bmp = view.getDrawingCache();

        canvas.drawBitmap(bmp, 0, 0, paint);     
        view.destroyDrawingCache();
        if(root.indexOfChild(tv) != -1)
            root.removeView(tv);


    }

}
Triode
  • 11,309
  • 2
  • 38
  • 48
  • This will draw the content of the TextView in center, but the Size is like "wrap_content" now. I want to implement a class, which decorates every view with a border (top, left, right, bottom - or rounded). So this class here is just example to demonstrate the behavior without all the other code. My class to decorate the views with a border works, including padding and margin - but the gravity not. – steffka Apr 10 '13 at 13:47
  • The bitmap you are getting is the exact same height what you are providing inside layout params tv.setLayoutParams(new LinearLayout.LayoutParams(300,200)); – Triode Apr 10 '13 at 13:51
  • oh, ok. i missed that. Thank you very much! – steffka Apr 11 '13 at 07:42
  • Is there another option instead of adding and removing the view ? – steffka Apr 11 '13 at 08:50