本文主要是介绍Gallery和imageSwitcher结合使用浏览图片(简单图片浏览器),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
效果如下图,点击小图显示大图:
activity_image_switcher_and_gallery.xml中的代码如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent" ><ImageSwitcher android:id="@+id/imageswitch"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_alignParentTop="true"></ImageSwitcher><Gallery android:id="@+id/gallery"android:layout_width="fill_parent"android:layout_height="60dip"android:layout_gravity="center"android:layout_alignParentBottom="true"/>
</RelativeLayout>
ImageSwitcherAndGalleryActivity.java中的代码:
package com.bzu.imageswitcher_gallery.activity;import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;
import android.support.v4.app.NavUtils;
import android.support.v4.view.ViewPager.LayoutParams;public class ImageSwitcherAndGalleryActivity extends Activity {// 存放大图片int[] bigImage = { 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 };// 存放小图片int[] smallImage = { 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 };@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_image_switcher_and_gallery);final ImageSwitcher imageswitch = (ImageSwitcher) this.findViewById(R.id.imageswitch);Gallery gallery = (Gallery) this.findViewById(R.id.gallery);imageswitch.setFactory(new ViewFactory() {@Overridepublic View makeView() {ImageView imageView = new ImageView(ImageSwitcherAndGalleryActivity.this);imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));return imageView;}});//final ImageAdapter myImageAdapter=new ImageAdapter(this);gallery.setAdapter(new ImageAdapter(this));//imageswitch.setImageResource((int) myImageAdapter.getItemId(0));gallery.setSelection(smallImage.length/2);gallery.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> adapter, View view, int position,long id) {imageswitch.setImageResource(bigImage[position]);}});}//定制适配器private class ImageAdapter extends BaseAdapter {private Context context;int mGalleryItemBackground;public ImageAdapter(Context context) {this.context = context;TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery);mGalleryItemBackground = a.getResourceId(R.styleable.HelloGallery_android_galleryItemBackground, 0);a.recycle();}@Overridepublic int getCount() {return smallImage.length;}@Overridepublic Object getItem(int position) {return smallImage[position];}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ImageView imageView = new ImageView(ImageSwitcherAndGalleryActivity.this);imageView.setImageResource(smallImage[position]);imageView.setBackgroundResource(mGalleryItemBackground);return imageView;}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.activity_image_switcher_and_gallery,menu);return true;}}
这篇关于Gallery和imageSwitcher结合使用浏览图片(简单图片浏览器)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!