1

我有一个自定义视图CustomStrechViewextends称为ImageView. 这个视图的目的是让我可以在不丢失纵横比的情况下获得一个图像来填充视图。

现在,CustomStrechView当我在大多数应用程序中使用它时效果很好,但是当我尝试在我HorizontalScrollView的 中应用它时,图像似乎都消失了。我不确定为什么会这样。我试过删除<HorizontalScrollView />,当我做图像显示备份时,我觉得有一些属性需要设置,但我似乎找不到它。

谁能指出我做错了什么或更好的方法来让自定义视图在 a 中工作HorizontalScrollView


编辑

在玩了一会儿之后,我确定只有当我对width尺寸进行硬编码时,我才能让自定义视图显示图像,即:

<com.example.dragdropshapes.CustomStrechView
    android:layout_width="200dp"

保持高度fill_parent就好了...


代码

这是我的自定义视图在 xml 文件中的使用方式:

<!-- Removing this makes the images show up -->
<HorizontalScrollView       
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true"
    android:layout_marginTop="50dp">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:showDividers="middle"
        android:orientation="horizontal">

        <!-- Changing this to a normal ImageView makes the image show up -->
        <com.example.dragdropshapes.CustomStrechView
            android:id="@+id/pickshapes"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent"
            android:clickable="true"
            android:onClick="pickShapes"
            android:background="@drawable/border"
            android:src="@drawable/ic_launcher"
            />
        <!-- There are 5 other custom ImageViews here -->
    </LinearLayout>
</HorizontalScrollView>

这是自定义视图代码的主要部分:

public class CustomStrechView extends ImageView{

  private final Drawable srcPic;
  private final String TAG = "strechyTag";
  private boolean changeSize = false;
  private int Xpx;
  private int Ypx;

    public CustomStrechView(Context context) {
        super(context);
        srcPic = this.getDrawable();
    }

     //... other constructors, not really important...

     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
         Log.d(TAG, "in the onMeasure!!\n");
         int width, height;

        width = MeasureSpec.getSize(widthMeasureSpec);
        height = width * srcPic.getIntrinsicHeight() / srcPic.getIntrinsicWidth();
        setMeasuredDimension(width, height);
    }
}
4

1 回答 1

0

通过一些调试,我自己至少能够部分解决这个问题。结果是widthMeasureSpec.getSize(widthMeasureSpec);调用中获取的为 0。因此计算以宽度和高度为 0 结束,setMeasuredDimension然后调用将使图像不可见。

现在我说我已经部分解决了,因为我仍然不明白为什么我得到了 0 的值。我认为这是因为HorizontalScrollView宽度没有确定(它取决于里面的内容)所以不管MeasureSpec.getSize(widthMeasureSpec);是实际上做的是与父母交谈,并且父母在正常LinearRelativeLayout具有定义的宽度,而在 aHorizontalScrollView它没有......但这只是在这一点上的猜测。

所以我对其进行了修复,如果width为 0,我们将通过 来运行计算heightheight需要两种情况来覆盖它以避免除以 0 错误(theelse if和 the 的总称else)。

这是我当前使用问题中提到的相同 xml 文件的解决方案。

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
    int width = MeasureSpec.getSize(widthMeasureSpec);
    int height = MeasureSpec.getSize(heightMeasureSpec);

    if(width > 0)
        height = width * srcPic.getIntrinsicHeight() / srcPic.getIntrinsicWidth();
    else if(srcPic.getIntrinsicWidth() < srcPic.getIntrinsicHeight())
     width = height / (srcPic.getIntrinsicHeight() / srcPic.getIntrinsicWidth());
    else
     width = height / (srcPic.getIntrinsicWidth() / srcPic.getIntrinsicHeight());

    setMeasuredDimension(width, height);
}
于 2013-10-17T15:59:03.290 回答