Android学习第十一天----ImageSwitch

在主页中显示图片,然后点击向上翻或者向下翻能够实现图片的切换

xml中两个button组件,还有一个ImageSwitcher组件

<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"
    tools:context=".MainActivity" >

    <ImageSwitcher
        android:id="@+id/imageSwitcher1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView1"
        android:layout_marginBottom="85dp" >

    </ImageSwitcher>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="158dp"
        android:layout_marginLeft="21dp"
        android:layout_toRightOf="@+id/imageSwitcher1"
        android:text="up" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button1"
        android:layout_alignBottom="@+id/button1"
        android:layout_marginLeft="52dp"
        android:layout_toRightOf="@+id/button1"
        android:text="down" />

</RelativeLayout>

java代码:

package com.example.imageswitcher;

import android.app.Activity;

import android.graphics.Color;
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 MainActivity extends Activity implements OnClickListener
{
    private Button mButton1,mButton2;
    private ImageSwitcher mImageSwitcher;
    private int [] image={R.drawable.xiao,R.drawable.qi,R.drawable.xi,R.drawable.lei,R.drawable.ku,};
    private int index = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        mImageSwitcher = (ImageSwitcher)findViewById(R.id.imageSwitcher1);
        mButton1 = (Button)findViewById(R.id.button1);
        mButton2 = (Button)findViewById(R.id.button2);
        
        mImageSwitcher.setFactory(new ViewFactory()
        {
            
            @Override
            public View makeView()
            {
                ImageView mImageView = new ImageView(MainActivity.this);
                mImageView.setBackgroundColor(Color.RED);
                
                return mImageView;
            }
        });
        
        mImageSwitcher.setImageResource(image[index]);
        
        mButton1.setOnClickListener(this);
        mButton2.setOnClickListener(this);
        
        
    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
        case R.id.button1:
            if(index > 0)
            {
                index--;
            }
            else
            {
                index = image.length -1;
            }
            mImageSwitcher.setImageResource(image[index]);
            break;
            
        case R.id.button2:
            if(index < image.length -1)
            {
                index++;
            }
            else
            {
                index = 0;
            }
            mImageSwitcher.setImageResource(image[index]);
            break;

        default:
            break;
        }
        
    }
}

 

posted @ 2013-03-18 22:07  小三小山  阅读(199)  评论(0编辑  收藏  举报