该例子来自APIDemo
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.apis.view;
import com.example.android.apis.R;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.Gallery.LayoutParams;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;
public class ImageSwitcher1 extends Activity implements
AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.image_switcher_1);
mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
//提供显示图片的地方
mSwitcher.setFactory(this);
//系统的anim中的fade_in.xml
mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
//系统的anim中的fade_out.xml
mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
g.setOnItemSelectedListener(this);
}
public void onItemSelected(AdapterView parent, View v, int position, long id) {
mSwitcher.setImageResource(mImageIds[position]);
}
public void onNothingSelected(AdapterView parent) {
}
//设置ImgaeSwitcher的为IamgeView的变化并且设置其属性,除此之外可以设置第一次加载的时候ImageSwitcher显示的图片默认第一张
public View makeView() {
ImageView i = new ImageView(this);
i.setBackgroundColor(0xFF000000);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
return i;
}
private ImageSwitcher mSwitcher;
public class ImageAdapter extends BaseAdapter {
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
i.setImageResource(mThumbIds[position]);
i.setAdjustViewBounds(true);
i.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
i.setBackgroundResource(R.drawable.picture_frame);
return i;
}
private Context mContext;
}
private Integer[] mThumbIds = {
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};
private Integer[] mImageIds = {
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};
}
//frameworks\base\core\res\res\anim\fade_in.xml
<alpha xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@anim/accelerate_interpolator"
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:duration="@android:integer/config_mediumAnimTime"
/>
//frameworks\base\core\res\res\anim\fade_out.xml
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@anim/decelerate_interpolator"
android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="@android:integer/config_longAnimTime" />
显示效果
总结
TextSwitcher与ImageSwitcher差不多,不过ImageSwitcher规定的view为ImageView,而TextSwitcher为TextView。(这个在public View makeView()这个里面设置)。若是ImageSwitcher则在makeView()里面定义的是IamgeView并且将其返回给IamgeSwitcher, 而TextSwitcher则在makeView里面定义的是TextView并且将其返回给TextSwitcher.
java.lang.Object
↳ android.view.View
↳ android.view.ViewGroup
↳ android.widget.FrameLayout
↳ android.widget.ViewAnimator
↳ android.widget.ViewSwitcher
↳ android.widget.TextSwitcher
java.lang.Object
↳ android.view.View
↳ android.view.ViewGroup
↳ android.widget.FrameLayout
↳ android.widget.ViewAnimator
↳ android.widget.ViewSwitcher
↳ android.widget.ImageSwitcher
![点击查看原始大小图片](http://dl2.iteye.com/upload/attachment/0021/7896/9dfb9bda-5ad7-3075-b6eb-d59f42af67d7-thumb.png)
- 大小: 86.7 KB
分享到:
相关推荐
android的图片查看器 ImageSwitcher组件,可直接导入使用
android Gallery + ImageSwitcher
android ImageSwitcher 简单实现图片定时切换
使用android中的ImageSwitcher 简单的实现了图片浏览的功能
自定义ImageSwitcher实现更新图片切换效果
其实各种切换特效都能用Android的属性动画来做的,我就如此简单做了一个,目前有3种特效:水平百叶窗、垂直百叶窗和马赛克,其实懂得这个原理做任何特效都是可以的。 为方便各位童鞋看到运行效果,我上传的是完整的...
Gallery和ImageSwitcher同步自动(滚动)播放图片库
主要介绍了Android之ImageSwitcher的实例详解的相关资料,这里提供实例帮助大家理解这个控件的功能,希望能帮助到大家,需要的朋友可以参考下
GridView+ImageSwitcher实现的图片切换(左滑右滑切换)
ImageSwitcher.zip,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
图像切换器(ImageSwitcher),用于实现类似于Windows操作系统的“Windows照片查看器”中的上一张、下一张切换图片的功能。在使用ImageSwitcher时,必须实现ViewSwitcher.ViewFactory接口,并通过makeView()方法来...
ImageSwitcherDemoAndroid ImageSwitcher component sampleAndroid中的ImageSwitcher控件的使用示例,使用ImageSwitcher控件来实现图片的左右滑动功能
简单的android ImageSwitcher实例
仿百叶窗效果代码DEMO 简单效果 体验代码
Android 控件ImageSwitcher 图片切换器
jquery写的一个图片左右切换效果,浏览器兼容,图片下方有缩略图显示
这是一个基于ImageSwitcher的Android简易图片播放器,适合初学者学习参考。
a simple sample for imageSwitcher
安卓ImageSwitcher组件实例