PVoLan
PVoLan

Reputation: 1040

Android - stretch image partially

Is there any way to stretch image partially in android? Something like this

enter image description here

Here, image is stretched in the 1/3 middle section, but left and right endings remains "as is".

Upvotes: 1

Views: 2745

Answers (3)

Lumis
Lumis

Reputation: 21639

That can be done in code too:

int w = myImage.getWidth();
int h =  myImage.getHeight();
int thirdWidht = w / 3;

//create a new blank image
Bitmap stretchImage = Bitmap.createBitmap(w + thirdWidth, h, Bitmap.Config.ARGB_8888 );

Canvas c = new Canvas(stretchImage);

//draw left bit
c.drawBitmap(normalImage, new Rect(0,0,thirdWidht,h), new Rect(0,0,thirdWidht,h), true);

//draw stretched middle bit
c.drawBitmap(normalImage, new Rect(thirdWidht,0,thirdWidht * 2, h), new Rect(thirdWidht,0,thirdWidht * 3,h), true);

//draw right bit
c.drawBitmap(normalImage, new Rect(thirdWidht * 2,0,w,h), new Rect(thirdWidht * 3,0,w + thirdWidht,h), true);

myImageView.setImageBitmap(stretchImage);

Upvotes: 0

yep
yep

Reputation: 1273

What you're doing is built into android, actually. Turn that image into a 9-patch.

http://developer.android.com/guide/developing/tools/draw9patch.html

Upvotes: 3

michael
michael

Reputation: 3290

You can prepare your image as NinePatch using Draw 9-patch and then loadt it with NinePatch class

Upvotes: 1

Related Questions