瘋狂 Android 講義, 4/e – 自動播放資源圖片庫(AdapterViewFlipper)範例 P121~P124

瘋狂 Android 講義, 4/e – 自動播放資源圖片庫(AdapterViewFlipper)範例 P121~P124

瘋狂 Android 講義, 4/e – 自動播放資源圖片庫(AdapterViewFlipper)範例 P121~P124


資料來源:

    https://github.com/daichangya/book/tree/master/android

    https://pan.baidu.com/s/1d_xYJI0UQ_1tQzSj_V_NIg 提取码:70ch


XML Code

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".MainActivity">
	<AdapterViewFlipper
		android:id="@+id/flipper"
		android:layout_width="match_parent"
		android:layout_height="match_parent"
		android:layout_alignParentTop="true"
		android:flipInterval="5000" />
	<Button
		android:id="@+id/button"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout_marginStart="8dp"
		android:layout_marginBottom="8dp"
		android:onClick="prev"
		android:text="上一个"
		app:layout_constraintBottom_toBottomOf="parent"
		app:layout_constraintStart_toStartOf="parent" />

	<Button
		android:id="@+id/button2"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout_marginBottom="8dp"
		android:onClick="next"
		android:text="下一个"
		app:layout_constraintBottom_toBottomOf="parent"
		app:layout_constraintEnd_toStartOf="@+id/guideline"
		app:layout_constraintStart_toStartOf="@+id/guideline" />
	<Button
		android:id="@+id/button4"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout_marginEnd="8dp"
		android:layout_marginBottom="8dp"
		android:onClick="auto"
		android:text="自动播放"
		app:layout_constraintBottom_toBottomOf="parent"
		app:layout_constraintEnd_toEndOf="parent" />
	<android.support.constraint.Guideline
		android:id="@+id/guideline"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:orientation="vertical"
		app:layout_constraintGuide_percent="0.5" />
</android.support.constraint.ConstraintLayout>


JAVA Code

package org.crazyit.ui;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterViewFlipper;
import android.widget.BaseAdapter;
import android.widget.ImageView;

/**
 * Description:<br>
 * 网站: <a href="http://www.crazyit.org">疯狂Java联盟</a><br>
 * Copyright (C), 2001-2020, Yeeku.H.Lee<br>
 * This program is protected by copyright laws.<br>
 * Program Name:<br>
 * Date:<br>
 *
 * @author Yeeku.H.Lee kongyeeku@163.com<br>
 * @version 1.0
 */
public class MainActivity extends Activity
{
	private int[] imageIds = new int[]{R.drawable.shuangzi, R.drawable.shuangyu,
			R.drawable.chunv, R.drawable.tiancheng, R.drawable.tianxie,
			R.drawable.sheshou, R.drawable.juxie, R.drawable.shuiping,
			R.drawable.shizi, R.drawable.baiyang, R.drawable.jinniu, R.drawable.mojie};
	private AdapterViewFlipper flipper;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		flipper = findViewById(R.id.flipper);
		// 创建一个BaseAdapter对象,该对象负责提供AdapterViewFlipperTest所显示的列表项
		BaseAdapter adapter = new BaseAdapter()
		{
			@Override
			public int getCount()
			{
				return imageIds.length;
			}

			@Override
			public Object getItem(int position)
			{
				return position;
			}

			@Override
			public long getItemId(int position)
			{
				return position;
			}

			// 该方法返回的View代表了每个列表项
			@Override
			public View getView(int position, View convertView, ViewGroup parent)
			{
				ImageView imageView;
				if (convertView == null) {
					// 创建一个ImageView
					imageView = new ImageView(MainActivity.this);
				} else {
					imageView = (ImageView) convertView;
				}
				imageView.setImageResource(imageIds[position]);
				// 设置ImageView的缩放类型
				imageView.setScaleType(ImageView.ScaleType.FIT_XY);
				// 为imageView设置布局参数
				imageView.setLayoutParams(new ViewGroup.LayoutParams(
						ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
				return imageView;
			}
		};
		flipper.setAdapter(adapter);
	}
	public void prev(View source)
	{
		// 显示上一个组件
		flipper.showPrevious();
		// 停止自动播放
		flipper.stopFlipping();
	}
	public void next(View source)
	{
		// 显示下一个组件。
		flipper.showNext();
		// 停止自动播放
		flipper.stopFlipping();
	}
	public void auto(View source)
	{
		// 开始自动播放
		flipper.startFlipping();
	}
}

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *