简单图片浏览器(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) {
	}
}


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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Android中,可以使用GalleryImageSwitcher来实现可左右循环滑动的图片浏览器。 首先,在布局文件中添加一个Gallery和一个ImageSwitcher: ``` <Gallery android:id="@+id/gallery" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:gravity="center_vertical" /> <ImageSwitcher android:id="@+id/imageSwitcher" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@id/gallery" /> ``` 然后,在Java代码中,需要为Gallery设置一个Adapter,并为ImageSwitcher设置一个ViewFactory。代码如下: ``` public class MainActivity extends AppCompatActivity { private Gallery mGallery; private ImageSwitcher mImageSwitcher; private int[] mImageIds = new int[]{R.drawable.image1, R.drawable.image2, R.drawable.image3}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mGallery = (Gallery) findViewById(R.id.gallery); mImageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher); mGallery.setAdapter(new ImageAdapter(this)); mImageSwitcher.setFactory(new ViewFactory() { @Override public View makeView() { ImageView imageView = new ImageView(MainActivity.this); imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); return imageView; } }); mGallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { mImageSwitcher.setImageResource(mImageIds[position % mImageIds.length]); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); } private class ImageAdapter extends BaseAdapter { private Context mContext; public ImageAdapter(Context context) { mContext = context; } @Override public int getCount() { return Integer.MAX_VALUE; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView; if (convertView == null) { imageView = new ImageView(mContext); imageView.setLayoutParams(new Gallery.LayoutParams(200, 200)); imageView.setScaleType(ImageView.ScaleType.FIT_XY); } else { imageView = (ImageView) convertView; } imageView.setImageResource(mImageIds[position % mImageIds.length]); return imageView; } } } ``` 这里的关键是设置Gallery的Adapter为一个无限循环的Adapter,以及在Gallery的OnItemSelectedListener中更新ImageSwitcher图片资源。同时,ImageAdapter负责为Gallery提供每个Item的View。 这样,我们就可以实现一个左右循环滑动的图片浏览器了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值