开发者

Android - stretch image partially

开发者 https://www.devze.com 2023-04-11 18:43 出处:网络
Is there any way to stretch image partially in android? Something like this 开发者_JAVA百科 Here, image is stretched in the 1/3 middle section, but left and right endings remains \"as is\".What you\

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

Android - stretch image partially

开发者_JAVA百科

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


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


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


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号