博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
阅读《Android 从入门到精通》(24)——切换图片
阅读量:6701 次
发布时间:2019-06-25

本文共 4087 字,大约阅读时间需要 13 分钟。

切换图片(ImageSwitcher)

java.lang.Object;

android.view.View;
android.widget.ViewGroup;
android.widget.FrameLayout;
android.widget.ViewAnimator;
android.widget.ImageSwitcher;

ImageSwitcher 提供了显示图片以及图片切换的动画,这一块能够应用于制作动感影集。

ImageSwitcher 类方法

ImageSwitcher 和 Gallery 演示样例

完整project:

ViewFactory下述project中主要演示了 ImageSwitcher 和 Gallery 的使用方法,须要注意的是:ViewFactory.makeView 的实现中,ImageView 所要设置的布局类型是 FrameLayout.LayoutParams,除此之外,其它地方出现要设置的參数均为 Gallery.LayoutParams。否则会导致崩溃!

此外还能够学习下,Window 相关的属性

1.MainActivity.java

package com.sweetlover.activity;import com.sweetlover.imageswitcher.R;import android.app.Activity;import android.content.Context;import android.graphics.Color;import android.os.Bundle;import android.view.Gravity;import android.view.View;import android.view.ViewGroup;import android.view.Window;import android.view.animation.AnimationUtils;import android.widget.AdapterView;import android.widget.AdapterView.OnItemSelectedListener;import android.widget.FrameLayout;import android.widget.Gallery.LayoutParams;import android.widget.ImageView;import android.widget.ImageView.ScaleType;import android.widget.ViewSwitcher.ViewFactory;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageSwitcher;@SuppressWarnings("deprecation")public class MainActivity extends Activity implements ViewFactory,		OnItemSelectedListener {	public class SelfAdapter extends BaseAdapter {		private Context context = null;				public SelfAdapter(Context context) {			super();			this.context = context;		}		@Override		public int getCount() {			// TODO Auto-generated method stub			return pic.length;		}		@Override		public Object getItem(int position) {			// TODO Auto-generated method stub			return pic[position];		}		@Override		public long getItemId(int position) {			// TODO Auto-generated method stub			return position;		}		@Override		public View getView(int position, View convertView, ViewGroup parent) {			// TODO Auto-generated method stub			ImageView imageView = new ImageView(context);			LayoutParams param = new LayoutParams(WIDTH, HEIGHT);			imageView.setImageResource(pic[position]);			imageView.setAdjustViewBounds(true);			imageView.setLayoutParams(param);			return imageView;		}	}	private Gallery gallery = null;	private SelfAdapter selfAdapter = null;	private ImageSwitcher imageSwitcher = null;	private FrameLayout.LayoutParams param = null;		private static final int WIDTH = 480;	private static final int HEIGHT = 640;	private static final int BACKGND_COLOR = Color.BLACK;	private static final int IN_ANIM = android.R.anim.fade_in;	private static final int OUT_ANIM = android.R.anim.fade_out;	private static final Integer[] pic = { R.drawable.pic1, R.drawable.pic2,			R.drawable.pic3 };	@Override	protected void onCreate(Bundle savedInstanceState) {		// TODO Auto-generated method stub		super.onCreate(savedInstanceState);		requestWindowFeature(Window.FEATURE_NO_TITLE);		setContentView(R.layout.activity_main);		gallery = (Gallery) findViewById(R.id.gallery);		imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);		selfAdapter = new SelfAdapter(this);		imageSwitcher.setFactory(this);		imageSwitcher.setInAnimation(AnimationUtils				.loadAnimation(this, IN_ANIM));		imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,				OUT_ANIM));		gallery.setAdapter(selfAdapter);		gallery.setOnItemSelectedListener(this);	}	@Override	public void onItemSelected(AdapterView
parent, View view, int position, long id) { // TODO Auto-generated method stub imageSwitcher.setImageResource(pic[position]); } @Override public void onNothingSelected(AdapterView

> parent) { // TODO Auto-generated method stub } @Override public View makeView() { // TODO Auto-generated method stub ImageView imageView = new ImageView(this); param = new FrameLayout.LayoutParams(WIDTH, HEIGHT, Gravity.CENTER); imageView.setBackgroundColor(BACKGND_COLOR); imageView.setScaleType(ScaleType.CENTER); imageView.setLayoutParams(param); return imageView; } }

2.activity_main.xml

3.AndroidManifest.xml

你可能感兴趣的文章
神奇的pdfkit工具——将字符串保存为pdf文件
查看>>
Oracle的resetlogs机制浅析
查看>>
AIX samba服务器配置
查看>>
Think in AngularJS:对比jQuery和AngularJS的不同思维模式
查看>>
redhat oracle 开机自启动
查看>>
安装iproute2
查看>>
删除rz上传失败乱码的文件
查看>>
zabbix4
查看>>
10种顶级javascript框架比较-The Top 10 Javascript MVC Frameworks
查看>>
HAproxy + Keepalive实现LDAP代理服务
查看>>
plone进行 用户和权限管理
查看>>
利用ACS来实现AAA服务
查看>>
VMware Workstation 8下Ubuntu 13.04中安装VMware Tools出错
查看>>
Tokyo Tyrant安装和配置
查看>>
php调试
查看>>
轻松获知数据库事务
查看>>
linux top命令详解
查看>>
Weblogic的管理服务器与受管服务器
查看>>
国内开源镜像站
查看>>
Eclipse下的项目管理插件介绍
查看>>