William L.
William L.

Reputation: 3886

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!

Upvotes: 6

Views: 2865

Answers (2)

Ali Ashraf
Ali Ashraf

Reputation: 1919

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;
    }

Upvotes: 1

Kevin Coppock
Kevin Coppock

Reputation: 134664

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);

Upvotes: 6

Related Questions