【问题标题】:ImageView larger than the screen as a background比屏幕大的ImageView作为背景
【发布时间】:2017-06-07 14:01:44
【问题描述】:

您好,我有一个 ImageView(background),其高度应与屏幕高度相对应,另一方面,宽度应保持不变。

之前是这样的

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="match_parent">

 <HorizontalScrollView
     android:layout_width="wrap_content"
     android:layout_height="match_parent"> 

    <ImageView
        android:id="@+id/songDetails_songImage"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:contentDescription="@string/songDetails_cd_songCover"
        app:srcCompat="@drawable/default_song_cover" />

</HorizontalScrollView>  
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
...

这很好 - 图像视图占据整个背景,用户可以向右或向左滚动以查看整个图像。

我的目标是为滚动设置动画,为此我需要摆脱水平视图(以防止用户滚动),但每当我将 Horizo​​ntalScrollView 更改为 Frame/Relative 布局(宽度设置为 wrap_content)时图像视图按比例缩小以适合屏幕。

有没有办法保持 imageView 的宽度不变(高度与屏幕的高度匹配),使其保持比设备屏幕大?(不裁剪)

然后我可以使用自定义动画向右滚动。

我已经查看了类似的问题,但我找不到任何适用于这种情况的方法。

【问题讨论】:

    标签: android android-layout android-imageview horizontalscrollview


    【解决方案1】:

    您可以做的是尝试覆盖 Horizo​​ntalScrollView 的 onTouchListener。

    例如,像这样的:

    scrollView.setOnTouchListener(new OnTouch());
    
    private class OnTouch implements OnTouchListener
    {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
        return true;
        }
    }
    

    这将禁止用户滚动 Horizo​​ntalScrollView,而您仍然可以通过编程方式滚动它。

    【讨论】:

    • 我想过这个问题,但是不使用 Horizo​​ntalScrollView 就没有办法做到这一点吗?
    • 我不确定。您是说删除 Horizo​​ntalScrollView 时图像会缩小。您可以尝试使用 ImageView 的 ScaleType。如果可用的 ScaleType 之一有效,您可以尝试通过在 ImageView 上使用 translateX 动画来进行“滚动”。
    • Aww 可能尝试了 ScaleType 和 AdjustViewBounds 的每一种组合,而且每一种似乎都试图适应设备屏幕。这样做的方式是我在 xxhdpi 可绘制文件夹中有相同图像的几个版本,当我更改 scaleType 时,它​​只是选择适合屏幕的较小图像并显示它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多