Android BitmapDrawable setTileModeX 在 TextView 上不起作用



我有一个只能水平重复的TextView和一个位图。我想设置文本视图的背景并仅在 X 轴上重复它。环顾四周后,我发现您只能通过代码而不是XML来做到这一点。我创建了一个BitmapDrawable使用:,

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

但是,即使采用这种方式,可绘制对象也会在 Y 轴上重复。这是在Honeycomb 3.2中。

有人可以对此有所了解,也许提供一个例子来说明它的工作原理吗?

//试试这个

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

也试试这个

bg.setTileModeX(1); //Repeats the bitmap in both direction.
bg.setTileModeY(-1);//Do not tile the bitmap. This is the default value.

最新更新