android--图片的水平自动切换

实现该功能用到了Gallery组件,该组件能够水平方向显示图片内容,并可以拖动;

主要属性:

    

创建一个BaseAdapter类对象,重写其中的方法

	private void ShowGallery() {
		// TODO Auto-generated method stub
		myGallery = (Gallery) findViewById(R.id.gallery1);
		

		BaseAdapter mybaseAdapter = new BaseAdapter() {
			
			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				// TODO Auto-generated method stub
				int pos;
				ImageView imageView = new ImageView(MainActivity.this);
				imageView.setBackgroundColor(0xFFFFFFFF);
				imageView.setScaleType(ImageView.ScaleType.FIT_XY);//设置缩放方式
				imageView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));//图片显示高和宽
				if(position>=len){
					pos=(position%len);
				}else{
					pos=position;
				}
				imageView.setImageResource(myimage[pos]);
				return imageView;
			}
			
			@Override
			public long getItemId(int position) {
				// TODO Auto-generated method stub
				if(position>=len){
					return position%len;
				}else{
					return position;
				}
			}
			
			@Override
			public Object getItem(int position) {
				// TODO Auto-generated method stub
				if(position>=len){
					return position%len;
				}else{
					return position;
				}
			}
			
			@Override
			public int getCount() {
				// TODO Auto-generated method stub
				return Integer.MAX_VALUE;//返回Integer的最大值为了无限循环,上面的取余也是
			}
		};
		myGallery.setAdapter(mybaseAdapter);//关联
		 CutImage();//切换图片
	}
	private void ShowGallery() {
		// TODO Auto-generated method stub
		myGallery = (Gallery) findViewById(R.id.gallery1);
		

		BaseAdapter mybaseAdapter = new BaseAdapter() {
			
			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				// TODO Auto-generated method stub
				int pos=position;
				
				Log.i("hany", ""+position);
				ImageView imageView = new ImageView(MainActivity.this);
			if(convertView==null){
				imageView.setBackgroundColor(0xFFFFFFFF);
				imageView.setScaleType(ImageView.ScaleType.FIT_XY);
				imageView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
				if(position>=len){
					pos=(position%len);
				}else{
					pos=position;
				}
			}else{
					imageView=(ImageView) convertView;
				}
				imageView.setImageResource(myimage[pos]);
				SmallImageUpdate(pos);
				return imageView;
			}
			
			@Override
			public long getItemId(int position) {
				// TODO Auto-generated method stub
				if(position>=len){
					return position%len;
				}else{
					return position;
				}
			}
			
			@Override
			public Object getItem(int position) {
				// TODO Auto-generated method stub
				if(position>=len){
					return position%len;
				}else{
					return position;
				}
			}
			
			@Override
			public int getCount() {
				// TODO Auto-generated method stub
				return Integer.MAX_VALUE;
			}
		};
		myGallery.setAdapter(mybaseAdapter);//关联
		CutImage();//切换图片

 CutImage()利用计时器反复执行某个动作 
  

	private void  CutImage() {
		// TODO Auto-generated method stub
		Getnowdate();

		mytime= new Timer();//一种计时器详见android--Timer
		mytask = new TimerTask() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				Message m = new Message();
				m.what=1;
				myhandler.sendMessage(m);
			}
		};
		
		myhandler = new Handler(){

			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				if(msg.what==1){
					//Getnowdate();
					UpdateImage();
				}
				
			}


			
		};
		
		mytime.schedule(mytask, 1000, 4000);
	}			
UpdateImage():

	private void SmallImageUpdate(int index){
		Reset();
		ImageView imageView;
		imageView = (ImageView) findViewById(Smallimage[index]);
		imageView.setBackgroundResource(R.drawable.gray2);
		myGallery.setSelection(index);
	}


 
  
	private void Reset(){
		ImageView view1 = (ImageView) findViewById(R.id.image1);
		ImageView view2 = (ImageView) findViewById(R.id.image2);
		ImageView view3 = (ImageView) findViewById(R.id.image3);
		ImageView view4 = (ImageView) findViewById(R.id.image4);
		ImageView view5 = (ImageView) findViewById(R.id.image5);
		view1.setBackgroundResource(R.drawable.white);
		view2.setBackgroundResource(R.drawable.white);
		view3.setBackgroundResource(R.drawable.white);
		view4.setBackgroundResource(R.drawable.white);
		view5.setBackgroundResource(R.drawable.white);
	}


 
  


android--图片的水平自动切换_第1张图片


你可能感兴趣的:(android)