点击按钮实现图片轮播效果

实践案例:

xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".Main2Activity"
android:orientation="vertical"
android:gravity="center"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="350dp"> <android.support.v7.widget.AppCompatImageView
android:id="@+id/img1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/img1" /> <android.support.v7.widget.AppCompatImageView
android:id="@+id/img2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/img2" /> <android.support.v7.widget.AppCompatImageView
android:id="@+id/img3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/img3" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"> <Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="切换图片" /> <Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="返回主页" /> </LinearLayout> </LinearLayout>

Java

package com.example.administrator.demo2;

import android.content.Intent;
import android.media.Image;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView; public class Main2Activity extends AppCompatActivity {
//定义所有的轮播图片
int[] image = new int[]{
R.mipmap.img1,
R.mipmap.img2,
R.mipmap.img3
};
//定义初始下标为0
int Index = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
//获取ImageView
final ImageView img = (ImageView) findViewById(R.id.img1);
img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Index>=2){
Index=-1;
}
//改变ImageView中的Src属性值
img.setImageResource(image[++Index]);
}
}); Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Index>=2)
Index=-1;
//改变ImageView中的Src属性值
img.setImageResource(image[++Index]);
}
}); Button ubt1 = (Button) findViewById(R.id.button2);
ubt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent it = new Intent();
it.setClass(Main2Activity.this,MainActivity.class);
startActivity(it);
}
}); }
}

Android开发案例 设置背景图片轮播的更多相关文章

  1. Android 开发最牛的图片轮播控件,基本什么都包含了。

    Android图片轮播控件  源码下载地址: Android 图片轮播 现在的绝大数app都有banner界面,实现循环播放多个广告图片和手动滑动循环等功能.因为ViewPager并不支持循环翻页, ...

  2. Hexo瞎折腾系列(2) - 添加背景图片轮播

    动态背景图片插件jquery-backstretch jquery-backstretch是一款简单的jQuery插件,可以用来设置动态的背景图片,以下是官方网站的介绍. A simple jQuer ...

  3. Android代码中设置背景图片

    //设置背景图片        String picfile= Environment.getExternalStorageDirectory() + "/pdp/pdp.png" ...

  4. Android开发实战之ViewPager的轮播

    在安卓开发的许多控件中,如果你没有使用过ViewPager,就不能算是一个安卓开发工程师,在本篇博文中,我会总结ViewPager的使用方法, 以及一些开发中的拓展.希望本篇博文对你的学习和工作有所帮 ...

  5. android 项目中设置背景图片

    xml文件设置背景图片中:任意一个控件,button imageView 或layout,在其的xml属性设置中,添加 [XML] view plaincopy android:background= ...

  6. Android学习笔记之图片轮播...

    PS:一个bug又折腾了一个下午....哎... 学习内容: 1.Android利用ViewPager和PagerAdapter实现图片轮播... 2.使用反射机制获取Android的资源信息... ...

  7. Android 使用ViewPager 做的半吊子的图片轮播

    Android 使用ViewPager 做的半吊子的图片轮播 效果图 虽然不咋样,但是最起码的功能是实现了,下面我们来一步步的实现它. 界面 下面我们来分析一下界面的构成 整体的布局: 因为我们要做出 ...

  8. Android开发(九)——ViewFlipper实现图片轮播

    图片轮播的实现方法有很多,主要有View.ViewFilpper.ViewFilpper系统自带的一个多页面管理控件,它可以实现子界面的自动切换. 首先 需要为ViewFlipper加入View (1 ...

  9. 【iOS开发-55】图片轮播案例:scrollView的分页、滚动栏、利用代理控制定时器和Page Control以及多线程问题

    案例: (1)用storyboard布局,这里用了三样东西. --UIScrollView就是我们准备存放滚动图片的容器. --Page Control就是控制页数的那几个小点点.能够设置有多少个点. ...

随机推荐

  1. Linux系统中安装软件方法总结

    Linux系统中安装软件方法总结 [1]Linux系统中安装软件的几种方式 [2] Linux配置yum源(本地源和网络源) [3] SuSE下zypper源配置 [4] SUSE zypper 本地 ...

  2. When do we pass arguments by reference or pointer?

    在C++中,基于以下如下我们通过以引用reference的形式传递变量. (1)To modify local variables of the caller function A reference ...

  3. mysql之join浅析

    1.可以使用join吗?使用join有什么问题呢?-- >超过3个表不使用join,笛卡尔积问题 -->这些问题是怎么造成的呢? 如果可以使用 Index Nested-Loop Join ...

  4. linux 磁盘满了,vim 编辑文件时无法保存

    早上来发现 redis 不能用,报 MISCONF Redis is configured to save RDB snapshots, but it is currently not able to ...

  5. JS 的三种定义变量 var let const

    Let 只在 let 命令所在的代码块内有效,在外就会报错 Let 是块级作用域,函数内部使用let定义后,对函数外部无影响 Let/const 不存在变量提升,使用前一定要声明后,在使用,否则会报错 ...

  6. 1945-祖安 say hello-String

    1 #define _CRT_SECURE_NO_WARNINGS 1 2 #include<bits/stdc++.h> 3 char str[100][40]; 4 char s[10 ...

  7. 3、Linux的Redis安装

    Linux下安装redis 1.Redis下载 [Redis官网下载地址](https://redis.io/download)    进入官网进行下载 wget https://download.r ...

  8. C#编程概述(摘抄)

    C#编程概述 一个简单的C#程序 标识符 标识符是一种字符串,用来命名变量.方法.参数和许多后面将要阐述的其他程序结构. 关键字 所有C#关键字都由小写字母组成,但是.NET类型名使用Pascal大小 ...

  9. Jenkins监控

    目录 一.Monitoring插件 二.Prometheus监控 一.Monitoring插件 Monitoring插件(monitoring)使用JavaMelody,对Jenkins进行监控.插件 ...

  10. CF1497A Meximization 题解

    Content 给定 \(n\) 个数 \(a_1,a_2,\dots,a_n\),你需要将这些数重新排列,使得 \(\sum\limits_{i=1}^n\operatorname{mex}(a_1 ...