Android:调整图像一部分的大小和缩放

发布于 2024-09-10 13:46:25 字数 1678 浏览 5 评论 0原文

给定一个图像,我希望能够仅缩放该图像的一部分。比如说,我想扩大图像的一半,以便该一半占据整个空间。

这怎么可能?

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 + width must be <= bitmap.width()

Please help

Given an image, I want to be able to scale only a part of that image. Say, I want to expand half of the image so that, that half takes up the whole space.

How is this possible?

Will ImageView fitXY work because I thought it'll work only for the whole original image.

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

This works only if in createBitmap, the X and Y coordinates of the first pixels in the source are 0. meaning, I'm not able to take a subset of the image. Only able to scale the whole image. But createBitmap is meant to subset an image.

In the log, when the parameters are not 0, I get the following exception: java.lang.IllegalArgumentException: x + width must be <= bitmap.width()

Please help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陈独秀 2024-09-17 13:46:25

首先,您必须使用要缩放的部分创建一个新位图,

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

现在从结果位图中,您必须使用以下命令创建缩放后的位图

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

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

First you have to create a new bitmap with the part you want to scale using

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

Now from the result bitmap, you have to create your scaled bitmap using

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

For exapmle:

Bitmap originalBitmap = BitmapFactory.decodeResource(res, id);
Bitmap partImage = originalBitmap.createBitmap(width, height, config);
Bitmap scaledImage = partImage.createScaledBitmap(partImage, dstWidth, dstHeight, filter);
抚笙 2024-09-17 13:46:25

因此,我必须修复几个拼写错误,但这个示例对我来说效果很好。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();

否则,当我再次尝试 SDK 7 时有效

So I had to fix a couple of Typos, but this example did a good job for me.http://www.anddev.org/resize_and_rotate_image_-_example-t621.html
typos:

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

become:

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

otherwise, worked when I tried it agains SDK 7

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文