Android實(shí)現(xiàn)背景圖片輪播
本文實(shí)例為大家分享了Android實(shí)現(xiàn)背景圖片輪播的具體代碼,供大家參考,具體內(nèi)容如下
點(diǎn)擊按鈕實(shí)現(xiàn)圖片輪播效果
實(shí)踐案例:

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
};
//定義初始下標(biāo)為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);
}
});
}
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android基于AdapterViewFlipper實(shí)現(xiàn)的圖片/文字輪播動(dòng)畫控件
- Android輪播圖點(diǎn)擊圖片放大效果的實(shí)現(xiàn)方法
- Android實(shí)現(xiàn)圖片輪播列表
- 詳解android 視頻圖片混合輪播實(shí)現(xiàn)
- Android開發(fā)實(shí)現(xiàn)的自動(dòng)換圖片、輪播圖效果示例
- Android實(shí)現(xiàn)輪播圖片展示效果
- Android自定義圖片輪播Banner控件使用解析
- Android高級(jí)圖片滾動(dòng)控件實(shí)現(xiàn)3D版圖片輪播器
- Android開發(fā)使用Handler的PostDelayed方法實(shí)現(xiàn)圖片輪播功能
- 用AdapterViewFlipper輕松完成圖片輪播
相關(guān)文章
詳解Android JetPack之LiveData的工作原理
這篇文章主要介紹了詳解Android JetPack之LiveData的工作原理,幫助大家更好的理解和學(xué)習(xí)使用Android開發(fā),感興趣的朋友可以了解下2021-03-03
Flutter使用JsBridge方式處理Webview與H5通信的方法
這篇文章主要介紹了Flutter使用JsBridge方式處理Webview與H5通信的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
Android中EditText屏蔽第三方輸入法表情的方法示例
最近在工作終于遇到一個(gè)問題,因?yàn)榈谌捷斎敕ū砬榈膯栴}導(dǎo)致Android中TextView的內(nèi)容顯示異常,只能想辦法解決了,下面這篇文章主要記錄了在處理Android中EditText屏蔽第三方輸入法表情的方法,需要的朋友可以參考借鑒,下面來一起看看吧。2017-01-01
Android開發(fā)之開關(guān)按鈕控件ToggleButton簡(jiǎn)單用法示例
這篇文章主要介紹了Android開發(fā)之開關(guān)按鈕控件ToggleButton簡(jiǎn)單用法,結(jié)合實(shí)例形式分析了Android開關(guān)按鈕控件ToggleButton的相關(guān)xml布局與調(diào)用操作技巧,需要的朋友可以參考下2017-12-12
Android App仿QQ制作Material Design風(fēng)格沉浸式狀態(tài)欄
這篇文章主要介紹了Android App仿QQ制作Material Design風(fēng)格沉浸式狀態(tài)欄的實(shí)例,同時(shí)也給出了4.4版本下實(shí)現(xiàn)效果與5.0的對(duì)比,需要的朋友可以參考下2016-04-04
Android實(shí)戰(zhàn)打飛機(jī)游戲之怪物(敵機(jī))類的實(shí)現(xiàn)(4)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)戰(zhàn)打飛機(jī)游戲之怪物(敵機(jī))類的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-07-07
Android fragment實(shí)現(xiàn)按鈕點(diǎn)擊事件的示例講解
下面小編就為大家分享一篇Android fragment實(shí)現(xiàn)按鈕點(diǎn)擊事件的示例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01
Android?NotificationListenerService通知監(jiān)聽服務(wù)使用
這篇文章主要為大家介紹了Android?NotificationListenerService通知監(jiān)聽服務(wù)使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11

