2011-12-01 43 views
1

我有一个ArrayList中的图像位置列表(SD卡图像路径)。我可以将它传递给Gallary并让用户通过图像轻扫?Android:Gallary像刷卡效果

如果没有,我怎么能实现类似的东西?

有些帮助赞赏。感谢您的时间提前。

PS:我在网上搜索,但无法找到我想要的。谢谢。

回答

2

您可以直接使用Gallery类。这里有一些代码片段,但是有大量的例子可以在线获得。

onCreate(Bundle b)应该看起来像这样。

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

    Gallery gallery = (Gallery) findViewById(R.id.gallery); 
    gallery.setAdapter(new ImageAdapter(this)); 

    gallery.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView parent, View v, int position, long id) { 
      Toast.makeText(HelloGallery.this, "" + position, Toast.LENGTH_SHORT).show(); 
     } 
    }); 
} 

这里是main.xml

<?xml version="1.0" encoding="utf-8"?> 
<Gallery xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gallery" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
/> 

res/values/attrs.xml shold这个样子。

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="HelloGallery"> 
     <attr name="android:galleryItemBackground" /> 
    </declare-styleable> 
</resources> 

Adapter类应该是如下

public class ImageAdapter extends BaseAdapter { 
    int mGalleryItemBackground; 
    private Context mContext; 

    private Integer[] mImageIds = { 
      R.drawable.sample_1, 
      R.drawable.sample_2, 
      R.drawable.sample_3, 
      R.drawable.sample_4, 
      R.drawable.sample_5, 
      R.drawable.sample_6, 
      R.drawable.sample_7 
    }; 

    public ImageAdapter(Context c) { 
     mContext = c; 
     TypedArray attr = mContext.obtainStyledAttributes(R.styleable.HelloGallery); 
     mGalleryItemBackground = attr.getResourceId(
       R.styleable.HelloGallery_android_galleryItemBackground, 0); 
     attr.recycle(); 
    } 

    public int getCount() { 
     return mImageIds.length; 
    } 

    public Object getItem(int position) { 
     return position; 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     ImageView imageView = new ImageView(mContext); 

     imageView.setImageResource(mImageIds[position]); 
     imageView.setLayoutParams(new Gallery.LayoutParams(150, 100)); 
     imageView.setScaleType(ImageView.ScaleType.FIT_XY); 
     imageView.setBackgroundResource(mGalleryItemBackground); 

     return imageView; 
    } 

这个例子是可用here。看看它。

+0

但是这并没有刷卡的效果呐?我们需要点击缩略图。我需要像在默认Android Gallary中那样拥有滑动效果。 –

+0

它有类似的滑动效果。否则看看coverflow。这延伸了画廊。 –

+0

刷卡效果将可用于thumnails仪式?但我想要图像的滑动效果。我甚至不想要缩略图。只是显示图像全屏幕与fling效果。封面流看起来不错。尝试几个例子 –