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="返回主頁(yè)" />
</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);
}
});
}
}
以上就是本文的全部?jī)?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開(kāi)發(fā)實(shí)現(xiàn)的自動(dòng)換圖片、輪播圖效果示例
- Android實(shí)現(xiàn)輪播圖片展示效果
- Android自定義圖片輪播Banner控件使用解析
- Android高級(jí)圖片滾動(dòng)控件實(shí)現(xiàn)3D版圖片輪播器
- Android開(kāi)發(fā)使用Handler的PostDelayed方法實(shí)現(xiàn)圖片輪播功能
- 用AdapterViewFlipper輕松完成圖片輪播
相關(guān)文章
Android程序開(kāi)發(fā)之Fragment實(shí)現(xiàn)底部導(dǎo)航欄實(shí)例代碼
流行的應(yīng)用的導(dǎo)航一般分為兩種,一種是底部導(dǎo)航,一種是側(cè)邊欄。本文給大家介紹Fragment實(shí)現(xiàn)底部導(dǎo)航欄,對(duì)Fragment實(shí)現(xiàn)底部導(dǎo)航欄相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-03-03
Android開(kāi)發(fā)中實(shí)現(xiàn)應(yīng)用的前后臺(tái)切換效果
這篇文章主要介紹了Android開(kāi)發(fā)中實(shí)現(xiàn)應(yīng)用的前后臺(tái)切換效果的方法,文章最后還附帶了監(jiān)聽(tīng)程序是否進(jìn)入后臺(tái)的判斷方法,需要的朋友可以參考下2016-02-02
Android Studio 導(dǎo)入新工程項(xiàng)目圖解
這篇文章主要介紹了Android Studio 導(dǎo)入新工程項(xiàng)目圖解,需要的朋友可以參考下2017-12-12
Android自定義View實(shí)現(xiàn)QQ消息氣泡
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)QQ消息氣泡,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
Android使用Intent發(fā)送短信的實(shí)現(xiàn)方法
這篇文章主要介紹了Android使用Intent發(fā)送短信的實(shí)現(xiàn)方法,結(jié)合簡(jiǎn)單實(shí)例形式分析了Android短信發(fā)送功能的實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-07-07
Android中兩個(gè)類讓你再也不用實(shí)現(xiàn)onActivityResult()
這篇文章主要給大家介紹了關(guān)于Android中兩個(gè)類讓你再也不用實(shí)現(xiàn)onActivityResult()的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位Android開(kāi)發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧2018-08-08
Android編程實(shí)現(xiàn)Gallery中每次滑動(dòng)只顯示一頁(yè)的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)Gallery中每次滑動(dòng)只顯示一頁(yè)的方法,涉及Android擴(kuò)展Gallery控件實(shí)現(xiàn)翻頁(yè)效果控制的功能,涉及Android事件響應(yīng)及屬性控制的相關(guān)技巧,需要的朋友可以參考下2015-11-11

