2013-07-05 62 views
6

我正嘗試使用horizo​​ntalscrollview做一個圖片庫的簡單示例並動態添加圖片。我搜索了一些例子,但大多數都太複雜。有一個簡單的例子來說明如何做到這一點?帶有水平滾動視圖的圖片庫

+3

發佈您的代碼,如果您遇到困難,那麼我們可以解決問題,我們不在這裏工作的任何方式。 –

回答

12

Here是一些簡單的例子已經實施水平滾動視圖看起來像圖片庫

這將幫助你

添加Horizo​​ntalScrollView佈局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <HorizontalScrollView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 
     <LinearLayout 
      android:id="@+id/mygallery" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      /> 
    </HorizontalScrollView> 

</LinearLayout> 

主要的Java代碼:

package com.example.androidhorizontalscrollviewgallery; 

import java.io.File; 

import android.os.Bundle; 
import android.os.Environment; 
import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.view.Gravity; 
import android.view.View; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
import android.widget.Toast; 

public class MainActivity extends Activity { 

LinearLayout myGallery; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     myGallery = (LinearLayout)findViewById(R.id.mygallery); 

     String ExternalStorageDirectoryPath = Environment 
      .getExternalStorageDirectory() 
      .getAbsolutePath(); 

     String targetPath = ExternalStorageDirectoryPath + "/test/"; 

     Toast.makeText(getApplicationContext(), targetPath, Toast.LENGTH_LONG).show(); 
     File targetDirector = new File(targetPath); 

     File[] files = targetDirector.listFiles(); 
     for (File file : files){ 
     myGallery.addView(insertPhoto(file.getAbsolutePath())); 
     }  
    } 

    View insertPhoto(String path){ 
    Bitmap bm = decodeSampledBitmapFromUri(path, 220, 220); 

    LinearLayout layout = new LinearLayout(getApplicationContext()); 
    layout.setLayoutParams(new LayoutParams(250, 250)); 
    layout.setGravity(Gravity.CENTER); 

    ImageView imageView = new ImageView(getApplicationContext()); 
    imageView.setLayoutParams(new LayoutParams(220, 220)); 
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
    imageView.setImageBitmap(bm); 

    layout.addView(imageView); 
    return layout; 
    } 

    public Bitmap decodeSampledBitmapFromUri(String path, int reqWidth, int reqHeight) { 
    Bitmap bm = null; 

    // First decode with inJustDecodeBounds=true to check dimensions 
    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(path, options); 

    // Calculate inSampleSize 
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 

    // Decode bitmap with inSampleSize set 
    options.inJustDecodeBounds = false; 
    bm = BitmapFactory.decodeFile(path, options); 

    return bm; 
    } 

    public int calculateInSampleSize(

    BitmapFactory.Options options, int reqWidth, int reqHeight) { 
    // Raw height and width of image 
    final int height = options.outHeight; 
    final int width = options.outWidth; 
    int inSampleSize = 1; 

    if (height > reqHeight || width > reqWidth) { 
     if (width > height) { 
     inSampleSize = Math.round((float)height/(float)reqHeight); 
     } else { 
     inSampleSize = Math.round((float)width/(float)reqWidth); 
     } 
    } 

    return inSampleSize; 
    } 

} 

注意:在這個ex充足的情況下,即使不在屏幕上,Horizo​​ntalScrollView中的位圖也不會被刪除。所以如果加載的位圖太多,就會拋出java.lang.OutOfMemoryError的錯誤!

+0

你能幫我在這[問題](http://stackoverflow.com/questions/33219942/horitzontalscrollview-or-carrousel)? :d –

-7

你可以創建一個div並將溢出設置爲隱藏,然後嵌套另一個裏面的所有圖像的高度或寬度取決於你想要水平的動畫,以寬度乘以圖像的數量並記住在此包括邊際。然後,您編寫一個jQuery的動畫對大格將其移動到左側或右側再次運行根據自己的喜好

eg $('pic').animate({left:'_____ <- enter width of single image together with margin here ' +'px','slow' <-- whatever speed you prefer);

,然後用它從那裏玩。您的處理程序可以再次單擊另一個按鈕或圖像...偏好。它沒有太多的編碼是非常靈活的,它不是最好的選擇。如果這不適合你,那麼創建一個圖像數組並循環遍歷它們,而不是使用這些特定的尺寸進行滾動。祝你好運。希望我已經幫助你。

+3

jquery不會在這裏幫助在android – roarster