【问题标题】:Android Programming crop background imageAndroid 编程裁剪背景图片
【发布时间】:2016-01-20 20:25:42
【问题描述】:

我想在我的应用中显示一张图片作为背景。现在我使用了这个语句:android:background="@drawable/background" in <LineraLayout>。这是.xml 代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="overbit.user.MainActivity"
    android:background="@drawable/background">
</LinearLayout>

现在我得到了这个输出:

但我想要这样:

也许你们中的某个人可以帮助我。谢谢。

【问题讨论】:

    标签: java android xml


    【解决方案1】:

    BitmapDrawable,它允许这样做。首先你需要创建一个drawable资源如下(让它成为项目资源res/drawable/mybg.xml中的一个文件):

    <bitmap
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:src="@drawable/background"
        android:gravity="center" />
    

    然后在你的布局中将其指定为背景资源:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context="overbit.user.MainActivity"
        android:background="@drawable/mybg">
    </LinearLayout>
    

    【讨论】:

    • 如何以编程方式进行?即我想要来自服务器的图像并将其显示为裁剪......可以使用毕加索吗?
    • 当可绘制对象是矢量 (SVG) 时,这不起作用,它在运行时崩溃。你知道如何让它与矢量一起工作吗?
    • @Shirane85 根据文档,BitmapDrawable 仅与 pngjpeggif 格式兼容。您不能使 it 与矢量一起使用。我知道的支持svg 文件的唯一可绘制资源是VectorDrawable,但它没有类似的设置。如果您不想为此付出太多努力,我认为drWisdom's answer 应该是最合适的。
    • @TheDreamsWind 是的,这就是我最终要做的。谢谢:)
    【解决方案2】:

    您需要在 Photoshop 中更改图像本身的尺寸或其他东西以达到所需的结果(由于 android 智能手机的屏幕尺寸不同,这仍然非常困难)。一种解决方法是将您的父布局更改为 Relativelayout,然后使用图像视图来显示您的图片,如下所示:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ImageView
            android:layout_width="match_parent" //make sure its match parent
            android:layout_height="match_parent"// and this one also
            android:src="@drawable/yourBackgroundImage"
            android:scaleType="..."/>
        <LinearLayout>put your childviews here</LinearLayout>
    

    android:scaleType 有 4 到 5 个选项可用...试验它们,直到获得所需的结果。另请注意,您需要使用 android:src 而不是 imageview 的 android:background 属性

    【讨论】:

    • @MyNewName 我发现这是一种方便且简单的方法。 imageview 中的 scaleType 属性确实为您提供了有用的灵活性。添加一个额外的 ImageView 并不会真正让你的 Layout 负担过重(除非它已经很混乱了)。
    【解决方案3】:

    您可以使用BitmapRegionDecoder 完成您想做的事情。

    来自文档:

    BitmapRegionDecoder 可用于从 图片。 BitmapRegionDecoder 在原始 图片很大,你只需要图片的一部分。

    它允许您相当容易地解码特定位图的 Rect 区域,您唯一需要做的就是计算您的 Rect 区域以相对于位图进行解码。

    【讨论】:

      【解决方案4】:

      在 android 中,ImageView 的位置由图像的左上角决定。我假设 x 方向是起点宽度的 1/3。现在我们可以设置枢轴了。基本上,枢轴点的位置是视图将围绕其旋转和缩放的位置。

      int displacementX = ImageView.getWidth() / 3;
      int displacementY = 10;
      imgview.setPivotX((float)displacementX);
      imgview.setPivotY((float)displacementY);
      

      现在我们已经设置了轴心,我们可以使用比例类型来适应图像。

      imgview.setScaleType(ImageView.ScaleType.CENTER_CROP);
      

      【讨论】:

        猜你喜欢
        • 2016-01-19
        • 1970-01-01
        • 2012-12-02
        • 2013-12-03
        • 1970-01-01
        • 2014-03-14
        • 1970-01-01
        • 1970-01-01
        • 2023-03-05
        相关资源
        最近更新 更多