我们在Windows 平台上要查看很多张图片,最简单的方法就是通过Windows图片和传真查看器,在上一张和下一张之间切换,Android平台上可以通过ImageSwicher类来实现这一效果(事实上通过其他方法也是可以实现的Bitmap)ImageSwitcher类必须设置一个ViewFactory,主要用来将显示的图片和父窗口区分开,通过makeView()方法来显示图片,这里会返回一个ImageView对象,而方法setImageResource用来显示指定的图片资源。我们现在看一个实例,当点击上一张或者下一张图片的时候切换浏览另一张图片。这里首先通过自己在界面中实现LinearLayout的方式来实现:
void android.widget.ImageSwitcher.setImageResource(int resid)
public void setImageResource(int resid)Added inAPI level 1
android.widget.LinearLayout.LayoutParams.LayoutParams(int width, int height)
public LinearLayout.LayoutParams(int width, int height) Added in API level 1
android.widget.Button.Button(Context context)
public Button (Context context)Added in API level 1
void android.widget.ViewSwitcher.setFactory(ViewFactory factory)
public void setFactory(ViewSwitcher.ViewFactory factory)
Sets the factory used to create the two views between which the ViewSwitcher will flip. Instead of using a factory, you can calladdView(android.view.View, int, android.view.ViewGroup.LayoutParams)
twice.
Parameters
factory | the view factory used to generate the switcher's content |
---|
public void addView(View child,ViewGroup.LayoutParams params)
Adds a child view with the specified layout parameters.
Note: do not invoke this method from draw(android.graphics.Canvas)
,onDraw(android.graphics.Canvas)
,dispatchDraw(android.graphics.Canvas)
or any related method.
Parameters
child | the child view to add |
---|---|
params | the layout parameters to set on the child |
package com.example.activity01;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ViewSwitcher.ViewFactory;
public class Activity01 extends Activity implements OnClickListener,ViewFactory
{
/* 所有要显示的图片资源索引 */
private static final Integer[] imagelist =
{
R.drawable.img1,
R.drawable.img2,
R.drawable.img3,
R.drawable.img4,
R.drawable.img5,
R.drawable.img6,
R.drawable.img7,
R.drawable.img8,
};
//创建ImageSwitcher对象
private ImageSwitcher m_Switcher;
//索引
private int index = 0;
//“下一页”按钮ID
private static final int BUTTON_DWON_ID = 0x123456;
//“上一页”按钮ID
private static final int BUTTON_UP_ID = 0x123457;
//ImageSwitcher对象的ID
private static final int SWITCHER_ID = 0x123458;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//创建一个线性布局LinearLayout
LinearLayout main_view = new LinearLayout(this);
//创建ImageSwitcher对象
m_Switcher = new ImageSwitcher(this);
//在线性布局中添加ImageSwitcher视图
main_view.addView(m_Switcher);
//设置ImageSwitcher对象的ID
m_Switcher.setId(SWITCHER_ID);
//设置ImageSwitcher对象的数据源 首先放置图片 然后放置按钮
m_Switcher.setFactory(this);
m_Switcher.setImageResource(imagelist[index]);
//设置显示上面创建的线性布局
setContentView(main_view);
//创建“下一张”按钮
Button next = new Button(this);
next.setId(BUTTON_DWON_ID);
next.setText("下一张");
next.setOnClickListener(this);
//就是一个构造函数 用于生成LinearLayout.LayoutParams对象
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(100, 100);
main_view.addView(next, param);
//创建“上一张”按钮
Button pre = new Button(this);
pre.setId(BUTTON_UP_ID);
pre.setText("上一张");
pre.setOnClickListener(this);
main_view.addView(pre, param);
}
//事件监听、处理
public void onClick(View v)
{
switch (v.getId())
{
//下一页
case BUTTON_DWON_ID:
index++;
if (index >= imagelist.length)
{
index = 0;
}
//ImageSwitcher对象资源索引
m_Switcher.setImageResource(imagelist[index]);
break;
//上一页
case BUTTON_UP_ID:
index--;
if (index < 0)
{
index = imagelist.length - 1;
}
//ImageSwitcher对象资源索引
m_Switcher.setImageResource(imagelist[index]);
break;
default:
break;
}
}
public View makeView()
{
//将所有图片通过ImageView来显示
return new ImageView(this);
}
}
下面是通过布局文件来设置界面组件的方式来实现的相关操作
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:background="#f0f0f0"
>
<Button
android:id="@+id/pre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@+string/pre"
/>
<ImageSwitcher
android:id="@+id/switcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
/>
<Button
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@+string/next"
/>
</LinearLayout>
</ScrollView>
package com.example.activity01;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;
public class Activity01 extends Activity implements ViewFactory
{
/* 所有要显示的图片资源索引 */
private static final Integer[] imagelist =
{
R.drawable.img1,
R.drawable.img2,
R.drawable.img3,
R.drawable.img4,
R.drawable.img5,
R.drawable.img6,
R.drawable.img7,
R.drawable.img8,
};
//创建ImageSwitcher对象
private ImageSwitcher m_Switcher = null;
private Button buttonNext = null;
private Button buttonPre = null;
//索引
private int index = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonNext = (Button)findViewById(R.id.next);
buttonPre =(Button)findViewById(R.id.pre);
m_Switcher = (ImageSwitcher)findViewById(R.id.switcher);
m_Switcher.setFactory(this);
m_Switcher.setImageResource(imagelist[index]);
buttonNext.setOnClickListener(listener);
buttonPre.setOnClickListener(listener);
}
//事件监听、处理
OnClickListener listener = new OnClickListener(){
public void onClick(View v)
{
switch (v.getId())
{
//下一页
case R.id.next:
index++;
if (index >= imagelist.length)
{
index = 0;
}
//ImageSwitcher对象资源索引
m_Switcher.setImageResource(imagelist[index]);
break;
//上一页
case R.id.pre:
index--;
if (index < 0)
{
index = imagelist.length - 1;
}
//ImageSwitcher对象资源索引
m_Switcher.setImageResource(imagelist[index]);
break;
default:
break;
}
}
};
@Override
public View makeView() {
//将所有图片通过ImageView来显示
return new ImageView(this);
}
}
很奇怪的一开始的界面布局xml文件并没有起作用