Android实现简单的图片切换

android实现简单的图片切换

具体的界面如图
在这里插入图片描述
方法一
利用内部类实现
只展示xml文件及MainActivity文件
xml具体代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.ttt.image.MainActivity">

    <TextView
        android:id="@+id/textShow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="40dp"
        android:text="0"
        android:textSize="30dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btnNext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textShow"
        android:layout_marginBottom="58dp"
        android:layout_marginEnd="32dp"
        android:layout_toStartOf="@+id/textShow"
        android:text="下一张" />

    <Button
        android:id="@+id/btnPre"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/btnNext"
        android:layout_marginStart="23dp"
        android:layout_toEndOf="@+id/textShow"
        android:text="上一张" />

    <ImageView
        android:id="@+id/imgshow"
        android:layout_width="300dp"
        android:layout_height="200dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="56dp"
        android:scaleType="fitXY"
        android:src="@drawable/image1" />

</RelativeLayout>

MainActivity文件

package com.example.ttt.image;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private ImageView imgShow;//定义图像显示框
    private Button btnNext,btnPre;//定义按钮
    private TextView textShow;//定义文本框
    private int[] picArrary;//定义设置数组记录下标
    private int index=0;//定义下标值

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imgShow=findViewById(R.id.imgshow);//获取值
        btnNext=findViewById(R.id.btnNext);
        btnPre=findViewById(R.id.btnPre);
        textShow=findViewById(R.id.textShow);
        picArrary=new int[]{R.drawable.image1,R.drawable.image2,R.drawable.image3,R.drawable.image4,R.drawable.image5,R.drawable.image6};

        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                index++;
                imgShow.setImageResource(picArrary[index%6]);
                textShow.setText(index%6+"");
            }
        });//实现下一张
        btnPre.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (index>0){
                    index--;
                    imgShow.setImageResource(picArrary[index%6]);
                    textShow.setText(index%6+"");
                }
                if (index==0){
                    index=picArrary.length;

                }


            }
        });//实现上一张
    }
}

方法二
利用接口实现
只展示xml文件及MainActivity文件
xml具体代码跟上面一样:
MainActivity文件

package com.example.ttt.image;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView imgShow;
    private Button btnNext,btnPre;
    private TextView textShow;
    private int[] picArrary;
    private int index=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imgShow=findViewById(R.id.imgshow);
        btnNext=findViewById(R.id.btnNext);
        btnPre=findViewById(R.id.btnPre);
        textShow=findViewById(R.id.textShow);
        picArrary=new int[]{R.drawable.image1,R.drawable.image2,R.drawable.image3,R.drawable.image4,R.drawable.image5,R.drawable.image6};
        btnNext.setOnClickListener(this);//利用当前类需添加
        btnPre.setOnClickListener(this);//利用当前类需添加


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btnNext:
                index++;
                imgShow.setImageResource(picArrary[index%6]);
                textShow.setText(index%6+"");
                break;
            case R.id.btnPre:
                if (index>0){
                    index--;
                    imgShow.setImageResource(picArrary[index%6]);
                    textShow.setText(index%6+"");
                }
                if (index==0){
                    index=picArrary.length;

                }
        }
        }
}

  • 9
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值