控件:图片切换 --- ImageSwitcher

◆ 掌握ImageSwitcher和VIewFactory的使用;
◆ 了解Animation的基本使用。

 

如果要想真正的实现图片的切换操作,那么问题的关键就在于ViewFactory工厂的使用上。
在这个接口中有一个操作方法:

public abstract View makeView()

这个方法的作用是创建一个View显示的组件。

View Code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id
="@+id/MyLayout"
android:orientation
="vertical"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent">
<ImageSwitcher
android:id="@+id/myImageSwitcher"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"/>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation
="horizontal"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent">
<Button
android:id="@+id/butPrevious"
android:text
="上一张图片"
android:enabled
="false"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"/>
<Button
android:id="@+id/butNext"
android:text
="下一张图片"
android:enabled
="true"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"/>
</LinearLayout>
</LinearLayout>
View Code
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ViewSwitcher.ViewFactory;

public class MyImageSwitcherDemo extends Activity {
// 图片切换
private ImageSwitcher myImageSwitcher = null;
// 按钮组件
private Button butPrevious = null;
// 按钮组件
private Button butNext = null;
// 资源图片ID
private int[] imgRes = new int[] {
R.drawable.ispic_a, R.drawable.ispic_b,
R.drawable.ispic_c, R.drawable.ispic_d,
R.drawable.ispic_e };
// 资源读取位置
private int foot = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 调用布局管理器
super.setContentView(R.layout.main);
// 取得组件
this.myImageSwitcher = (ImageSwitcher) super.findViewById(R.id.myImageSwitcher);
// 取得组件
this.butPrevious = (Button) super.findViewById(R.id.butPrevious);
// 取得组件
this.butNext = (Button) super.findViewById(R.id.butNext) ;
// 设置转换工厂
this.myImageSwitcher.setFactory(new ViewFactoryImpl());
// 设置动画
this.myImageSwitcher.setInAnimation(AnimationUtils.loadAnimation(
this,
android.R.anim.fade_in)
);
// 设置动画
this.myImageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(
this,
android.R.anim.fade_out)
);
// 设置图片
this.myImageSwitcher.setImageResource(imgRes[foot++]) ;
// 设置单击事件
this.butNext.setOnClickListener(new OnClickListenerNext()) ;
// 设置单击事件
this.butPrevious.setOnClickListener(new OnClickListenerPrevious()) ;
}

private class OnClickListenerPrevious implements OnClickListener {
@Override
public void onClick(View v) {
// 修改显示图片
MyImageSwitcherDemo.this.myImageSwitcher.setImageResource(imgRes[foot--]);
// 设置按钮状态
MyImageSwitcherDemo.this.checkButEnable();
}
}

private class OnClickListenerNext implements OnClickListener {
@Override
public void onClick(View v) {
// 修改显示图片
MyImageSwitcherDemo.this.myImageSwitcher.setImageResource(imgRes[foot++]);
// 设置按钮状态
MyImageSwitcherDemo.this.checkButEnable();
}
}

// 设置按钮状态
public void checkButEnable() {
if (this.foot < this.imgRes.length - 1) {
// 按钮可用
this.butNext.setEnabled(true);
} else {
// 按钮不可用
this.butNext.setEnabled(false);
}
if (this.foot == 0) {
this.butPrevious.setEnabled(false); // 按钮不可用
} else {
this.butPrevious.setEnabled(true); // 按钮可用
}
}

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;
}
}
}

 

View Code
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package
="org.lxh.demo"
android:versionCode
="1"
android:versionName
="1.0">
<uses-sdk android:minSdkVersion="3" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MyImageSwitcherDemo"
android:label
="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>
</manifest>

 

         



 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted on 2012-03-04 13:02  大米稀饭  阅读(584)  评论(0编辑  收藏  举报