2010-09-07 10 views
7

Je veux utiliser un bitmap que tuile sur l'horizontal seulement, est-il possible de le faire, de sorte qu'il ne se développe que sur X comme répéter x sur CSS. Je en utilisant le code suivant, mais la tuile bitmap horizontale et verticale:Tuile bitmap Android sur X seulement

<?xml version="1.0" encoding="utf-8"?> 
<bitmap xmlns:android="http://schemas.android.com/apk/res/android" 
    android:src="@drawable/pinstripe" 
    android:tileMode="repeat" 
    android:gravity ="top|fill_horizontal" 
    android:dither="true"/> 
+0

Avez-vous déjà trouver une solution? – William

Répondre

10

Voici la solution. Je voulais créer BitmapDrawable en code et spécifier uniquement le mode X tile Android Tile Bitmap

+6

+1 Fedor: Actuellement, il n'existe aucun moyen de spécifier des modes de tuiles X et Y indépendants en XML. – Buzzy

0

Je voulais aussi répéter le fond verticalement, mais je n'ai pas trouvé de solution décente, alors j'ai écrit ceci, j'espère que cela peut aider quelqu'un là-bas *

/** 
* Created by LeoLink on 2014-06-24. 
*/ 
public class VerticallyRepeatedBackgroundLinearLayout extends LinearLayout { 
    private int width, height; 
    private RectF mRect; 

    public VerticallyRepeatedBackgroundLinearLayout(Context context) { 
     super(context); 
     init(context); 
    } 

    public VerticallyRepeatedBackgroundLinearLayout(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(context); 
    } 

    public VerticallyRepeatedBackgroundLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
     init(context); 
    } 

    private void init(Context context) { 
     setWillNotDraw(false); 
     mRect = new RectF(); 
    } 

    @Override 
    protected void onSizeChanged(int w, int h, int oldw, int oldh) { 
     super.onSizeChanged(w, h, oldw, oldh); 
     if (oldw == 0 && oldh == 0 && w > 0 && h > 0) { 
      width = w; 
      height = h; 
     } 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 
     // clear the canvas 
     canvas.drawColor(Color.WHITE); 
     // draw 
     Drawable bg = getBackground(); 
     Bitmap bm = ((BitmapDrawable)bg).getBitmap(); 
     int w = bg.getIntrinsicWidth(); 
     int h = bg.getIntrinsicHeight(); 
     int drawWidth = width; 
     int drawHeight = drawWidth * h/w; 
     int bottom = 0; 
     while (height > bottom) { 
      mRect.set(0, bottom, drawWidth, bottom + drawHeight); 
      canvas.drawBitmap(bm, null, mRect, null); 
      bottom += drawHeight; 
     } 
    } 
} 

et dans votre XML

<com.blah.blah.VerticallyRepeatedBackgroundLinearLayout 
    android:id="@+id/collection_container" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/your_tile_bg"> 
</com.blah.blah.VerticallyRepeatedBackgroundLinearLayout> 
Questions connexes