【问题标题】:java android - why images are distorted?java android - 为什么图像失真?
【发布时间】:2013-08-10 00:59:04
【问题描述】:

自己试试吧——用 anyPngYouWant.png 放置一个 ImageView 布局,然后用

制作一个 bitmap.xml
<bitmap xmlns:android="http://schemas.android.com/apk/res/android" 
    android:src="@drawable/anyPngYouWant"
 />

并放入相同的布局..

为什么它们的大小不同?我尝试了许多不同的图像,但在 bitmal.xml 中 wrap_content 的大小仍然与源 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>

【问题讨论】:

  • 为什么需要放到中而不是直接使用,我觉得使用图片直接放到ImageView中没有问题。
  • 你不能使用 .png 这样的文件 @drawable/anyPngYouWant.png - 它们还不是可绘制资源
  • 因为我需要在 tileMode 中使用它。它在 tileMode 中失真。但即使没有它,它也会被扭曲。我无法使用设计文件,也无法做出正确的布局
  • 好的,g00dy,我该如何使用它们?我只需要为 tileMode 设置一个 png 文件。帮帮我
  • 在此处查看答案,了解如何将图像文件正确设置为imageView

标签: java android png


【解决方案1】:

勾选这个方法,获取图片文件的Bitmap对象,然后设置在imageView上:

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);

}

【讨论】:

  • 好吧,如果它在资源中,我为什么需要这个?我可以用这个制作平铺位图吗?
  • 如果它在资源中,像这样使用它:@drawable/anyPngYouWant 没有文件扩展名。
  • 我的错误,当然没有扩展!如果您尝试执行我所描述的操作 - 如果您将其放在 bitmap.xml 中的布局上,您会发现您的任何资源的 png 文件的大小都会不同。只需尝试在布局上放置 2 张相同的图片 - 一张作为 ImageView 并使用 anyPngYouWant ,另一张使用 bitmap.xml 并将 anyPngYouWant 设置为源。它们的大小会不同!
  • 这取决于几个因素,如果布局具有固定大小(我的意思是宽度和高度 - 这包括 fill_parent 等,除了 wrap_content)你会看到一个图像与原始尺寸不同。你现在明白我为什么需要代码 + xml(可能有线索)?你可以做的是确保原始.png图像本身不失真(在PC上打开它),然后使用我的方法将其设置为imageView,宽度和高度为wrap_content,看看它是否扭曲。然后我们会找出问题所在。
  • 两种情况都是wrap_content,bitmap.xml是有问题的。我已经尝试过使用许多不同的图像 - bitmap.xml 总是会扭曲它们,尽管它是在布局上设置 wrap_content 的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-11
  • 1970-01-01
  • 1970-01-01
  • 2018-09-23
  • 1970-01-01
  • 2015-01-21
  • 1970-01-01
相关资源
最近更新 更多