【问题标题】:Image in layout is not the correct size布局中的图像尺寸不正确
【发布时间】:2016-08-02 20:06:59
【问题描述】:

看起来是这样的:

我在布局中添加了一张图片。因为图像很大很小,最后在我的屏幕中间,没有填满屏幕的整个宽度,我不得不把它拉长。这样一来,字母显然也伸展了,这使它看起来很糟糕。我知道这可能无法修复。每个图像都需要被拉伸,所以颜色很好,但是之后的字母总是有点朦胧。 如果这根本没有解决方案,请告诉我,这样我就知道了。如果您认为自己知道如何解决此问题,请提供帮助。

不认为它是相关的,但这是我添加图像的代码:

<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="com.example.rodekruis.MainActivity">


    <ImageView
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:src="@drawable/rkz_logo4"
          android:layout_marginBottom="30dp"
           android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:scaleType="fitXY"
         />

【问题讨论】:

  • 最简单也是唯一的解决方案是在 photoshop 中编辑它之前或使用类似于 photoshop online 的东西:sumopaint.com/home
  • 这是一个你总会遇到的问题。您必须提供适合所有屏幕的图像。看这里:developer.android.com/guide/practices/screens_support.html。这意味着,您必须根据屏幕大小/密度将不同的图像放在布局文件夹中......
  • android:scaleType="center" 或 android:scaleType="centerInside" 或 android:scaleType="centerCrop"
  • 问题在于图像分辨率,由于截图中的图像不是很复杂且难以创建,最后也是最好的解决方案是在绘图软件中创建具有更高分辨率的图像,跨度>
  • 我正在尝试在绘画中创建图片,但我必须创建并缩小它的尺寸,因为我知道在布局中它会伸展。当我痛苦地瘦它时,分辨率已经在降低,所以当它伸展时,尺寸很好,但分辨率却不好,因为我无法保持它与我找到图像相同的形状..

标签: android eclipse image layout


【解决方案1】:

尝试删除属性android:scaleType = "fitXY" 并替换android: layout_width = "fill_parent" on android: layout_width = "wrap_content"

<ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:src="@drawable/rkz_logo4"
      android:layout_marginBottom="30dp"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
/>

【讨论】:

  • 这会将图像缩小到更小的尺寸。一切都很清楚,但它的宽度不足以从左到右填满我的屏幕,这正是我想要的。
  • 您可以将drawable转换为位图,调整位图大小并以编程方式设置
【解决方案2】:

创建 MyImageView.class

public class MyImageView extends ImageView {

public final static int WIDTH_IMAGE = 640;
public final static int HEIGHT_IMAGE = 200;
public MyImageView(Context context) {
    super(context);
}

public MyImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public MyImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int width = MeasureSpec.getSize(widthMeasureSpec);
    int height = width * HEIGHT_IMAGE / WIDTH_IMAGE;
    setMeasuredDimension(width, height);
}
}

在xml中

<?xml version="1.0" encoding="utf-8"?>
<your.package.MyImageView    xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/image_banner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/rkz_logo4"
android:layout_marginBottom="30dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>

【讨论】:

  • 我在另一个帖子上得到了相反的评论,中心裁剪使其完全放大,FitXY 只是将其拉伸。两者都不是预期的结果,但 FitXY 越来越接近,因为颜色被正确拉伸
猜你喜欢
  • 2012-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-15
  • 1970-01-01
  • 2013-02-05
  • 2021-11-16
  • 2013-10-17
相关资源
最近更新 更多