【问题标题】:How to get ImageView full width when inside a HorizontalScrollView如何在 Horizo​​ntalScrollView 内获取 ImageView 全宽
【发布时间】:2019-01-09 09:11:45
【问题描述】:


我在 Horizo​​ntalScrollView 中放置了一个非常宽的图像。
ImageView/ScrollView 高度是动态的,因为我已将高度设置为 0dp 并添加了约束。
由于 ImageView 的缩放类型为 fitStart 且 adjustViewBounds 为 true - 正在调整图像的宽度。

XML:

     <HorizontalScrollView
        android:id="@+id/mapScrollView"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginBottom="10dp"
        app:layout_constraintBottom_toTopOf="@id/playButton"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <android.support.constraint.ConstraintLayout
            android:id="@+id/mapLayout"
            android:layout_width="wrap_content"
            android:layout_height="match_parent">

            <ImageView
                android:id="@+id/mapImage"
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:adjustViewBounds="true"
                android:scaleType="fitStart"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <com.gigi.testmap.activities.quest.QuestMapPathView
                android:id="@+id/mapLinesView"
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:layout_constraintBottom_toBottomOf="@id/mapImage"
                app:layout_constraintEnd_toEndOf="@id/mapImage"
                app:layout_constraintStart_toStartOf="@id/mapImage"
                app:layout_constraintTop_toTopOf="@id/mapImage" />

        </android.support.constraint.ConstraintLayout>

    </HorizontalScrollView>


我正在尝试获取 ImageView 宽度(总宽度,可见和不可见部分)。获取 ScrollView 总宽度也会有所帮助。
我的目标是将按钮放置在地图顶部根据渲染的 ImageView 的宽度和高度计算的位置。

我正在使用 Glide 加载图像:

final ImageView mapImage = mActivity.findViewById(R.id.mapImage);
Glide.with(mActivity).load(R.drawable.fullmap).into(mapImage);

mapImage.getViewTreeObserver().addOnGlobalLayoutListener(new 
ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        mapImage.getWidth(); // --> returns 0    
        mapImage.getViewTreeObserver()
             .removeOnGlobalLayoutListener(this);
    }
 });

我尝试使用树视图观察者的全局布局侦听器获取宽度,但我得到的只是 0。虽然返回了正确的高度

任何帮助将不胜感激, 非常感谢。

【问题讨论】:

  • 只是把秤类型改成FitXy
  • 没用。该问题似乎与首次调用全局布局侦听器时未呈现图像这一事实有关。
  • 你试过 piccaso 库吗?

标签: android android-imageview android-scrollview


【解决方案1】:

我没有使用 Glide 的经验。

您正在设置所有视图/容器android:layout_height="0dp" 首先尝试将其更改为任何其他任意值或wrap_content

您没有附上您尝试获取高度的代码。

你试过mapImage.getHeight()

【讨论】:

  • 感谢您的回答。我的问题是关于宽度的。请阅读 ConstraintLayout 以了解为什么某些值是 0dp
【解决方案2】:

对于Bitmap,我以前是这样弄的

Glide.with(mContext())
 .asBitmap()
 .load(path)
 .into(new SimpleTarget<Bitmap>() {
     @Override
     public void onResourceReady(Bitmap bitmap,
                                 Transition<? super Bitmap> transition) {
         int w = bitmap.getWidth();
         int h = bitmap.getHeight()

     }
 });

【讨论】:

  • 感谢您的回答。此解决方案返回图像文件的原始大小。我需要调整大小的。
  • 在使用 Glide 之前尝试使用包装内容并使用 holder.mapInstaler.layout(0,0,0,0)
  • 图像宽度已经是wrap_content。不确定您的建议。
【解决方案3】:

似乎在首次调用树视图全局布局侦听器后图像没有完全呈现(我猜这是因为图像宽度非常大) - 我所做的是检查图像宽度并且仅当大于 0,移除监听器并继续我的代码。

final ImageView mapImage = mActivity.findViewById(R.id.mapImage);
        Glide.with(mActivity).load(R.drawable.fullmap).into(mapImage);
        mapImage.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (mapImage.getWidth() > 0) {
                    mapImage.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    //continue width related code here
                }
            }
        });

也许不是完美的解决方案,在图像显示之前会有一点延迟 - 但对于我的用例来说没问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-02
    • 2014-02-13
    • 1970-01-01
    • 1970-01-01
    • 2012-10-04
    • 2014-07-22
    相关资源
    最近更新 更多