【问题标题】:how to shrink an image in size in android layout?如何在android布局中缩小图像的大小?
【发布时间】:2013-12-28 15:41:33
【问题描述】:

我有一张想要在 android 布局中显示的图片

但我想让它按比例缩小到原来的大小。

我该怎么做?

如果我指定一些宽度和高度,它会裁剪图像而不是缩小它

【问题讨论】:

    标签: android image android-layout layout imageview


    【解决方案1】:

    使用 scaleType 属性来定义 ImageView 的缩放,例如:

    android:scaleType="fitCenter"
    

    http://developer.android.com/reference/android/widget/ImageView.ScaleType.html

    【讨论】:

      【解决方案2】:

      您可以使用以下代码示例将图像缩放到 80% 大小。

      int srcWidth = srcBitmap.getWidth();
      int srcHeight = srcBitmap.getHeight();
      int dstWidth = (int)(srcWidth*0.8f);
      int dstHeight = (int)(srcHeight*0.8f);
      Bitmap dstBitmap = Bitmap.createScaledBitmap(srcBitmap, dstWidth, dstHeight, true);
      

      根据您需要缩放的百分比更改倍数。

      此外,请参阅此链接了解更多选项。 Scaling bitmap to 80 percent

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-13
        • 1970-01-01
        • 1970-01-01
        • 2022-09-28
        相关资源
        最近更新 更多