简单图片浏览器(ImageSwitcher和Gallery结合使用)

现在大部分的手机都有了照相功能,当我们浏览照片或图片时有没有想过做个简单的浏览器方便自己观看呢?

这里做了一个简单的图片浏览器,可以在观看大图时浏览小图;并可对小图进行放大观看;想看就看,随心所欲!

首先,对我们的浏览器做个布局吧:

<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>


可以根据喜好对小图左右移动哦!

然后就是实现方法了:

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() {
			@Override
			public void onClick(View v) {
				if(positon >= 1){
					positon = positon - 1;
					gallery.setSelection(positon);
				}
				
			}
		});
        rightbutton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (positon < smalldrawable.length-1) {
					positon = positon + 1;
					gallery.setSelection(positon);
				}
			}
		});
        
    }
    
    //重写ViewFactory中的makeView方法,对ImageSwitcher显示的ImageView对象进行了设置
	@Override
	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();
		}
    	
    	//返回图片的个数
		@Override
		public int getCount() {
			return smalldrawable.length;
		}

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

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

		@Override
		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;
		}
    	
    }

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


是不是很简单?简单的我都忘记了写博客了吐舌头,现在补上,以后可不能再忘记了。不积跬步无以至千里嘛!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值