《使用ImageSwitcher实现如下效果》

来源:互联网 发布:算法导论第二版百度云 编辑:程序博客网 时间:2024/05/21 10:38
?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"     android:background="#000099">    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="wrap_content" >        <ImageSwitcher            android:id="@+id/is_imageswitch"            android:layout_width="wrap_content"            android:layout_height="fill_parent"            >        </ImageSwitcher>    </LinearLayout>    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="wrap_content" >        <Button            android:id="@+id/btn_last"            android:layout_width="150px"            android:layout_height="wrap_content"            android:onClick="onClickLast"            android:text="上一张" >        </Button>        <Button            android:id="@+id/btn_next"            android:layout_width="150px"            android:layout_height="wrap_content"            android:onClick="onClickNext"            android:text="下一张" >        </Button>    </LinearLayout></LinearLayout>

package com.example.test5;import android.os.Bundle;import android.app.Activity;import android.content.Context;import android.view.Menu;import android.view.View;import android.widget.Button;import android.widget.ImageSwitcher;import android.widget.ImageView;import android.widget.ViewSwitcher.ViewFactory;public class MainActivity extends Activity {private ImageSwitcher is_imageSwitcher;//存放图片id的int数组 private int[] images={ R.drawable.pic1, R.drawable.pic2, R.drawable.pic3, R.drawable.pic4, R.drawable.pic5, R.drawable.pic6, }; private Button btn_next; private Button btn_last; private int index=0;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);is_imageSwitcher=(ImageSwitcher)findViewById(R.id.is_imageswitch); btn_last=(Button)findViewById(R.id.btn_last); btn_next=(Button)findViewById(R.id.btn_next); is_imageSwitcher.setFactory(new ImageViewFactory(this)); is_imageSwitcher.setBackgroundResource(images[index]);} class ImageViewFactory implements ViewFactory { private Context context; public ImageViewFactory(Context context) { this.context = context; } public View makeView() { // TODO Auto-generated method stub //定义每个图像的显示大小 ImageView iv = new ImageView(this.context); iv.setLayoutParams(new ImageSwitcher.LayoutParams(300, 300)); return iv; } } public void onClickLast(View v) { if(index>=0&&index<images.length-1) { index++; is_imageSwitcher.setBackgroundResource(images[index]); }else { index=images.length-1; } } public void onClickNext(View v) { if(index>0&&index<images.length) { index--; is_imageSwitcher.setBackgroundResource(images[index]); }else { index=images.length-1; } }@Overridepublic 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;}}

0 0