您的位置:首页 > 其它

ImageSwitcher的初用

2015-10-23 11:39 295 查看
一,切换图片类,ImageSwitcher, 上一张,下一张查看图片的类。

//viewFactory 主要用来将显示的图片和父窗口区分开来

public class ImageSwitcherActivity extends Activity implements ViewFactory {
private int[] drawables = { R.drawable.aa, R.drawable.bb, R.drawable.cc, R.drawable.dd, R.drawable.ee,
R.drawable.ff, R.drawable.gg };
private ImageSwitcher mSwitcher;//切换图片类
private static int index=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_imageswitcher_text);
mSwitcher=(ImageSwitcher) findViewById(R.id.switcher);
mSwitcher.setFactory(this);
mSwitcher.setImageResource(drawables[index]);
}

public void myclick(View v){
switch (v.getId()) {
case R.id.up:
index++;
if(index>=drawables.length-1)
index=0;

break;

case R.id.down:
index--;
if(index<0)
index=drawables.length-1;

break;
}
mSwitcher.setImageResource(drawables[index]);
}

//显示图片的类。
@Override
public View makeView() {

return new ImageView(this);
}

}

二,对就的xml类。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >

    <ImageSwitcher

        android:id="@+id/switcher"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content" >

    </ImageSwitcher>

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:gravity="center" >

        <Button

            android:id="@+id/up"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_margin="20dip"

            android:onClick="myclick"

            android:text="上一张" />

        <Button

            android:id="@+id/down"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:onClick="myclick"

            android:text="下一张" />

    </LinearLayout>

</LinearLayout>

三,对应的效果图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ImageSwitcher