Search code examples
androidcanvastextviewgravitydrawingcache

TextViews Gravity does not work vertically


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


Solution

  • 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);
    
    
        }
    
    }