Is there a way to repeat an image only vertically in android? I've tried it like this:
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/asphalt_texture"
android:tileMode="repeat"
android:dither="true"/>
But it repeats both ways and I only want it repeated vertically, any ideas would be appreciated!
Strangely enough, there does not seem to be a way to do this in XML. Through code, however, you can. The following methods:
BitmapDrawable.setTileModeX(Shader.TileMode mode)
BitmapDrawable.setTileModeY(Shader.TileMode mode)
should do what you need. Simply pass in one of the Shader.TileMode
enumerations (REPEAT
, MIRROR
, CLAMP
) for whichever axis (y-axis for vertical) you need the repeat effect on.
So you should be able to do something like this:
BitmapDrawable draw = (BitmapDrawable)getResources().getDrawable(R.drawable.draw);
draw.setTileModeY(Shader.TileMode.REPEAT);
I feel it is straight forward:(This code will tile in Y and repeat in x)
In your onWindowFoucsChanged you call:
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With