
大家好,又见面了,我是全栈君。
ImageSwitcher组件的主要功能是完毕图片的切换显示,比如用户在进行图片浏览的时候。能够通过button点击一张张的切换显示的图片,并且使用ImageSwitcher组件在每次切换的时候也能够为其添加一些动画的效果,此类定义例如以下:
java.lang.Object ↳ android.view.View ↳ android.view.ViewGroup ↳ android.widget.FrameLayout ↳ android.widget.ViewAnimator ↳ android.widget.ViewSwitcher ↳ android.widget.ImageSwitcher
用到的方法
1 | public ImageSwitcher(Context context) | 构造 | 创建ImageSwitcher对象 |
|---|---|---|---|
2 | public void setFactory(ViewSwitcher.ViewFactory factory) | 普通 | 设置ViewFactory对象。用于完毕两个图片切换时ViewSwitcher的转换操作 |
3 | public void setImageResource(int resid) | 普通 | 设置显示的图片资源ID |
4 | public void setInAnimation(Animation inAnimation) | 普通 | 图片读取进ImageSwitcher时的动画效果 |
5 | public void setOutAnimation(Animation outAnimation) | 普通 | 图片从ImageSwitcher要消失时的动画效果 |
假设要想实现图片的切换功能。则定义的Activity类还必须实现ViewSwitcher.ViewFactory接口,以指定切换视图的操作工厂,此接口定义例如以下:
public
static
interface ViewSwitcher.ViewFactory {
/**
*
创建一个新的
View
显示,并将其增加到
ViewSwitcher
之中
*
@return
新的
View
对象
*/
public
abstract View makeView() ;
}
如
private
class ViewFactoryImpl
implements ViewFactory {
@Override
public View makeView() {
ImageView img =
new ImageView(MyImageSwitcherDemo.
this);
//
实
例化图片显示
img.setBackgroundColor(0xFFFFFFFF);
//
设置背景颜色
img.setScaleType(ImageView.ScaleType.
CENTER);
//
居中显示
img.setLayoutParams(
new ImageSwitcher.LayoutParams(
//
自
适应图片大小
LayoutParams.
FILL_PARENT, LayoutParams.
FILL_PARENT));
//
定义组件
return img;
}
} XMl文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <ImageSwitcher android:id="@+id/imageSwitcher1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="114dp" > </ImageSwitcher> <Button android:id="@+id/button1" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button2" android:layout_alignBottom="@+id/button2" android:layout_marginRight="20dp" android:layout_toLeftOf="@+id/imageSwitcher1" android:text="上一张" /> <Button android:id="@+id/button2" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toRightOf="@+id/imageSwitcher1" android:text="下一张" /></RelativeLayout>JAVA文件
package com.example.imageswitcher;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;
public class MainActivity extends Activity {
private Button ButNext, ButPrevious;//初始化button
private ImageSwitcher imageSwitcher;//初始化组件
private int Images[] = { R.drawable.a1, R.drawable.a2, R.drawable.a3,
R.drawable.a4, R.drawable.a5, R.drawable.a6 };//设置图片数据
private int foot = 0;//设置角标
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageSwitcher = (ImageSwitcher) this.findViewById(R.id.imageSwitcher1);//获得组件
ButNext = (Button) this.findViewById(R.id.button1);
ButPrevious = (Button) this.findViewById(R.id.button2);
imageSwitcher.setFactory(new Myfactory());//为组件设置组件工厂
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(
MainActivity.this, android.R.anim.fade_in));//设置图片进入动画
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(
MainActivity.this, android.R.anim.fade_out));//设置图片离开动画
imageSwitcher.setImageResource(Images[foot++]);//设置图片
// button事件监听
ButNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
imageSwitcher.setImageResource(Images[foot++]);
MainActivity.this.CheckEnable();//设置button是否可用防止数组越界
}
});
// button事件监听
ButPrevious.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
imageSwitcher.setImageResource(Images[foot--]);
MainActivity.this.CheckEnable();
}
});
}
class Myfactory implements ViewFactory {
@Override
public View makeView() {
// TODO Auto-generated method stub
ImageView image = new ImageView(MainActivity.this);//设置图片组件
image.setBackgroundColor(Color.GRAY);//设置对齐效果
image.setScaleType(ImageView.ScaleType.CENTER);//设置剧中
image.setLayoutParams(new ImageSwitcher.LayoutParams( // 自适应图片大小
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); // 定义组件
return image;//返回图片
}
}
public void CheckEnable() {
if (this.foot < this.Images.length - 1) {
this.ButNext.setEnabled(true); // button可用
} else {
this.ButNext.setEnabled(false); // button不可用
}
if (this.foot == 0) {
this.ButPrevious.setEnabled(false); // button不可用
} else {
this.ButPrevious.setEnabled(true); // button可用
}
}
}

Textswitcher与该组件的操作基本同样。不再做详细介绍,读者可自行练习
下节预报:
gallery拖拉组件
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/116612.html原文链接:https://javaforall.cn