MyDemo.java
package com.jackie.imageswitcherproject;
import android.R.color;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
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.ImageView.ScaleType;
import android.widget.ViewSwitcher.ViewFactory;
public class MainActivity<MyDemo> extends Activity {
private Button butPrevious=null;
private Button butNext=null;
private ImageSwitcher myImageSwitcher=null;
private int imgRes[]=new int[]{R.drawable.pic_2,R.drawable.pic_3,
R.drawable.pic_4,R.drawable.pic_5,R.drawable.pic_7};
private int foot=0; //表示当前已经显示的数组图片的脚标
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_main);
this.butPrevious=(Button) super.findViewById(R.id.butPrevious);
this.butNext=(Button) super.findViewById(R.id.butNext);
this.myImageSwitcher=(ImageSwitcher) super.findViewById(R.id.myImageSwitcher);
this.myImageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));
this.myImageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
this.myImageSwitcher.setFactory(new ViewFactoryImpl());
this.myImageSwitcher.setImageResource(MainActivity.this.imgRes[this.foot++]);
this.butPrevious.setOnClickListener(new OnClickListenerPrevious());
this.butNext.setOnClickListener(new OnClickListenerNext());
}
private class ViewFactoryImpl implements ViewFactory{
@Override
public View makeView() {
ImageView img=new ImageView(MainActivity.this);
img.setBackgroundColor(color.black);
img.setScaleType(ImageView.ScaleType.CENTER);
img.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
return img;
}}
private class OnClickListenerPrevious implements OnClickListener{
@Override
public void onClick(View arg0) {
MainActivity.this.checkButEnable();
if(foot==imgRes.length){
foot=imgRes.length-1;
}
MainActivity.this.myImageSwitcher
.setImageResource(MainActivity.this.imgRes[MainActivity.this.foot--]);
}
}
private class OnClickListenerNext implements OnClickListener{
@Override
public void onClick(View arg0) {
MainActivity.this.checkButEnable();
if(foot==-1){
foot=0;
}
MainActivity.this.myImageSwitcher
.setImageResource(MainActivity.this.imgRes[MainActivity.this.foot++]);
}
}
private 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);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
import android.R.color;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
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.ImageView.ScaleType;
import android.widget.ViewSwitcher.ViewFactory;
public class MainActivity<MyDemo> extends Activity {
private Button butPrevious=null;
private Button butNext=null;
private ImageSwitcher myImageSwitcher=null;
private int imgRes[]=new int[]{R.drawable.pic_2,R.drawable.pic_3,
R.drawable.pic_4,R.drawable.pic_5,R.drawable.pic_7};
private int foot=0; //表示当前已经显示的数组图片的脚标
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_main);
this.butPrevious=(Button) super.findViewById(R.id.butPrevious);
this.butNext=(Button) super.findViewById(R.id.butNext);
this.myImageSwitcher=(ImageSwitcher) super.findViewById(R.id.myImageSwitcher);
this.myImageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));
this.myImageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
this.myImageSwitcher.setFactory(new ViewFactoryImpl());
this.myImageSwitcher.setImageResource(MainActivity.this.imgRes[this.foot++]);
this.butPrevious.setOnClickListener(new OnClickListenerPrevious());
this.butNext.setOnClickListener(new OnClickListenerNext());
}
private class ViewFactoryImpl implements ViewFactory{
@Override
public View makeView() {
ImageView img=new ImageView(MainActivity.this);
img.setBackgroundColor(color.black);
img.setScaleType(ImageView.ScaleType.CENTER);
img.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
return img;
}}
private class OnClickListenerPrevious implements OnClickListener{
@Override
public void onClick(View arg0) {
MainActivity.this.checkButEnable();
if(foot==imgRes.length){
foot=imgRes.length-1;
}
MainActivity.this.myImageSwitcher
.setImageResource(MainActivity.this.imgRes[MainActivity.this.foot--]);
}
}
private class OnClickListenerNext implements OnClickListener{
@Override
public void onClick(View arg0) {
MainActivity.this.checkButEnable();
if(foot==-1){
foot=0;
}
MainActivity.this.myImageSwitcher
.setImageResource(MainActivity.this.imgRes[MainActivity.this.foot++]);
}
}
private 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);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
main.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"
tools:context=".MainActivity" >
<ImageSwitcher
android:id="@+id/myImageSwitcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/butPrevious"
android:layout_below="@id/myImageSwitcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:text="上一张图片"/>
<Button
android:id="@+id/butNext"
android:layout_toRightOf="@id/butPrevious"
android:layout_below="@id/myImageSwitcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="true"
android:text="下一张图片"/>
</RelativeLayout>
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ImageSwitcher
android:id="@+id/myImageSwitcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/butPrevious"
android:layout_below="@id/myImageSwitcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:text="上一张图片"/>
<Button
android:id="@+id/butNext"
android:layout_toRightOf="@id/butPrevious"
android:layout_below="@id/myImageSwitcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="true"
android:text="下一张图片"/>
</RelativeLayout>