Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android编程裁剪背景图像_Java_Android_Xml - Fatal编程技术网

Java Android编程裁剪背景图像

Java Android编程裁剪背景图像,java,android,xml,Java,Android,Xml,我想在我的应用程序中显示图像作为背景。现在我在中使用了以下语句:android:background=“@drawable/background”。以下是.xml代码: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent

我想在我的应用程序中显示图像作为背景。现在我在
中使用了以下语句:
android:background=“@drawable/background”
。以下是
.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>

现在我得到这个输出:

但我希望它是这样的:

也许你们中有人能帮我。谢谢。

有,可以这样做。首先,您需要创建一个可绘制资源,如下所示(让它成为项目资源中的一个文件
res/drawable/mybg.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/mybg">
</LinearLayout>

您需要在photoshop或其他软件中更改图像本身的大小,以达到所需的效果(但由于android智能手机的屏幕大小不同,这可能非常困难)。解决方法是将父布局更改为Relativelayout,然后使用imageview以如下方式显示图片:


把你的孩子的观点放在这里

android:scaleType有4-5个选项可供选择。。。尝试一下,直到你得到你想要的结果。还请注意,您需要使用imageview的android:src而不是android:background属性

从文档中:

BitMapRegionCoder可用于解码图像中的矩形区域 形象。BitMapRegionCoder在原始 图像很大,您只需要图像的一部分

imgview.setScaleType(ImageView.ScaleType.CENTER_CROP);

它允许您相当容易地解码特定位图的矩形区域,您需要做的唯一事情是计算相对于位图解码的矩形区域。

在android中,图像视图的位置由图像的左上方确定。我假设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);

@我的新名字我觉得这是一个方便和简单的方式。imageview中的scaleType属性确实为您提供了有用的灵活性。添加额外的ImageView并不会使您的布局负担过重(除非它已经非常凌乱)。我如何通过编程实现呢?i、 我想从服务器的图像,并显示为裁剪。。。可以使用毕加索吗?当可绘制的是矢量(SVG)时,这不起作用,它在运行时崩溃。您知道如何使用矢量吗?@Shirane85根据文档,仅与
png
jpeg
gif
格式兼容。你不能让它与向量一起工作。据我所知,唯一支持
svg
文件的可绘制资源是,但它没有类似的设置。“如果你不想在这方面投入太多的精力,我想这应该是最合适的。”他说,“是的,我最终就是这么做的。”。谢谢:)
imgview.setScaleType(ImageView.ScaleType.CENTER_CROP);