ImageSwitcherGallery

先来看一下运行图:

点击右移之后:

点击左移之后:

下面看一下具体的代码:

MainActivity.java

package com.example.mainactivity;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.Gallery.LayoutParams;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;

public class MainActivity extends Activity implements ViewFactory , OnItemSelectedListener{
	Gallery gallery;
	ImageSwitcher imageSwitcher;
	int [] largedrawable,smalldrawable;
	Button leftbutton,rightbutton;
	int positon;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置没有标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //设计全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //上面的设置全屏的代码一定要放在setContentView(R.layout.activity_main)的上面,否则出异常
        setContentView(R.layout.activity_main);
        
        gallery = (Gallery) findViewById(R.id.mygallery);
        
        imageSwitcher = (ImageSwitcher) findViewById(R.id.myswitcher);
        imageSwitcher.setFactory(this);
        largedrawable = new int[]{R.drawable.sample_0,R.drawable.sample_1,R.drawable.sample_2,R.drawable.sample_3,R.drawable.sample_4,R.drawable.sample_5,R.drawable.sample_6,R.drawable.sample_7};
        imageSwitcher.setImageResource(largedrawable[0]);
        
        smalldrawable = new int[]{R.drawable.sample_thumb_0,R.drawable.sample_thumb_1,R.drawable.sample_thumb_2,R.drawable.sample_thumb_3,R.drawable.sample_thumb_4,R.drawable.sample_thumb_5,R.drawable.sample_thumb_6,R.drawable.sample_thumb_7};
        
        imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
        imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
        
        gallery = (Gallery) findViewById(R.id.mygallery);
        gallery.setAdapter(new ImageAdapter(MainActivity.this));
        gallery.setOnItemSelectedListener(this);
        gallery.setSelected(true);
        positon = 5;
        gallery.setSelection(positon, true);
        
        
        leftbutton =(Button) findViewById(R.id.left);
        rightbutton = (Button) findViewById(R.id.right);
        leftbutton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if(positon >= 1){
					positon = positon - 1;
					gallery.setSelection(positon);
				}
				
			}
		});
        rightbutton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				if (positon < smalldrawable.length-1) {
					positon = positon + 1;
					gallery.setSelection(positon);
				}
			}
		});
        
    }
    
    //重写ViewFactory中的makeView方法,对ImageSwitcher显示的ImageView对象进行了设置
	public View makeView() {
		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; 
	}
	
	/**
	 * 负责产生gallery中的图片
	 */
    private class ImageAdapter extends BaseAdapter{
    	Context context;
    	int mygalleryItemBackground;
    	public ImageAdapter(Context context) {
    		this.context = context;
    		
    		//加边框
    		TypedArray typedArray = obtainStyledAttributes(R.styleable.HelloGallery);
    		mygalleryItemBackground = typedArray.getResourceId(R.styleable.HelloGallery_android_galleryItemBackground, 0);
    		typedArray.recycle();
		}
    	
    	//返回图片的个数
		public int getCount() {
			return smalldrawable.length;
		}

		public Object getItem(int position) {
			return smalldrawable[position];
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView imageView = new ImageView(context);
			imageView.setImageResource(smalldrawable[position]);	 // 设置imageView中的图像资源
			imageView.setAdjustViewBounds(true);           			 // 设置图像大小尺寸自适应
			imageView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			imageView.setBackgroundResource(mygalleryItemBackground);
			return imageView;
		}
    	
    }

	public void onItemSelected(AdapterView<?> arg0, View arg1, int position,
			long arg3) {
		imageSwitcher.setImageResource(largedrawable[position]);
	}
	public void onNothingSelected(AdapterView<?> arg0) {
	}
}


布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="vertical">

	<ImageSwitcher
	    android:id="@+id/myswitcher"
	    android:layout_width="fill_parent"
	    android:layout_height="450dp"
	    ></ImageSwitcher>

	<LinearLayout 
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:orientation="horizontal"
	    >
	    <Button 
	        android:id="@+id/left"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="左移"
	        />
		<Gallery 
		    android:id="@+id/mygallery"
		    android:layout_width="220dp"
		    android:layout_height="60dp"
		    android:spacing="10dp"
		    />
		 <Button 
		    android:id="@+id/right"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="右移"
	        />  
	</LinearLayout>
</LinearLayout>


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值