【问题标题】:How to scale bitmapdrawable (background) and keeping the aspect ratio till bigger than device's width and height如何缩放 bitmapdrawable(背景)并保持纵横比大于设备的宽度和高度
【发布时间】:2014-08-09 17:21:20
【问题描述】:

我已将图库中的图像解码为位图,并且我正在使用模糊类来模糊位图。 (Error when trying to blur image using RenderScript support library on android phone)

我想从模糊的位图创建一个背景,但它被这段代码拉伸了(rl 是我的 RelativeLayout):

  Uri selectedImageUri = data.getData();
          Blur blur = new Blur();
          imagepath = getRealPathFromURI(selectedImageUri);

          BitmapFactory.Options options = new BitmapFactory.Options();
          options.inJustDecodeBounds = true;
          BitmapFactory.decodeFile(imagepath, options);
          int pow = 0;
          while (options.outHeight >> pow > options.outHeight / 2 || options.outWidth >> pow > options.outWidth / 2) {
              pow += 1;
          options.inSampleSize = 1 << pow; 
          options.inJustDecodeBounds = false;
          }
         Bitmap bmImg = BitmapFactory.decodeFile(imagepath, options);
         Bitmap BlurredIMG = Blur.doBlur(bmImg, 33, false);
         BitmapDrawable bmp = new BitmapDrawable(BlurredIMG);
         rl.setBackground(bmp);

如何保持位图的纵横比并对其进行缩放以覆盖设备的宽度和高度? 我需要改用 ImageView 吗?怎么样?

【问题讨论】:

    标签: android bitmap background drawable


    【解决方案1】:

    设置RelativeLayout 背景资源可能不是要走的路。相反,您可能希望拥有一个填充父级并使用缩放的 ImageView。在某些听起来像你的情况下,这对我有用。

    看看这个:

    Fit a ImageView (and its src) to the layout width and make its height proportional

    这与您的问题相似,但不妨试试这个:

    scale the size of downloaded image to fit the imageview

    关键是您需要一个填充背景的 ImageView,然后在其上放置其他元素。您的布局背景的标准选项不足以满足您的需求。

    【讨论】:

    【解决方案2】:

    我找到了其他问题的答案 (Android: Scale a Drawable or background image?),我使用 centerCrop 作为比例类型。

        <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    
        <ImageView
            android:id="@+id/BG"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:scaleType="centerCrop" />
    
    </FrameLayout>
    

    没想到答案会这么简单!

    【讨论】:

      猜你喜欢
      • 2016-05-18
      • 2016-04-01
      • 2014-07-01
      • 2015-01-21
      • 2015-07-31
      • 1970-01-01
      • 2017-09-16
      • 1970-01-01
      • 2021-06-23
      相关资源
      最近更新 更多