0

Is there a way to process an image / drawable in Android (Java) to make the last X pixels of the image stretch or repeat to the bottom of the screen. I would like to know if this is possible to achieve and if there is a name for this effect or existing algorithm. Thank you in advance. Just please note that I am aware of 9patch images and it is not the way I want to go with, I explicitly need to apply this in code. Thank you in advance, bellow is an image

This is the effect I want to achieve

Speed Demon
  • 691
  • 1
  • 9
  • 21

3 Answers3

1

The best option to achieve that is using a Draw 9-patch image

Tutorials :

Android 9 Patch Image Tutorial

Draw9patch Tutorial [HD]

Jorgesys
  • 124,308
  • 23
  • 334
  • 268
1

Possible algorithm:

1) get Bitmap object of your image

2) read image pixels(bitmap.getPixels(...) method) into an int[] array

3) create a blank bitmap of desired height

4) use setPixels() method to copy initial bitmap data to a new one

5) use same method to copy last line to new bitmap as many times as you want(use offset parameter wisely)

This sounds too complicated for such a small task, but you asked for a way of acheiving it programmatically, so here you go

EDIT Easier way for your situation:

Make use of Shader.TileMode.CLAMP

1) Create BitmapDrawable from your bitmap or from resources

2) Use setTileModeY(TilMode.CLAMP);

Sam
  • 1,652
  • 17
  • 25
  • A way to go for me. This is the kind of algorithm i am looking for. I may be experimenting with it, but if you are aware of some tutorials/links they are more than welcome. – Speed Demon Feb 10 '14 at 19:48
  • check out another method – Sam Feb 10 '14 at 19:58
0

You can take a strip from the image and use repeat like this

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/your_pattern"
    android:tileMode="repeat" />

EDIT : If you wanted programmatically

1- crop from the image using this code from cut the portion of bitmap :

private Bitmap cropBitmap1()
{
    Bitmap bmp2 = BitmapFactory.decodeResource(this.getResources(), R.drawable.image1); 
    Bitmap bmOverlay = Bitmap.createBitmap(bmp2.getWidth(), 1, Bitmap.Config.ARGB_8888);

    Paint p = new Paint();
    p.setXfermode(new PorterDuffXfermode(Mode.CLEAR));              
    Canvas c = new Canvas(bmOverlay); 
    c.drawBitmap(bmp2, 0, 0, null); 
    c.drawRect(30, 30, 100, 100, p);

    return bmOverlay;
}

2- The returned bitmap , convert it to bitmapdrawable:

BitmapDrawable navigationBackground =new BitmapDrawable(getResources(),bitmap);
navigationBackground.setTileModeX(Shader.TileMode.REPEAT);
Community
  • 1
  • 1
youssefhassan
  • 1,067
  • 2
  • 11
  • 17
  • I thought of this solution but that would be my last go, I'd like to know if I am able to process the image programmatically – Speed Demon Feb 10 '14 at 19:37
  • this option require more process than using a Draw 9-patch image. – Jorgesys Feb 10 '14 at 19:57
  • @Elenasys He said he does not want to use ninepatch in the question "am aware of 9patch images and it is not the way I want to go with". – youssefhassan Feb 10 '14 at 19:58
  • Maybe it's not professional to say this, but thank you very much man, I love you, it worked :) – Speed Demon Feb 10 '14 at 20:37
  • Do you know how can I adjust the parameters to get the bottom 1 px portion without rotating the bitmap ? This works but it extracts it from the top. I guess I need to modify this c.drawRect(30, 30, 100, 100, p); – Speed Demon Feb 10 '14 at 22:00