Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you repeat an image only vertically in android?

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!

like image 985
William L. Avatar asked Jul 22 '12 19:07

William L.


2 Answers

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);
like image 153
Kevin Coppock Avatar answered Oct 18 '22 22:10

Kevin Coppock


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;
    }
like image 1
Ali Ashraf Avatar answered Oct 18 '22 23:10

Ali Ashraf