2010-07-21 94 views
2

给定一张图像,我想只能缩放该图像的一部分。说,我想扩大一半的图像,这样就占了整个空间的一半。Android:调整图像大小并缩放一部分

这怎么可能?

将ImageView fitXY工作,因为我认为它只适用于整个原始图像。

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout linearLayout = new LinearLayout(this); 

      Bitmap bitmap = BitmapFactory.decodeResource(getResources(),  R.drawable.icon); 


      int width = bitmap.getWidth(); 

      int height = bitmap.getHeight(); 

      int newWidth = 640; 

      int newHeight = 480; 


      float scaleWidth = ((float) newWidth)/width; 

      float scaleHeight = ((float) newHeight)/height; 


      Matrix matrix = new Matrix(); 

      matrix.postScale(scaleWidth, scaleHeight); 

      // create the new Bitmap object 

      Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 50, 50, width, 

        height, matrix, true); 

      BitmapDrawable bmd = new BitmapDrawable(resizedBitmap); 



      ImageView imageView = new ImageView(this); 

      imageView.setImageDrawable(bmd); 

      imageView.setScaleType(ScaleType.CENTER); 



      linearLayout.addView(imageView, new LinearLayout.LayoutParams( 

        LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 

      setContentView(linearLayout); 
    } 
} 

这只有在createBitmap,X和源中的第一像素的y坐标是0的意思,我不能够采取的形象的一个子集。只能够缩放整个图像。但createBitmap是为了图像的子集。

在日志中,当参数不为0,我得到以下异常:java.lang.IllegalArgumentException异常:X +宽度必须< = bitmap.width()

请帮

回答

1

所以我不得不修复一些错别字,但这个例子对我来说做得很好。 http://www.anddev.org/resize_and_rotate_image_-_example-t621.html 错别字:

int width = bitmapOrg.width(); 
int height = bitmapOrg.height(); 

成为:

int width = bitmapOrg.getWidth(); 
int height = bitmapOrg.getHeight(); 

否则,工作时,我尝试了agains SDK 7

2

首先,你必须创建出一种新的位图,你想用规模

createBitmap() //pass the source bitmap, req height and width 

现在从结果位图中,你必须创建一个使用

createScaledbitmap() //pass the result bitmap , req width, height 

对于exapmle您缩放位图:

Bitmap originalBitmap = BitmapFactory.decodeResource(res, id); 
Bitmap partImage = originalBitmap.createBitmap(width, height, config); 
Bitmap scaledImage = partImage.createScaledBitmap(partImage, dstWidth, dstHeight, filter); 
相关问题