现在的位置: 首页 > 综合 > 正文

ImageSwitcher , Gallery的使用

2013年10月18日 ⁄ 综合 ⁄ 共 2828字 ⁄ 字号 评论关闭

做相册,这个类必须的,呵呵

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.Gallery.LayoutParams;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;

public class GalleryActivity extends Activity implements AdapterView.OnItemSelectedListener,ViewSwitcher.ViewFactory{
    /** Called when the activity is first created. */
private Gallery gallery;
private ImageSwitcher mSwitcher;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题
        setContentView(R.layout.main);
         mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
         gallery = (Gallery) findViewById(R.id.gallery);
         

         mSwitcher.setFactory(this);  / 监听/makeView方法。

       //设置动画

         mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
                 android.R.anim.fade_in));
         mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
                 android.R.anim.fade_out));
       //设置数据适配器
        gallery.setAdapter(new ImageAdapter(this));
        gallery.setOnItemSelectedListener(this);
    }
    public class ImageAdapter extends BaseAdapter{
    Context context;
   
    public ImageAdapter(Context context) {
    this.context = context;
    }
@Override
public int getCount() {
// TODO Auto-generated method stub

return mImageIds.length;
}

@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}

@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}

@Override
public View getView(int position, View view, ViewGroup parent) {
// TODO Auto-generated method stub

ImageView imageview  = new ImageView(context); 

imageview.setImageResource(mImageIds[position]);

imageview.setAdjustViewBounds(true);

                       //没有默认布局,所以下面的设置必须有

imageview.setScaleType(ImageView.ScaleType.FIT_XY);

imageview.setLayoutParams(new Gallery.LayoutParams(
                      LayoutParams.WRAP_CONTENT, 
                      LayoutParams.WRAP_CONTENT));
return imageview;
}
   
    }
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
mSwitcher.setImageResource(mImageIds[position]);

}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}
@Override
public View makeView() {
// TODO Auto-generated method stub
ImageView i =  new ImageView(this);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);

i.setLayoutParams(new ImageSwitcher.LayoutParams(
        LayoutParams.FILL_PARENT,
                LayoutParams.FILL_PARENT));
return i;
}
private Integer[] mImageIds = {
       R.drawable.photo1,
       R.drawable.photo2,
       R.drawable.photo3,
       R.drawable.photo4,
       R.drawable.photo5,
       R.drawable.photo6,
       R.drawable.photo7,
       R.drawable.photo8
};
}

抱歉!评论已关闭.