[英]How do you use a custom view in a HorizontalScrollView?
I've got a custom view (called CustomStrechView
) which extends
an ImageView
. 我有一个自定义视图(称为
CustomStrechView
),它extends
了ImageView
。 The point of this view is so that I can get an image to fill the view without losing the aspect ratio. 该视图的目的是使我可以在不损失宽高比的情况下获得图像以填充视图。
Now the CustomStrechView
works fine when I use it in most applications, but when I try to apply it within my HorizontalScrollView
, the images all seem to disappear. 现在,当我在大多数应用程序中使用
CustomStrechView
时,它可以正常工作,但是当我尝试将其应用到HorizontalScrollView
,图像似乎都消失了。 I'm not sure why this happens. 我不确定为什么会这样。 I've tried removing the
<HorizontalScrollView />
and when I do the images show back up, so I feel there is some property that needs to be set, but I can't seem to find it. 我尝试删除
<HorizontalScrollView />
并在显示图像时进行备份,因此我觉得需要设置一些属性,但似乎找不到。
Can anyone point out what I'm doing wrong or a better way to get a custom view to work in a HorizontalScrollView
? 谁能指出我做错了什么,还是使自定义视图在
HorizontalScrollView
工作的更好方法?
Edit : 编辑 :
After playing around with this for a bit, I've determined that I can get the custom view to show the image only if I hardcode the width
dimension, ie: 经过一段时间的研究,我确定只有对
width
尺寸进行硬编码时,才能获得自定义视图以显示图像,即:
<com.example.dragdropshapes.CustomStrechView
android:layout_width="200dp"
Leaving the height as fill_parent
is fine... 将高度保留为
fill_parent
很好...
Code : 代码 :
Here's how my custom view is being used in the xml file: 这是我的自定义视图在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>
Here's the major part of the custom view code: 这是自定义视图代码的主要部分:
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);
}
}
Through some debugging I was able to at least partially solve this myself. 通过调试,我至少可以自己解决部分问题。 Turns out that the
width
acquired from the MeasureSpec.getSize(widthMeasureSpec);
原来是从
MeasureSpec.getSize(widthMeasureSpec);
获得的width
MeasureSpec.getSize(widthMeasureSpec);
call was 0. Thus the calculation ended in width and height being 0 and the setMeasuredDimension
call would then make the image invisible. 调用为0。因此,计算的宽度和高度为0结束,然后
setMeasuredDimension
调用将使图像不可见。
Now I'm saying I have this partially solved, because I still don't understand why I was getting that value of 0. I think it's because in a HorizontalScrollView
the width isn't determined (it depends on the content within) thus whatever MeasureSpec.getSize(widthMeasureSpec);
现在,我说我已经部分解决了这个问题,因为我仍然不明白为什么我得到那个0值。我认为这是因为在
HorizontalScrollView
,宽度是不确定的(取决于其中的内容),因此无论MeasureSpec.getSize(widthMeasureSpec);
is actually doing is talking to the parent, and the parent in a normal Linear
or RelativeLayout
has a defined width where as in a HorizontalScrollView
it does not... but that's just speculation at this point. 实际上,这是在与父级对话,正常的
Linear
或RelativeLayout
的父级具有定义的宽度,而在HorizontalScrollView
则没有。但是,这只是推测。
So I made a fix for it, if the width
is 0 we run the calculation via the height
instead. 因此,我对此进行了修复,如果
width
为0,则改为通过height
运行计算。 The height
needed two cases to cover it to avoid divide by 0 errors (the else if
and the else
collectively). height
需要用两种情况来覆盖它,以免除以0的错误(“ else if
和“ else
共同)。
Here's my current solution with the same xml file I noted in the Question. 这是我当前的解决方案,带有我在问题中提到的相同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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.