效果图

1)可以把图像的id存放数组中,利用setImageResource()或setImageDrawable()方法(放在数组中便于循环)

2)已经是第一张图像时,再点击“上一页”,应Toast提示:已经是第一张图像,并不再往前翻;同样,已经是最后一张图像时,再点击“下一页”,应Toast提示:已经是最后一张图像,并不再往后翻。

 

给出源代码

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
android:orientation="horizontal"
tools:context="com.example.asus.a161304049_gary03.MainActivity"> <LinearLayout
android:id="@+id/Layoutt"
android:layout_width="match_parent"
android:layout_height="215dp"
android:orientation="vertical"> <ImageView
android:id="@+id/imagexianshi"
android:layout_width="match_parent"
android:layout_height="180dp"
app:srcCompat="@drawable/qzu1" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"> <Button
android:id="@+id/Back"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="上一张" /> <Button
android:id="@+id/Next"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="下一张" />
</LinearLayout> </LinearLayout> </FrameLayout>

activity_main.xml

package com.example.asus.a161304049_gary03;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Button;
import android.widget.Toast; public class MainActivity extends AppCompatActivity { //用一个数组来保存图片
int[] images = new int[]{
R.drawable.qzu1,
R.drawable.qzu2,
R.drawable.qzu3,
R.drawable.qzu5
};
//作为图片的下标
int countImg = 0;
//前进和后退的按钮
private Button B1,B2;
//用ImageView来存放图片
private ImageView Xianshi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); B1 = (Button) findViewById(R.id.Back);
B2 = (Button) findViewById(R.id.Next);
Xianshi = (ImageView) findViewById(R.id.imagexianshi); Xianshi.setImageResource(images[0]); B1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
//这里就是一个简单的逻辑啦
//如果选中的图片是第一张,那么就不能再选择前面的一张。输出Toast提示的消息
if(countImg>0){
countImg--;
//用setImageResource来显示图片
Xianshi.setImageResource(images[countImg]);
}else{
Toast.makeText(MainActivity.this,"已经是第一张图像,并不再往前翻",Toast.LENGTH_SHORT).show();
}
}
}); B2.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
//如果选中的图片是最后,那么就不能再选择后面的一张。输出Toast提示的消息
if(countImg<3){
countImg++;
Xianshi.setImageResource(images[countImg]);
}else{
Toast.makeText(MainActivity.this,"已经是最后一张图像,并不再往后翻",Toast.LENGTH_SHORT).show();
}
}
});
}
}

MainActivity

ImageView控件用来存放图片展示出来,xml属性不是本文重点,这里就不再介绍。

第一步:定义成员变量

    //用一个数组来保存图片
int[] images = new int[]{
R.drawable.qzu1,
R.drawable.qzu2,
R.drawable.qzu3,
R.drawable.qzu5
};
//作为图片的下标
int countImg = 0;
//前进和后退的按钮
private Button B1,B2;
//用ImageView来存放图片
private ImageView Xianshi;

第二步:实现按钮"上一张"和"下一张"的事件响应机制

 protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); B1 = (Button) findViewById(R.id.Back);
B2 = (Button) findViewById(R.id.Next);
Xianshi = (ImageView) findViewById(R.id.imagexianshi); Xianshi.setImageResource(images[0]); B1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
//这里就是一个简单的逻辑啦
//如果选中的图片是第一张,那么就不能再选择前面的一张。输出Toast提示的消息
if(countImg>0){
countImg--;
//用setImageResource来显示图片
Xianshi.setImageResource(images[countImg]);
}else{
Toast.makeText(MainActivity.this,"已经是第一张图像,并不再往前翻",Toast.LENGTH_SHORT).show();
}
}
}); B2.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
//如果选中的图片是最后,那么就不能再选择后面的一张。输出Toast提示的消息
if(countImg<3){
countImg++;
Xianshi.setImageResource(images[countImg]);
}else{
Toast.makeText(MainActivity.this,"已经是最后一张图像,并不再往后翻",Toast.LENGTH_SHORT).show();
}
}
});
}

传送门:消息模式Toast.make Text的几种常见用法

Android_(控件)使用ImageView简单实现图片翻转的更多相关文章

  1. 矩阵, 矩阵 , Android基础控件之ImageView

    天下文章大家抄,以下所有内容,有来自copy,有来自查询,亦有自己的总结(目的是总结出自己的东西),所以说原创,不合适,说是转载也不恰当,所以我称之为笔记,可惜没有此分类选项,姑且不要脸一点,选择为原 ...

  2. 【Android】11.0 UI开发(二)——列表控件ListView的简单实现1

    ************************ 转载请注明出处:https://www.cnblogs.com/xiaofu007/p/10342462.html ***************** ...

  3. Android基本控件Spinner的简单使用【转】

    Android基本控件Spinner的简单使用 感谢大佬:https://blog.csdn.net/bingocoder/article/details/80469939 学习过了Textview, ...

  4. iOS开发UI篇—使用picker View控件完成一个简单的选餐应用

    iOS开发UI篇—使用picker View控件完成一个简单的选餐应用 一.实现效果 说明:点击随机按钮,能够自动选取,下方数据自动刷新. 二.实现思路 1.picker view的有默认高度为162 ...

  5. Delphi7 第三方控件1stClass4000的TfcImageBtn按钮控件动态加载jpg图片例子

    Delphi7 第三方控件1stClass4000的TfcImageBtn按钮控件动态加载jpg图片例子 procedure TForm1.Button1Click(Sender: TObject); ...

  6. 抛砖引玉 【镜像控件】 WPF实现毛玻璃控件不要太简单

    原文:抛砖引玉 [镜像控件] WPF实现毛玻璃控件不要太简单 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/Vblegend_2013/articl ...

  7. Android_(控件)使用Gallery浏览手机上SD卡中图片

    运行截图: (发现后面两张照片是自己自拍,大写的尴尬对图片进行涂鸦了!!!) 程序结构: <?xml version="1.0" encoding="utf-8&q ...

  8. Android控件之ImageView(显示图片的控件)

    一.ImageView属性: android:src = "@drawable/ic_launcher"——ImageView的内容图像(可以和android:background ...

  9. android基本控件学习-----ImageView

    ImageView的讲解 一.src和background的区别 background我们通常理解是背景,而src是内容,当使用src填入图片的时候是以图片的大小直接填充,并不会进行拉伸,而backg ...

随机推荐

  1. hashMap,hashTable,TreeMap,concurrentHashMap区别

    hashMap: 基于哈希表实现 treeMap: 基于二叉树实现,适用于排序 hashTable: 底层还是HashMap,在方法上加了同步 concurrentHashMap: java7底层通过 ...

  2. Fonour.AspnetCore 生成SQL SERVER数据库

    Install-Package EntityFramework Add-Migration InitialCreate Update-Database

  3. XML转换成DataTable

    #region XML转dataset //str 是xml字符串 public static DataTable GetResultXMLToDataTable (string str,string ...

  4. Length of LOB data (190999) to be replicated exceeds configured maximum 65536. 错误修改

    在上传附件时,本地是可以的但服务器上就有了文件大小的限制,不能上传.经过打断点找到这样一个错误: Length of LOB data (190999) to be replicated exceed ...

  5. nodes.js详细安装

    nodes.js详细安装 Node.js 本章节我们将向大家介绍在window和Linux上安装Node.js的方法. 本安装教程以Node.js v4.4.3 LTS(长期支持版本)版本为例. No ...

  6. python爬虫redis-ip代理池搭建几十万的ip数据--可以使用

    from bs4 import BeautifulSoupimport requests,os,sys,time,random,redisfrom lxml import etreeconn = re ...

  7. js 的一些小技巧

    来源:https://www.w3cplus.com/javascript/javascript-tips.html 1.确保数组的长度 在处理网格结构时,如果原始数据每行的长度不相等,就需要重新创建 ...

  8. 使用WSGI创建REST接口

    问题 你想使用一个简单的REST接口通过网络远程控制或访问你的应用程序,但是你又不想自己去安装一个完整的web框架. 解决方案 构建一个REST风格的接口最简单的方法是创建一个基于WSGI标准(PEP ...

  9. java中的运算符与表达式

    运算符与表达式 运算符分类: 0.赋值运算符 = 1.算数运算符 + - * / % 2.比较运算符 < > == <= >= != 3.逻辑运算符 & | ! &am ...

  10. 【Swift后台】目录

    背景介绍 环境安装