android中画廊视图Gallery和ImageSwitcher组件的使用

随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)

Gallery能够水平方向显示其内容,一般用来浏览图片,被选中的选项位于中间,并且可以响应事件显示信息,下面结合ImageSwitcher组件来实现一个通过缩略图来浏览照片的程序,代码如下:

Activity:

package com.lovo;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;

public class TestGalleryActivity extends Activity {
	// 声明ImageSwitcher
	private ImageSwitcher mSwitcher;
	// 图片数组ID
	private int[] m = { R.drawable.image1, R.drawable.image2,
			R.drawable.image3, R.drawable.image4, R.drawable.image5,
			R.drawable.image6, R.drawable.image7 };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// 设置窗口特征无标题
		// requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		// 获得ImageSwitcher对象
		mSwitcher = (ImageSwitcher) findViewById(R.id.swither);
		// 为ImageSwitcher设置工厂
		mSwitcher.setFactory(new ViewFactory() {
			@Override
			public View makeView() {
				// 创建imageView对象
				ImageView imageView = new ImageView(TestGalleryActivity.this);
				// 设置背景颜色
				imageView.setBackgroundColor(0xFF000000);
				// 设置精度类型
				imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
				// 设置布局参数
				imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
						LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
				return imageView;

			}
		});
		// 设置出现和离开的动画效果
		mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
				android.R.anim.fade_in));
		mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
				android.R.anim.fade_out));
		// 获得Gallery对象
		Gallery gallery = (Gallery) findViewById(R.id.gallery);
		gallery.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// 设置图片资源
				mSwitcher.setImageResource(m[arg2]);
			}
		});
		// 为Gallery添加适配器
		gallery.setAdapter(new ImageAdapter(this));

	}

	class ImageAdapter extends BaseAdapter {
		private Context mContext;

		public ImageAdapter(Context context) {
			mContext = context;
		}

		@Override
		public int getCount() {
			return m.length;
		}

		@Override
		public Object getItem(int arg0) {
			return arg0;
		}

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

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			// 实例化ImageView对象
			ImageView imageView = new ImageView(mContext);
			// 设置图片资源
			imageView.setImageResource(m[arg0]);
			// 设置边界对齐
			imageView.setAdjustViewBounds(true);
			// 设置布局参数
			imageView.setLayoutParams(new Gallery.LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			// 设置背景资源
			imageView.setBackgroundResource(R.drawable.about_background_land);
			return imageView;
		}
	}
}


布局XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageSwitcher
        android:id="@+id/swither"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ImageSwitcher>

    <Gallery
        android:id="@+id/gallery"
        android:layout_width="fill_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:background="#55000000"
        android:gravity="center_vertical"
        android:spacing="16dp" >
    </Gallery>

</RelativeLayout>


 附上图片效果:

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

u010142437

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值