相关疑难解决方法(0)

Android:如何在保持宽高比的同时将图像拉伸到屏幕宽度?

我想下载一个图像(大小未知,但总是大致正方形)并显示它以使其水平填充屏幕,并垂直拉伸以保持图像的宽高比,在任何屏幕尺寸上.这是我的(非工作)代码.它水平拉伸图像,但不垂直,因此被压扁...

ImageView mainImageView = new ImageView(context);
    mainImageView.setImageBitmap(mainImage); //downloaded from server
    mainImageView.setScaleType(ScaleType.FIT_XY);
    //mainImageView.setAdjustViewBounds(true); 
    //with this line enabled, just scales image down
    addView(mainImageView,new LinearLayout.LayoutParams( 
            LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
Run Code Online (Sandbox Code Playgroud)

java layout android

118
推荐指数
9
解决办法
13万
查看次数

防止背景图像拉伸视图

每当我将背景分配给使用wrap_content布局的视图时,如果背景图像大于视图,则视图会被拉伸,以便它可以保持整个背景.为什么会如此以及如何预防呢?即使图像是9-patch并且标记了可伸展区域,也会出现这种情况 - 为什么图像缩小到视图的大小?

android

24
推荐指数
2
解决办法
2万
查看次数

imageView没有拉伸以适应屏幕

我有一个drawable,需要作为背景.它需要拉伸才能填满屏幕(忽略纵横比).

我为ldpi,mdpi,hdpi和xhdpi提供了合适的大小.(我知道它们与相应的宽度和高度的比例是合适的,这还够吗?)

我试过这样做如下:

在imageView上使用scaleType:fitXY

这确实在imageView周围(内部?)留下了白色边距.

fitXY图像

我不满意的解决方法是使用scaleType中心,如下所示:

我不满意,因为平板电脑屏幕上仍然存在这个问题. 在此输入图像描述

注意:

  • 正如您可能已经注意到的那样,我已经在SO上的其他位置尝试了建议的修复,例如将Adjust View Boundaries设置为true.他们在这里没有帮助.:(
  • 我想要一个非程序化的答案/修复,就像在XML本身中这样做.我真的不想写代码来设置背景图像; 生活应该比这简单得多.(:P)

更新1:

这是XML布局

<merge xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" -->
    <!-- xmlns:tools="http://schemas.android.com/tools" -->
    <!-- android:id="@+id/FrameLayout1" -->
    <!-- android:layout_width="fill_parent" -->
    <!-- android:layout_height="fill_parent" -->
    <!-- tools:context=".SplashScreen" > -->


    <!--
         This FrameLayout insets its children based on system windows using
         android:fitsSystemWindows
    -->

    <View
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:onClick="onClickTapToContinue"
         >
    </View>

    <ImageView
        android:id="@+id/bg_gradient"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="fill"
        android:adjustViewBounds="true"
        android:contentDescription="@string/this_is_the_background_image"
        android:scaleType="center"
        android:src="@drawable/bg_gradient" />

    <ImageView
        android:id="@+id/bg_artwork"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:adjustViewBounds="true"
        android:contentDescription="@string/artwork_man"
        android:src="@drawable/bg_artwork" />

    <!-- </FrameLayout> -->

    <ImageView …
Run Code Online (Sandbox Code Playgroud)

android

5
推荐指数
1
解决办法
8769
查看次数

标签 统计

android ×3

java ×1

layout ×1