1

你好,我有一個ImageView(背景)的高度應該對應屏幕高度,另一方面寬度應該保持不變。圖像視圖佔據整個背景,用戶可以向右或向左滾動看到整個圖像 -ImageView大於屏幕作爲背景

在此之前是這樣

<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框架/相對佈局(寬度設置爲wrap_content)圖像視圖會縮小以適應屏幕。

有沒有辦法讓ImageView的寬度不變(與高度匹配屏幕),這樣它會比手機屏幕保持更大?(不裁剪)

然後我可以使用自定義動畫滾動對。

我看過類似的問題,但我無法找到任何可以在這種情況下工作的東西。

回答

2

你可以做的是嘗試覆蓋Horizo​​ntalScrollView的onTouchListener。

例如,這樣的事情:

scrollView.setOnTouchListener(new OnTouch()); 

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

這從滾動Horizo​​ntalScrollView,而你很可能還是把它的滾動編程方式禁用用戶。

+0

我想過,但沒有一種方法可以做到這一點,而不使用Horizo​​ntalScrollView? –

+0

我不確定。你說當你刪除Horizo​​ntalScrollView時,圖像被縮小了。你可以嘗試玩ImageView的ScaleType。如果其中一個可用的ScaleType工作,則可以嘗試通過在ImageView上使用translationX動畫來執行「滾動」。 – JaapM

+0

Aww嘗試了ScaleType和AdjustViewBounds的每種組合,並且每一個組合似乎都試圖適應設備屏幕。它的做法是在xxhdpi可繪製文件夾中有相同圖像的幾個版本,當我更改scaleType時,只需選擇適合屏幕並顯示它的較小圖像。 –