2015-11-17 46 views
0

我正在嘗試創建帶有圖像的水平滾動視圖(如照片查看器,但不是全屏)。在iOS中,您可以將子視圖添加到UIScroll視圖,有什麼方法可以在android上執行相同的操作?如果有幫助,我使用Xamarin。 我試圖從Xamarin https://components.xamarin.com/view/MultiImageView/ 使用multiimageview成分,但沒有檢索當前索引,我需要選擇的圖像數量Android Xamarin中有沒有類似於UIScrollVIew的東西?

+0

在Android中當我試圖添加到Horizo​​ntalScrollView墜毀多個視圖它只是叫滾動型 – Nanoc

回答

0

除了Android的滾動型是隻有垂直滾動你有一個HorizontalScrollView

小心你的記憶。在Android中,您需要確保在屏幕出現時爲圖像釋放內存。像UniversalImageLoader或Picasso這樣的庫對此很有幫助,但我不知道是否可以將它們包含在Xamarin中。

+0

,認爲這隻能承載一個孩子 – Mookker

+0

是,在Android上滾動型和Horizo​​ntalScrollView總是需要下面的另一佈局。爲了您的目的,LinearLayout的orientation = horizo​​ntal似乎很適合。 – Christian

0

如果您是使用XML的佈局,你可以使用下面的

<HorizontalScrollView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:fillViewport="true" 
    android:scrollbars="horizontal|vertical" > 

你也可以做的正是通過代碼同樣的事情(見參考文獻https://developer.xamarin.com/api/type/Android.Widget.HorizontalScrollView/)。

最後,如果您使用Xamarin.Forms,您可以使用已在所有平臺上實施的ScrollView,並且您可以設置「方向」。 (請參閱參考https://developer.xamarin.com/api/type/Xamarin.Forms.ScrollView/

0

請確保您創建圖像的縮略圖版本,否則當您有多個圖像時,您將開始遇到內存和性能問題。這是來自我自己的應用程序的代碼,它代碼BaseAdapter <>,並在列表中顯示照片以及其他內容。我在列表適配器中進行大小調整,但沒有理由不能先完成或緩存然後重新使用。

// get the a photo item based on list position 
var item = items[position]; 

// just getting photo info from my list of photos 
var info = item.Data.Photos[0]; 

var options = new BitmapFactory.Options() { InJustDecodeBounds = true }; 
BitmapFactory.DecodeFile(info.Filename, options); 

// Raw height and width of image 
var height = options.OutHeight; 
var width = options.OutWidth; 
int inSampleSize = 1; 

var viewWidth = 100; 
var viewHeight = 100; 

if (height > viewHeight || width > viewWidth) 
{ 

    // Calculate ratios of height and width to requested height and width 
    var heightRatio = (int)Math.Round((float)height/(float)viewHeight); 
    var widthRatio = (int)Math.Round((float)width/(float)viewWidth); 

    // Choose the smallest ratio as inSampleSize value, this will guarantee 
    // a final image with both dimensions larger than or equal to the 
    // requested height and width. 
    inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; 
} 

options.InSampleSize = inSampleSize; 
options.InJustDecodeBounds = false; 

image.SetImageBitmap(BitmapFactory.DecodeFile(info.Filename, options)); 
相關問題