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;
        }
        
    }
}

 

转载于:https://www.cnblogs.com/will-peng/archive/2013/03/18/2966898.html

  • 0
    点赞
  • 0
    收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:编程工作室 设计师:CSDN官方博客 返回首页
评论
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值