2011-09-16 6 views
7

Ho un'immagine che sto usando come sfondo per un RelativeLayout. L'immagine deve essere affiancata orizzontalmente per creare un motivo.Riesci a disegnare bitmap in x ma allungare in y

sono in grado di ottenere l'immagine per affiancare orizzontalmente utilizzando questo codice:

BitmapDrawable b3 = (BitmapDrawable)getResources().getDrawable(R.drawable.background); 

b3.setTileModeX(Shader.TileMode.REPEAT); 

v.findViewById(R.id.layout).setBackgroundDrawable(b3); 

Il problema è che l'immagine piastrelle anche in verticale. Sembra di affiancare la modalità "clamp" in verticale, ma la modalità "repeat" in orizzontale. Ecco uno screenshot:

TileMode

Come si può vedere, l'immagine è solo un po 'più piccolo lo spazio che occupa, e il bordo inferiore è "bloccato".

Come è possibile impostare l'immagine per allungare verticalmente ma affiancarla orizzontalmente?

risposta

5

Questo metodo richiama la creazione di nuova bitmap, ma sembra che sia acrhiving vostro obiettivo

 View view = findViewById(R.id.layout); 
     BitmapDrawable bd = (BitmapDrawable) getResources().getDrawable(R.drawable.tile); 
     int width = view.getWidth(); 
     int intrinsicHeight = bd.getIntrinsicHeight(); 
     Rect bounds = new Rect(0,0,width,intrinsicHeight); 
     bd.setTileModeX(TileMode.REPEAT); 
     bd.setBounds(bounds); 
     Bitmap bitmap = Bitmap.createBitmap(bounds.width(), bounds.height(), bd.getBitmap().getConfig()); 
     Canvas canvas = new Canvas(bitmap); 
     bd.draw(canvas); 
     BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap); 
     view.setBackgroundDrawable(bitmapDrawable); 

Si prega di notare che funziona solo se vista è stato già layouted, quindi un metodo Lile onWindowFocusChanged è un buon posto per questo codice .

+0

È possibile farlo solo combinando i file XML di Drawable? – neworld

0

Avere lo stesso problema io stesso. Provato con l'immagine a 9patch ma sembra che tu non possa usare le immagini a 9patch insieme alla piastrellatura, il 9patch allunga l'immagine indipendentemente da ciò che definisci nella proprietà di piastrellatura. Alla fine quello che farò è chiedere al creatore dell'immagine di allungarlo verticalmente o farlo da solo. Mi piacerebbe una soluzione migliore se qualcuno la trova.

2

sento che è semplice: (Questo codice di piastrelle in Y e ripetere in x)

Nella tua onWindowFoucsChanged si chiama:

public void onWindowFocusChanged(boolean hasFocus) { 
     // TODO Auto-generated method stub 
     super.onWindowFocusChanged(hasFocus); 
     Drawable d = getRepeatingBG(this, R.drawable.image_that_you_want_to_repeat); 
     body_view.setBackgroundDrawable(d); 

    } 

private Drawable getRepeatingBG(Activity activity, int center) 
    { 

     DisplayMetrics dm = new DisplayMetrics(); 
     activity.getWindowManager().getDefaultDisplay().getMetrics(dm); 

     BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inScaled=true; 

     Bitmap center_bmp = BitmapFactory.decodeResource(activity.getResources(), center, options); 
     center_bmp.setDensity(Bitmap.DENSITY_NONE); 
     center_bmp=Bitmap.createScaledBitmap(center_bmp, dm.widthPixels , center_bmp.getHeight(), true); 

     BitmapDrawable center_drawable = new BitmapDrawable(activity.getResources(),center_bmp); 
//change here setTileModeY to setTileModeX if you want to repear in X 
     center_drawable.setTileModeY(Shader.TileMode.REPEAT); 

     return center_drawable; 
    } 
1

Troppo tardi per voi, ma può essere utile per gli altri.

Per eseguire questa operazione è possibile creare una vista personalizzata. È sufficiente ridimensionare la bitmap di origine fino alla vista e quindi disegnarla ripetutamente nell'area di disegno:

public class RepeatingXImageView extends View { 

Bitmap bitmap; 
Paint paint; 

public RepeatingXImageView(Context context) { 
    super(context); 
} 

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

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

@Override 
protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 
    super.onLayout(changed, left, top, right, bottom); 
    if(changed) { 
     paint = new Paint(); 
     bitmap = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.seekbar_overlay); 
     bitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(), bottom - top, false); 
    } 
} 

@Override 
protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 
    if(bitmap == null) return; 
    int left = 0; 
    while(left < getWidth()) { 
     canvas.drawBitmap(bitmap, left, 0, paint); 
     left += bitmap.getWidth(); 
    } 
} 
} 
Problemi correlati