tilemode只在X中

时间:2011-07-26 14:30:38

标签: android xml bitmap drawable

我有一个xml文件:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/header"
android:tileMode="repeat" />

我有一个标题,我想在X中重复我的drawable并在Y中拉伸它。我怎么能在xml中这样做。

首先尝试: 我删除了我的xml,我在活动开始时已经完成了这个:

BitmapDrawable bp =(BitmapDrawable)getResources().getDrawable(R.drawable.header);
bp.setTileModeX(TileMode.REPEAT);
LinearLayout layout = (LinearLayout)findViewById(R.id.headerRedLayout);
layout.setBackgroundDrawable(bp);

效果很好,但我希望重复从布局的底部开始

1 个答案:

答案 0 :(得分:0)

在onDraw()方法中执行此操作

请记住R.drawable.bg_tile是你的形象

protected void onDraw (Canvas canvas) {
    bgTile = BitmapFactory.decodeResource(context.getResources(), R.drawable.bg_tile);

    float left = 0;
    float top = 0;
    float bgTileWidth = bgTile.getWidth();

    int screenWidth  = canvas.getWidth(); //Not sure of this
    int screenHeight = canvas.getHeight(); //Not sure of this too

    while (left < screenWidth) {
        canvas.drawBitmap(
               bgTile,
               null,
               new Rect(left,top,left+bgTileWidth,screenHeight),
               null
               );
        left += bgTileWidth;
    }
}