Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android BitmapDrawable setTileModeX does not work on TextView

I have a TextView and a bitmap that can be repeated only horizontally. I want to set the background of my textview and repeat it only on the X axis. After looking around I saw that you can only do that via code and not in XML. I created a BitmapDrawable using:,

BitmapDrawable bg = new BitmapDrawable(r, BitmapFactory.decodeResource(r, R.drawable.my_drawable));
bg.setTileModeX(Shader.TileMode.REPEAT);
setBackgroundDrawable(bg);

However, even with this way the drawable is also repeated on the Y axis. This is in Honeycomb 3.2.

Can someone shed some light on this, perhaps provide an example of it working?

like image 870
dnkoutso Avatar asked May 23 '12 20:05

dnkoutso


1 Answers

//try this

BitmapDrawable bg = new BitmapDrawable(r, BitmapFactory.decodeResource(r,R.drawable.my_drawable));

        int width = view.getWidth();
        int intrinsicHeight = bd.getIntrinsicHeight();
        Rect bounds = new Rect(0,0,width,intrinsicHeight);
       bg.setTileModeX(Shader.TileMode.REPEAT);
        bg.setBounds(bounds);
        Bitmap bitmap = Bitmap.createBitmap(bounds.width(), bounds.height(), bg.getBitmap().getConfig());
        Canvas canvas = new Canvas(bitmap);
        bg.draw(canvas);
        BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
yourTxtView.setBackgroundDrawable(bg);

// try this too

bg.setTileModeX(1); //Repeats the bitmap in both direction.
bg.setTileModeY(-1);//Do not tile the bitmap. This is the default value.
like image 61
Padma Kumar Avatar answered Oct 14 '22 11:10

Padma Kumar