java android-为什么图像会失真?

java android-为什么图像会失真?,java,android,png,Java,Android,Png,你自己试试吧-用anyPngYouWant.png打开一个布局,然后用anyPngYouWant.png创建一个bitmap.xml <bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:src="@drawable/anyPngYouWant" /> 并放在同一个布局上 为什么尺寸不同?我尝试过制作许多不同的图像,但仍然在bitmal.xml中,wrap_内容的大小与源

你自己试试吧-用anyPngYouWant.png打开一个布局,然后用anyPngYouWant.png创建一个bitmap.xml

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

并放在同一个布局上

为什么尺寸不同?我尝试过制作许多不同的图像,但仍然在bitmal.xml中,wrap_内容的大小与源png不同。我只是想让他们保持一致

布局:

<RelativeLayout 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"

    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/anyPngYouWant" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/imageView1"
        android:src="@drawable/bitmap" />

</RelativeLayout>

检查此方法,它获取图像文件的位图对象,然后在
图像视图上设置:

File ifile = new  File(“PATH/anyPngYouWant.png”);
if(ifile.exists()){

    Bitmap bmp = BitmapFactory.decodeFile(ifile.getAbsolutePath());

    ImageView iv = (ImageView) findViewById(R.id.imageView1);
    iv.setImageBitmap(bmp);

}

为什么你需要将它放入而不是直接使用它,我认为使用图像直接将它放入ImageView没有问题。你不能使用
.png
这样的文件
@drawable/anyPngYouWant.png
-它们还不是可绘制的资源。因为我需要在tileMode中使用它。它在tileMode中被扭曲。但即使没有它,它也是扭曲的。我不能使用设计文件,我也不能用所有这些做正确的布局好的,g00dy,我怎么能使用它们?我只需要为tileMode设置一个png文件。请帮我检查一下这里的答案,看看如何正确地将图像文件设置为
imageView
。如果它在Resources中,我为什么需要它?我可以为这个制作平铺位图吗?如果它在参考资料中,请像这样使用:
@drawable/anyPngYouWant
不带文件扩展名。我错了,它当然没有扩展名!如果您尝试执行我所描述的操作,您将看到,如果您将任何资源的png文件放在bitmap.xml的布局中,它的大小都会不同。只需尝试在版面上放置两张相同的图片,一张是带有anyPngYouWant的ImageView,另一张是带有bitmap.xml的,并将anyPngYouWant设置为源。他们将是不同的大小!这取决于多个因素,如果版面大小固定(我的意思是宽度和高度-这包括填充父对象等,但包裹内容除外),您将看到与原始图像大小不同的图像。现在你知道我需要什么了代码+xml(可能有线索)?您可以做的是确保原始.png图像本身没有失真(在PC上打开),然后使用我的方法将其设置为宽度和高度均为
wrap_content
imageView
,然后查看是否失真。然后我们将找出问题所在。这两种情况都是wrap_内容,bitmap.xml是问题所在。我在许多不同的图像上都尝试过这种方法——虽然在布局上设置了wrap_内容,但bitmap.xml总是会扭曲图像。