Android - 部分拉伸图像

时间:2011-10-10 12:39:57

标签: android image

有没有办法在android中部分拉伸图像?像这样的东西

enter image description here

此处,图像在1/3中间部分拉伸,但左右两端保持“原样”。

3 个答案:

答案 0 :(得分:3)

你正在做的是内置于android中,实际上。将该图像转换为9补丁。

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

答案 1 :(得分:1)

您可以使用Draw 9-patch将图像准备为NinePatch,然后使用NinePatch class

加载它

答案 2 :(得分:0)

也可以在代码中完成:

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