1. 程式人生 > >ImageView 圖片切換 點選左右箭頭切換圖片

ImageView 圖片切換 點選左右箭頭切換圖片


 
MainActivity.java
package test.com.myimageview;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.TextView;
import 
android.widget.ViewSwitcher; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { List<Drawable> drawableList = new ArrayList<Drawable>();//存放圖片 TextView arrowLeftTextView;//左箭頭 TextView arrowRightTextView;//右箭頭 private int index
=0;// @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); drawableList.add(getResources().getDrawable(R.drawable.image01));//圖片01 drawableList.add(getResources().getDrawable(R.drawable.image02
));//圖片02 drawableList.add(getResources().getDrawable(R.drawable.image03));//圖片03 drawableList.add(getResources().getDrawable(R.drawable.image04));//圖片04 final ImageSwitcher imageSwitcher = findViewById(R.id.imageSwitcher_is); arrowLeftTextView = findViewById(R.id.arrow_left_iv); arrowRightTextView = findViewById(R.id.arrow_right_iv); //工廠 imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() { @Override public View makeView() { return new ImageView(MainActivity.this); } }); imageSwitcher.setImageDrawable(drawableList.get(index++));//初始化顯示第一張圖片 arrowLeftTextView.setOnClickListener(new View.OnClickListener(){//左箭頭單擊事件 @Override public void onClick(View view) { imageSwitcher.setImageDrawable(drawableList.get(index--)); if(index<0)//如果到達了圖片的開始,則直接顯示最後一張圖片 { index = drawableList.size()-1; } } }); arrowRightTextView.setOnClickListener(new View.OnClickListener(){//右箭頭單擊事件 @Override public void onClick(View view) { imageSwitcher.setImageDrawable(drawableList.get(index++)); if(index>=drawableList.size())//如果點選到達圖片的末尾,則回到第一張圖片 { index = 0; } } }); } }


 
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">



    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center">
        
        <TextView
            android:id="@+id/arrow_left_iv"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:text="<"
            android:gravity="center"/>
        <ImageSwitcher
            android:id="@+id/imageSwitcher_is"
            android:layout_width="150dp"
            android:layout_height="150dp"/>
        <TextView
            android:id="@+id/arrow_right_iv"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:text=">"
            android:gravity="center"/>
        
    </LinearLayout>




</android.support.design.widget.CoordinatorLayout>