1

我需要缩放 imageView 的一部分(比如从 x 到 y),而不是整个图像。setscaleType() 函数恰好适用于整个图像而不是特定部分。请让我知道是否有办法在android中做到这一点。

提前致谢。

4

2 回答 2

5

您可以通过两种方式实现它。

  1. 创建图像的单独部分(photoshop)
  2. 创建新的位图(矩形)并将其绘制在新的 Canvas 上并使用它。

我更喜欢你选择 2 选项。

于 2012-06-04T13:16:51.447 回答
1

看一眼

public static Bitmap createBitmap(位图源,int x,int y,int width,int height,Matrix m,布尔过滤器)

自:API 级别 1

从源位图的子集中返回一个不可变的位图,由可选矩阵转换。它以与原始位图相同的密度进行初始化。

参数

    source  The bitmap we are subsetting
    x   The x coordinate of the first pixel in source
    y   The y coordinate of the first pixel in source
    width   The number of pixels in each row
    height  The number of rows
    m   Optional matrix to be applied to the pixels
    filter  true if the source should be filtered. Only applies if the matrix contains more than just translation.

退货

A bitmap that represents the specified subset of source

您还可以裁剪 image-in-android 的特定部分,然后使用 createBitmap 格式化新裁剪的图像。


使用 createBitmap 的示例:

public class bitmaptest extends Activity {
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        LinearLayout linLayout = new LinearLayout(this);

        // load the origial BitMap (500 x 500 px)
        Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
               R.drawable.ic_launcher);

        int width = bitmapOrg.getWidth();
        int height = bitmapOrg.getHeight();
        int newWidth = 200;
        int newHeight = 200;

        // calculate the scale - in this case = 0.4f
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;

        // createa matrix for the manipulation
        Matrix matrix = new Matrix();
        // resize the bit map
        matrix.postScale(scaleWidth, scaleHeight);
        // rotate the Bitmap
        matrix.postRotate(45);

        // recreate the new Bitmap
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
                          width, height, matrix, true);

        // make a Drawable from Bitmap to allow to set the BitMap
        // to the ImageView, ImageButton or what ever
        BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);

        ImageView imageView = new ImageView(this);

        // set the Drawable on the ImageView
        imageView.setImageDrawable(bmd);

        // center the Image
        imageView.setScaleType(ScaleType.CENTER);

        // add ImageView to the Layout
        linLayout.addView(imageView,
                new LinearLayout.LayoutParams(
                      LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT
                )
        );

        // set LinearLayout as ContentView
        setContentView(linLayout);
    }
}
于 2012-06-04T21:36:01.713 回答