android之ImageSwitcher 图片查看

布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <ImageSwitcher android:id="@+id/switcher"
    	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_alignParentLeft="true"
    	android:layout_height="60dp"
    	android:spacing="15dp"
    	android:layout_alignParentBottom="true"
    	android:gravity="center_vertical"
    	android:background="#aaaaaa">
    </Gallery>
</RelativeLayout>

主程序如下:

package com.cloay.imageswitcher;

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

/**
 * ImageSwitcherActivity.java
 * @author cloay
 * 2011-7-16
 */
public class ImageSwitcherActivity extends Activity implements OnItemSelectedListener, ViewFactory{
	private ImageSwitcher imageSwitcher;
	private Gallery gallery;
	private Integer [] imagesId = new Integer[]{R.drawable.b, R.drawable.c, R.drawable.d, 
			R.drawable.f, R.drawable.g};
	private Integer [] selectId = new Integer[]{R.drawable.b, R.drawable.c, R.drawable.d, 
			R.drawable.f, R.drawable.g};
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        imageSwitcher = (ImageSwitcher)findViewById(R.id.switcher);
        imageSwitcher.setFactory(this);
        //设置图片切换时的动画效果
        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.gallery);
        //自定义ImageAdapter继承于BaseAdapter,是一个内部类
        gallery.setAdapter(new ImageAdapter(this));
        gallery.setOnItemSelectedListener(this);
    }

	@Override
	public View makeView() {
		ImageView image = new ImageView(this);
		image.setScaleType(ImageView.ScaleType.FIT_CENTER);
		image.setLayoutParams(new ImageSwitcher.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.MATCH_PARENT));
		return image;
	}

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

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		
	}
	public class ImageAdapter extends BaseAdapter{
		private Context context;
		int galleryItemBackground;
		public ImageAdapter (Context c){
			context = c;
			TypedArray typeArray = obtainStyledAttributes(R.styleable.Gallery1);
			galleryItemBackground = typeArray.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
			typeArray.recycle();
		}
		@Override
		public int getCount() {
			//返回selectId[]的长度
			return selectId.length;
		}

		@Override
		public Object getItem(int position) {
			return 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(selectId[position]);
			imageView.setAdjustViewBounds(true);  //允许调整边框
			//设定底部画廊,自适应大小
			imageView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			//设置画廊背景
			imageView.setBackgroundResource(galleryItemBackground);
			return imageView;
		}
		
	}
}

说明:转载请注明出处!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是在Android Studio中实现图片切换的步骤: 1. 在res/layout文件夹下创建一个新的XML布局文件,例如image_switcher.xml,用于显示ImageSwitcher控件。 2. 在image_switcher.xml文件中添加以下代码: ```xml <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageSwitcher android:id="@+id/image_switcher" android:layout_width="match_parent" android:layout_height="match_parent" /> </FrameLayout> ``` 3. 在MainActivity.java文件中添加以下代码: ```java public class MainActivity extends AppCompatActivity { private ImageSwitcher imageSwitcher; private int[] images = {R.drawable.image1, R.drawable.image2, R.drawable.image3}; private int currentIndex = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.image_switcher); imageSwitcher = findViewById(R.id.image_switcher); imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() { @Override public View makeView() { ImageView imageView = new ImageView(getApplicationContext()); imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); return imageView; } }); imageSwitcher.setImageResource(images[currentIndex]); imageSwitcher.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { currentIndex++; if (currentIndex == images.length) { currentIndex = 0; } imageSwitcher.setImageResource(images[currentIndex]); } }); } } ``` 4. 运行应用程序,单击ImageSwitcher控件即可切换图片
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值