Android ImageView实现上一页,下一页图片切换

编程知识 行业动态 更新时间:2024-06-13 00:21:18

刚开始学习Android真的是头很大,需要学习和理解的东西太多,还好网上可以四处搜索加强自己的理解和学习,多的不说上图片上代码。

实际效果是这样的:


布局文件:

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android/apk/res/android"
    xmlns:tools="http://schemas.android/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/image"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/imagename"
        android:textColor="@color/colorImage"
        android:textSize="25sp"/>

    <ImageView
        android:id="@+id/imgView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/image"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:contentDescription="@string/app_name" />

    <Button
        android:id="@+id/pre"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/next"
        android:layout_alignBottom="@+id/next"
        android:layout_marginStart="80dp"
        android:text="@string/Previousname" />

    <Button
        android:id="@+id/next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginStart="250dp"
        android:layout_marginTop="400dp"
        android:text="@string/Nextname" />
</RelativeLayout>

实现代码如下:

MainActivity.java

package com.example.imageview;

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


public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private Button pre;
    private Button next;
    private ImageView imgView;

    private int[] photos = {R.drawable.image1, R.drawable.image2, R.drawable.image3
            , R.drawable.image4, R.drawable.image5, R.drawable.image6};
    private int photoIndex = 0;
    private int maxIndex = 5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pre = (Button) findViewById(R.id.pre);
        next = (Button)findViewById(R.id.next);
        imgView = findViewById(R.id.imgView);
        pre.setOnClickListener(this);
        next.setOnClickListener(this);
        imgView.setImageResource(photos[3]);
    }

    @Override
    public void onClick(View view)
    {
        switch (view.getId())
        {
            case R.id.pre:
                if (photoIndex == 0)
                {
                    photoIndex = maxIndex;
                }
                else
                {
                    photoIndex = photoIndex - 1;
                }
                break;
            case R.id.next:
                if (photoIndex == maxIndex)
                {
                    photoIndex = 0;
                }
                else
                {
                    photoIndex = photoIndex + 1;
                }
                break;
            default:
                break;
        }
        //显示图片
        imgView.setImageResource(photos[photoIndex]);
    }
}

其中添加图片的话需要自己下载一些图片放到drawable路径下,之后才能进行引用


引用存放在string.xml文件中的文言
android:text="@string/Nextname"

一些小的细节可以自己来修改修饰。。

更多推荐

Android ImageView实现上一页,下一页图片切换

本文发布于:2023-03-31 14:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/bcc36a31ff9b5e5c29cf9588181e1dd3.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:下一页   上一页   图片   Android   ImageView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!