android学习---ImageSwitcher

本文介绍了一个简单的 Android 应用示例,通过使用 ImageSwitcher 控件实现了图片的前后切换功能。该应用包含两个按钮,分别用于切换到上一张和下一张图片。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

布局文件:

<LinearLayout 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:orientation="vertical" >

    <ImageSwitcher
        android:id="@+id/imageSwitcher1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="5dp" >
    </ImageSwitcher>

    <LinearLayout
        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:layout_marginTop="5dp"
        android:orientation="horizontal" >

        <!-- 前一个箭头 -->

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="forward" >
        </Button>

        <!-- 下一个箭头 -->

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="next" >
        </Button>
    </LinearLayout>

</LinearLayout>

实现代码:

package com.leaf.android;

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 Main extends Activity implements OnClickListener {
    private Button mButton1, mButton2;
    private ImageSwitcher mImageSwitcher;
    private int[] image = { R.drawable.car1, R.drawable.car2, R.drawable.car3,
            R.drawable.car4, R.drawable.car5 };
    private int index = 0;//用于浏览图片的次序
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mImageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher1);
        mButton1 = (Button) findViewById(R.id.button1);
        mButton2 = (Button) findViewById(R.id.button2);
        mButton1.setOnClickListener(this);
        mButton2.setOnClickListener(this);

        mImageSwitcher.setFactory(new ViewFactory() {

            public View makeView() {
                ImageView mImageView = new ImageView(Main.this);
                mImageView.setBackgroundColor(Color.GREEN);
                return mImageView;
            }
        });
        mImageSwitcher.setImageResource(image[index]);
    }

    public void onClick(View v) {
        // TODO Auto-generated method stub
        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;
        }
    }
}

效果:

转载于:https://www.cnblogs.com/lea-fu/p/3298266.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值