直奔主题~!

结构如图:

main.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent" android:gravity="center">
	<ImageSwitcher android:id="@+id/is"
		android:layout_width="300dip" android:layout_height="300dip"></ImageSwitcher>
	<Gallery android:layout_width="fill_parent" android:id="@+id/gl"
		android:layout_height="wrap_content" android:spacing="5dip"></Gallery>
</LinearLayout>

Control_Gallery_ImageSwitcher .java代码:

public class Control_Gallery_ImageSwitcherActivity extends Activity {
	private ImageSwitcher is;
	private Gallery gl;
	int[] imggroup;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        findAll();
        bind();
    }
    
    public void findAll()
    {
    	is=(ImageSwitcher) this.findViewById(R.id.is);
      	gl=(Gallery) this.findViewById(R.id.gl);
    	imggroup=new int[]{R.raw.jwc1,R.raw.jwc2,R.raw.jwc3,R.raw.jwc4,R.raw.jwc5};
    }
    
    public void bind()
    {
    	is.setFactory(new ViewFactory() {
			
			public View makeView() {
				// TODO Auto-generated method stub
				//return null;
				ImageView iv=new ImageView(Control_Gallery_ImageSwitcherActivity.this);
				iv.setLayoutParams(new  ImageSwitcher.LayoutParams(100, 100));
				return iv;
			}
		});
    	is.setAnimation(AnimationUtils.loadAnimation(Control_Gallery_ImageSwitcherActivity.this, android.R.anim.fade_in));
    	is.setAnimation(AnimationUtils.loadAnimation(Control_Gallery_ImageSwitcherActivity.this, android.R.anim.fade_out));
        is.setBackgroundResource(imggroup[0]);
        gl.setAdapter(new myadpter(Control_Gallery_ImageSwitcherActivity.this));
        gl.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				is.setBackgroundResource(imggroup[arg2]);
			}
		});
        
    }
    
    class myadpter extends BaseAdapter
    {
    	Context ct;
    	
    	public myadpter(Context ct)
    	{
    		this.ct=ct;
    	}

		public int getCount() {
			// TODO Auto-generated method stub
			return imggroup.length;
		}

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

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

		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			//return null;
		    ImageView iv=new ImageView(ct);
		    iv.setImageBitmap(BitmapFactory.decodeResource(getResources(), imggroup[position])) ;
		    iv.setLayoutParams(new Gallery.LayoutParams(80, 80));
			return iv;
		}
    	
    }
}

 

posted on 2011-09-14 16:57  Jwc  阅读(373)  评论(0编辑  收藏  举报