imageSwitcher控件用法

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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.imageswitcher.MainActivity$PlaceholderFragment" >

    <ImageSwitcher
        android:id="@+id/imageSwitcher1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignLeft="@+id/button1"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="38dp"
        android:layout_marginTop="61dp" >
    </ImageSwitcher>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/imageSwitcher1"
        android:layout_marginLeft="53dp"
        android:layout_marginTop="69dp"
        android:text="next" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="53dp"
        android:layout_marginTop="69dp"
        android:layout_alignBaseline="@+id/button1"
        android:layout_alignBottom="@+id/button1"
        android:layout_toRightOf="@id/button1"
        android:text="before" />

</RelativeLayout>

java代碼:

package com.example.imageswitcher;


import java.util.ArrayList;
import java.util.List;


import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
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 MainActivity extends ActionBarActivity implements OnClickListener,ViewFactory{
private Button button1,button2;
private ImageSwitcher imageSwitcher;
private List<Drawable> list=new ArrayList<Drawable>();
private int index=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
button1=(Button) findViewById(R.id.button1);
button2=(Button) findViewById(R.id.button2);
imageSwitcher=(ImageSwitcher) findViewById(R.id.imageSwitcher1);


button1.setOnClickListener(this);
button2.setOnClickListener(this);
imageSwitcher.setFactory(this);
list.add(getResources().getDrawable(R.drawable.item1));
list.add(getResources().getDrawable(R.drawable.item2));
list.add(getResources().getDrawable(R.drawable.item3));
list.add(getResources().getDrawable(R.drawable.item4));
list.add(getResources().getDrawable(R.drawable.item5));
list.add(getResources().getDrawable(R.drawable.item6));
list.add(getResources().getDrawable(R.drawable.item7));
list.add(getResources().getDrawable(R.drawable.item8));
if (list.size()>0) {
imageSwitcher.setImageDrawable(list.get(0));
}
}
@Override
public View makeView() {
// TODO Auto-generated method stub
return new ImageView(MainActivity.this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
index++;
if (index>=list.size()) {
index=0;
}
imageSwitcher.setImageDrawable(list.get(index));


break;


case R.id.button2:
index--;
if (index<0) {
index=list.size()-1;
}
imageSwitcher.setImageDrawable(list.get(index));
break;
}


}




}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章