关闭

gallery点击放大,循环内容

223人阅读 评论(0) 收藏 举报
分类:

gallery点击放大,循环内容

主要代码:

galleryAdapter.java  gallery适配器

public class galleryAdapter extends BaseAdapter{
 DisplayMetrics dm;
   static int w1;
   static int h1;
 Context mContext;
 private int selectItem = -1;
 private int drawable1[]=new int[] {R.drawable.scene_bed,R.drawable.scene_eat,R.drawable.scene_getup};
public galleryAdapter(Context mContext,int w1,int h1){
 this.mContext=mContext;
 this.w1=w1;
 this.h1=h1;
}
 @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;
 }
 public void setSelectItem(int selectItem) {
  
  if (this.selectItem != selectItem) {               
  this.selectItem = selectItem;
  notifyDataSetChanged();        
  }
 }
 

 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
  // TODO Auto-generated method stub
  ImageView imageView=new ImageView(mContext);
  
  if(selectItem==position){
     Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.hyperspace_out);
  imageView.setLayoutParams(new Gallery.LayoutParams(w1/6,(int) (h1/4.5)));
    imageView.startAnimation(animation);
    imageView.setImageResource(drawable1[position%drawable1.length]);
 
  }
  else{
         if(selectItem
          imageView.setImageResource(drawable1[position%drawable1.length]);
         }
         else{
          imageView.setImageResource(drawable1[position%drawable1.length]);
         }
   imageView.setLayoutParams(new Gallery.LayoutParams(w1/8,(int) (h1/7)));
  }
  //imageView.setScaleType(ImageView.ScaleType.MATRIX);
  
  return imageView;
 }

}
调用:

public class MainActivity extends Activity implements android.widget.AdapterView.OnItemClickListener,OnItemSelectedListener{
   
 private galleryAdapter adapter;
 DisplayMetrics dm;
   static int w1;
   static int h1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Gallery gallery=(Gallery)findViewById(R.id.gallery);
        dm=new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        w1=dm.widthPixels;
        h1=dm.heightPixels;
        adapter=new galleryAdapter(this,w1,h1);
        gallery.setAdapter(adapter);
        gallery.setSpacing(2);
        gallery.setOnItemSelectedListener(this);
        gallery.setOnItemClickListener(this);

 }



 @Override
 public void onItemSelected(AdapterView<?> parent, View view, int position,
   long id) {
  // TODO Auto-generated method stub
  
 }

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

 

 @Override
 public void onItemClick(AdapterView<?> parent, View view, int position,
   long id) {
  // TODO Auto-generated method stub
  adapter.setSelectItem(position);
  Toast.makeText(this, position%3+"", 300).show();
 }
   
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:22529次
    • 积分:185
    • 等级:
    • 排名:千里之外
    • 原创:95篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档