`
flyingsir_zw
  • 浏览: 12075 次
文章分类
社区版块
存档分类
最新评论

gallery 选中图片放大缩小

阅读更多
package com.testgallery.test;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;

public class TestGallerActivity extends Activity {
		private Gallery gallery;  
	    private AnimationSet manimationSet;  
	    private int[] imgs;  
	    @Override  
	    public void onCreate(Bundle savedInstanceState) {  
	        super.onCreate(savedInstanceState);  
	        setContentView(R.layout.main);  
	        init();  
	    }  
	    private void init(){  
	        imgs=new int[]{R.drawable.a,R.drawable.c,R.drawable.b};  
	        gallery = (Gallery)findViewById(R.id.gy_main);  
	        ImageAdapter imgAdapter = new ImageAdapter(this,imgs);  
	        gallery.setAdapter(imgAdapter);  
	        gallery.setSelection(imgs.length/2); 
	        gallery.setOnItemClickListener(listener);  
	          
	    }  
	    int temp = 10;
	    private OnItemClickListener listener = new  OnItemClickListener(){  
	        @Override  
	        public void onItemClick(AdapterView<?> parent, View view, int position,  
	                long id) {  
	            if(position != temp){  
	            	temp = position;
	                AnimationSet animationSet = new AnimationSet(true);  
	                if(manimationSet!=null&&manimationSet!=animationSet){  
//	                     ScaleAnimation scaleAnimation = new ScaleAnimation(1,0.5f,1,0.5f,  
//	                             Animation.RELATIVE_TO_SELF,0.5f,   //使用动画播放图片  
//	                              Animation.RELATIVE_TO_SELF,0.5f);  
//	                            scaleAnimation.setDuration(1000);  
//	                            manimationSet.addAnimation(scaleAnimation);  
//	                            manimationSet.setFillAfter(true); //让其保持动画结束时的状态。  
//	                           view.startAnimation(manimationSet);  
	                	if(null!=manimationSet)manimationSet.setFillAfter(false);  
	                }  
	                     ScaleAnimation scaleAnimation = new ScaleAnimation(1f,1.5f,1f,1.5f,   
	                             Animation.RELATIVE_TO_SELF,0.5f,   
	                             Animation.RELATIVE_TO_SELF,0.5f);  
	                           scaleAnimation.setDuration(1000);  
	                           animationSet.addAnimation(scaleAnimation);  
	                           animationSet.setFillAfter(true);   
	                           view.startAnimation(animationSet);  
	                           manimationSet = animationSet;  
	                }else{  
	                	if(null!=manimationSet)manimationSet.setFillAfter(false);  
	                }  
	        }  
	    };  
	    class ImageAdapter extends BaseAdapter{  
	        private Context ct;  
	        private int[] data;  
	        public ImageAdapter(Context ct,int[] data){  
	            this.ct=ct;  
	            this.data=data;  
	        }  
	        @Override  
	        public int getCount() {  
	            return data.length;  
	        }  
	        @Override  
	        public Object getItem(int position) {  
	            return data[position];  
	        }  
	        @Override  
	        public long getItemId(int position) {  
	            return position;  
	        }  
	        @Override  
	        public View getView(int position, View convertView, ViewGroup parent) {  
	            View view = null;  
	            if(convertView==null){  
	                ImageView img = new ImageView(ct);  
	                img.setImageResource(data[position]);  
	                view=img;  
	            }else{  
	                view=convertView;  
	            }  
	            return view;  
	        }  
	    } 
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics