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

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

然而,即使使用这种方式,也可以在y轴上重复拉伸。这是蜂巢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.

07-24 09:49
查看更多