imageswitch

1.MainActivity

package com.example.imageswitcher;

import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;

public class MainActivity extends Activity implements ViewFactory{
    Integer[]  image = {
    		R.drawable.katong1,
    		R.drawable.katong2,
    		R.drawable.katong3,
    		R.drawable.katong4,
    		R.drawable.katong5,
    };
    int count = 0;
    private ImageSwitcher   is;
    private Gallery  g;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		is = (ImageSwitcher)findViewById(R.id.imageSwitcher1);
		is.setFactory(this);
		
		is.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
		is.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
	
	    g  =  (Gallery)findViewById(R.id.gallery1);
	    
	    g.setAdapter(new ImageAdapter(this));
	    
	    g.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				is.setImageResource(image[arg2]);
			}
		});
	}
	public class ImageAdapter extends  BaseAdapter {
		
		public  Context c ;
		public int item;
		public ImageAdapter(Context c){
			this.c = c;
			TypedArray  a  = obtainStyledAttributes(R.styleable.Gallery1);
			
			item = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
			
			a.recycle();
			
		}
		
		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			// TODO Auto-generated method stub
			ImageView imageView = new  ImageView(c);
			
			imageView.setImageResource(image[arg0]);
			imageView.setScaleType(ImageView.ScaleType.FIT_XY);
			imageView.setLayoutParams(new Gallery.LayoutParams(100,120));
			imageView.setBackgroundResource(item);
			
			return imageView;
		}
		
		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}
		
		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return image.length;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	public void leftOnClick(View v){
		
			is.setImageResource(image[count]);
			count -- ;
		    if(count < 0){
		    	count = 0;
		    }
	}
	public void rightOnClick(View v){
		
			is.setImageResource(image[count]);
			count ++;
			if(count > 4){
				count = 4;
			}
		
	}

	@Override
	public View makeView() {
		// TODO Auto-generated method stub
		ImageView imageView = new  ImageView(this);
		
		imageView.setBackgroundColor(0xFF000000);
		imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
		imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
		
		return imageView;
	}
}

2.布局layout

<RelativeLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
     >
     
    <ImageSwitcher
        android:id="@+id/imageSwitcher1"
        android:layout_width="fill_parent"
        android:layout_height="300dp" >
    </ImageSwitcher>
   <LinearLayout
       android:id="@+id/linear"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@id/imageSwitcher1" 
     android:layout_centerHorizontal="true"
  
       >
      <Button 
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:onClick="leftOnClick"
         android:text="右"
          /> 
      <Button 
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:onClick="rightOnClick"
         android:text="左"
          /> 
      
   </LinearLayout>
   
   <Gallery 
       android:id="@+id/gallery1"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:layout_alignParentBottom="true"
       />
</RelativeLayout>



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值