ImageView加ImageSwitch制作图片浏览器_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > ImageView加ImageSwitch制作图片浏览器

ImageView加ImageSwitch制作图片浏览器

 2013/11/7 23:15:30  lialzm  博客园  我要评论(0)
  • 摘要:Main/**图片浏览器*/publicclassMainActivityextendsActivityimplementsViewFactory{privateGallerygallery;privateImageSwitcherimageSwitcher;privateint[]imageid=newint[]{R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable
  • 标签:view imageView 图片 浏览 浏览器
Main /*
* 图片浏览器
*/

public class MainActivity extends Activity implements ViewFactory{
private Gallery gallery;
private ImageSwitcher imageSwitcher;
private int[] imageid=new int[]{R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gallery=(Gallery) findViewById(R.id.gallery1);
        imageSwitcher=(ImageSwitcher) findViewById(R.id.imageSwitcher);
        GalleryAdapter galleryAdapter=new GalleryAdapter(this, imageid);
        gallery.setAdapter(galleryAdapter);
        imageSwitcher.setFactory(this);
        //设置进入出去动画
        imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
        imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
 
        gallery.setOnItemClickListener(new OnItemClickListener() {
 
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                //position从1开始
                imageSwitcher.setImageResource(imageid[position%imageid.length]);
            }
 
        });
    }
    @Override
    public View makeView() {
        ImageView imageView=new ImageView(this);
        //设置imageswitch背景色
        imageView.setBackgroundColor(0xFF000000);
        imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        return imageView;
    }
}   Adapter   public class GalleryAdapter extends BaseAdapter{
    private Context context;
    private int[] imageid;
    int mGalleryItemBackground;
public GalleryAdapter(Context context,int[] imageid) {
    this.context=context;
    this.imageid=imageid;
    //图像的显示风格
    TypedArray typedArray = context.obtainStyledAttributes(R.styleable.Gallery);
    mGalleryItemBackground = typedArray.getResourceId(
            R.styleable.Gallery_android_galleryItemBackground, 0);
}
    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }
 
    @Override
    public Object getItem(int position) {
        return imageid[position];
    }
 
    @Override
    public long getItemId(int position) {
        return position;
    }
 
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView=new ImageView(context);
        imageView.setImageResource(imageid[position%imageid.length]);
        imageView.setBackgroundResource(mGalleryItemBackground);
        return imageView;
    }
 
}   res/values/attrs.xml   <?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="Gallery">
        <attr name="android:galleryItemBackground" />
    </declare-styleable>
</resources>   实现gallery无限循环,点击item,在imageswitch显示图片



来自为知笔记(Wiz)



发表评论
用户名: 匿名