溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么在Android中實現(xiàn)一個背景輪播圖效果

發(fā)布時間:2020-12-22 16:05:18 來源:億速云 閱讀:203 作者:Leah 欄目:開發(fā)技術

今天就跟大家聊聊有關怎么在Android中實現(xiàn)一個背景輪播圖效果,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據(jù)這篇文章可以有所收獲。

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中實現(xiàn)一個背景輪播圖效果有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

AI