ImageSwitcher 练习(android)
ImageSwitch 需要一个 ViewFactory 接口方法,返回一个 ImageView 对像,同时指定 setFactory 方法
其中试了试用 ImageView 实现 ImageSwitcher ,并没有发现有什么不同,还有是其它什么不同呢?
代码如下:
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="切换图片"
/>
<ImageSwitcher
android:id="@+id/imageswitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></ImageSwitcher>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="切换图片2"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="切换图片"
/>
<ImageSwitcher
android:id="@+id/imageswitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></ImageSwitcher>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="切换图片2"
/>
</LinearLayout>
java代码
package zziss.android.imageswitchtest;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;
public class ImageSwitchTestActivity extends Activity implements ViewFactory {
/** Called when the activity is first created. */
private ImageView iv;
private ImageSwitcher imw;
private Button btn;
private Button btn2;
private int i;
private int index;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
iv = (ImageView)this.findViewById(R.id.iv);
btn = (Button)this.findViewById(R.id.btn);
btn2 = (Button)this.findViewById(R.id.btn2);
imw = (ImageSwitcher)this.findViewById(R.id.imageswitch);
imw.setFactory(this);
i = 0;
index =0;
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
/*if (i==0)
{
iv.setImageResource(R.drawable.png1410);
i=1;
}
else
{
iv.setImageResource(R.drawable.png1411);
i=0;
}*/
if (index>=3)
index = 0;
iv.setImageResource(imagelist[index]);
index++;
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (index>=3)
index = 0;
imw.setImageResource(imagelist[index]);
index++;
}
});
}
private static int[] imagelist =
{
R.drawable.png1410,
R.drawable.png1411,
R.drawable.png1412
};
@Override
public View makeView() {
// TODO Auto-generated method stub
return new ImageView(this);
}
}
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;
public class ImageSwitchTestActivity extends Activity implements ViewFactory {
/** Called when the activity is first created. */
private ImageView iv;
private ImageSwitcher imw;
private Button btn;
private Button btn2;
private int i;
private int index;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
iv = (ImageView)this.findViewById(R.id.iv);
btn = (Button)this.findViewById(R.id.btn);
btn2 = (Button)this.findViewById(R.id.btn2);
imw = (ImageSwitcher)this.findViewById(R.id.imageswitch);
imw.setFactory(this);
i = 0;
index =0;
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
/*if (i==0)
{
iv.setImageResource(R.drawable.png1410);
i=1;
}
else
{
iv.setImageResource(R.drawable.png1411);
i=0;
}*/
if (index>=3)
index = 0;
iv.setImageResource(imagelist[index]);
index++;
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (index>=3)
index = 0;
imw.setImageResource(imagelist[index]);
index++;
}
});
}
private static int[] imagelist =
{
R.drawable.png1410,
R.drawable.png1411,
R.drawable.png1412
};
@Override
public View makeView() {
// TODO Auto-generated method stub
return new ImageView(this);
}
}