android之Gallery默认实现
2012-01-10 11:14
155 查看
先看下实现代码:
package com.android.gallery; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageSwitcher; import android.widget.ImageView; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.Gallery.LayoutParams; import android.widget.ViewSwitcher.ViewFactory; public class ImageGalleryActivity extends Activity implements ViewFactory, OnItemSelectedListener { private Gallery mGallery; private ImageSwitcher mImgSwitcher; private ImageAdapter mImgAdapter; private int[] resId = new int[]{ R.drawable.gallery_photo_1, R.drawable.gallery_photo_2, R.drawable.gallery_photo_3, R.drawable.gallery_photo_4, R.drawable.gallery_photo_5, R.drawable.gallery_photo_6, R.drawable.gallery_photo_7, R.drawable.gallery_photo_8 }; /** * Gallery实现中出现了图片重叠,这个只要在xml布局中添加android:spacing="10dip" */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mGallery = (Gallery)findViewById(R.id.gallery_id); mImgSwitcher = (ImageSwitcher)findViewById(R.id.imgSwitcher_id); mImgAdapter = new ImageAdapter(this); mGallery.setAdapter(mImgAdapter); mGallery.setOnItemSelectedListener(this); mImgSwitcher.setFactory(this); } class ImageAdapter extends BaseAdapter { private Context mContext; public ImageAdapter(Context context) { mContext = context; } @Override public int getCount() { // TODO Auto-generated method stub return Integer.MAX_VALUE; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return position; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View converView, ViewGroup parent) { ImageView mImgView = new ImageView(mContext); //这里采用求余数来实现图片循环显示 mImgView.setImageResource(resId[position % resId.length]); //这里ScaleType的类型有好多种,FIT_CENTER等,就是设置img的缩放比例 mImgView.setScaleType(ImageView.ScaleType.FIT_XY); //这个可以实现每屏只显示一张图片 //mImgView.setLayoutParams(new Gallery.LayoutParams(Gallery.LayoutParams.FILL_PARENT, Gallery.LayoutParams.FILL_PARENT)); mImgView.setLayoutParams(new Gallery.LayoutParams(163,106)); return mImgView; } } @Override public View makeView() { ImageView mImgView = new ImageView(this); mImgView.setBackgroundColor(0xFF000000); mImgView.setScaleType(ImageView.ScaleType.FIT_CENTER); mImgView.setLayoutParams(new ImageSwitcher.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); return mImgView; } @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub mImgSwitcher.setImageResource(resId[arg2 % resId.length]); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }
xml布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Gallery android:id="@+id/gallery_id" android:layout_width="fill_parent" android:layout_height="wrap_content" android:spacing="10dip" /> <ImageSwitcher android:id="@+id/imgSwitcher_id" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
相关文章推荐
- android实现程序启动直接调用系统默认浏览器
- 使用Android自带Gallery组件实现CoverFlow,源码+解析
- Android gallery实现图片的左右循环旋转源码分享
- 使用Android自带Gallery组件实现CoverFlow
- Android编程实现Gallery中每次滑动只显示一页的方法
- Android中实现静态的默认安装和卸载应用
- Android 自定义RecyclerView 实现真正的Gallery效果
- 使用Android自带Gallery组件实现CoverFlow
- Android利用Gallery和ImageSwitcher实现在线相册图片预览功能(异步加载图片)
- Android 自定义RecyclerView 实现真正的Gallery效果
- Android Gallery组件实现循环显示图像
- Android 自定义RecyclerView 实现真正的Gallery效果
- android scrollView 内嵌ViewPager或Gallery等水平滑动控件冲突问题解决实现同时滑动
- android UI进阶之用gallery实现可滑动的Tab
- Android 自定义RecyclerView 实现真正的Gallery效果
- Android中实现静态的默认安装和卸载应用
- Android-ListView实现类似Gallery的功能
- android预置默认的语音信箱号码具体实现
- Android Gallery组件实现循环显示图像
- Android中实现静态的默认安装和卸载应用