app開發源代碼,android gallery源代碼,可循環顯示圖像的Android Gallery組件

 2023-10-20 阅读 24 评论 0

摘要:類型:源碼相關大小:23.6M語言:中文 評分:9.1標簽:立即下載第 5 頁 本例中Main類的完整代碼本例中Main類的完整代碼如下:app開發源代碼、代碼package net.blogjava.mobile;import android.app.Activity;import android.content.Cont

372990c5008d59c9a355ac15d72d7759.png

類型:源碼相關大小:23.6M語言:中文 評分:9.1

標簽:

立即下載

第 5 頁 本例中Main類的完整代碼

本例中Main類的完整代碼如下:

app開發源代碼、代碼

package net.blogjava.mobile;

import android.app.Activity;

import android.content.Context;

import android.content.res.TypedArray;

import android.os.Bundle;

安卓開發源代碼?import android.view.View;

import android.view.ViewGroup;

import android.view.animation.AnimationUtils;

import android.widget.AdapterView;

import android.widget.BaseAdapter;

import android.widget.Gallery;

android navigation。import android.widget.ImageSwitcher;

import android.widget.ImageView;

import android.widget.AdapterView.OnItemSelectedListener;

import android.widget.Gallery.LayoutParams;

import android.widget.ViewSwitcher.ViewFactory;

public class Main extends Activity implements OnItemSelectedListener,

android inflate、ViewFactory

{

private Gallery gallery;

private ImageSwitcher imageSwitcher;

private ImageAdapter imageAdapter;

private int[] resIds = new int[]

android畫廊?{ R.drawable.item1, R.drawable.item2, R.drawable.item3, R.drawable.item4,

R.drawable.item5, R.drawable.item6, R.drawable.item7,

R.drawable.item8, R.drawable.item9, R.drawable.item10,

R.drawable.item11, R.drawable.item12, R.drawable.item13,

R.drawable.item14, R.drawable.item15 };

public class ImageAdapter extends BaseAdapter

gallery、{

int mGalleryItemBackground;

private Context mContext;

public ImageAdapter(Context context)

{

mContext = context;

安卓源代碼網站。TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);

mGalleryItemBackground = typedArray.getResourceId(

R.styleable.Gallery_android_galleryItemBackground, 0);

}

// 第1點改進,返回一個很大的值,例如,Integer.MAX_VALUE

public int getCount()

android bottomnavigationview,{

return Integer.MAX_VALUE;

}

public Object getItem(int position)

{

return position;

android小程序源代碼,}

public long getItemId(int position)

{

return position;

}

public View getView(int position, View convertView, ViewGroup parent)

android imageview。{

ImageView imageView = new ImageView(mContext);

// 第2點改進,通過取余來循環取得resIds數組中的圖像資源ID

imageView.setImageResource(resIds[position % resIds.length]);

imageView.setScaleType(ImageView.ScaleType.FIT_XY);

imageView.setLayoutParams(new Gallery.LayoutParams(163, 106));

blink gallery官網?imageView.setBackgroundResource(mGalleryItemBackground);

return imageView;

}

}

@Override

public void onItemSelected(AdapterView> parent, View view, int position,long id)

androidgallery解析。{

// 選中Gallery中某個圖像時,在ImageSwitcher組件中放大顯示該圖像

imageSwitcher.setImageResource(resIds[position % resIds.length]);

}

@Override

public void onNothingSelected(AdapterView> parent)

gallery是什么,{

}

@Override

// ImageSwitcher組件需要這個方法來創建一個View對象(一般為ImageView對象)

//? 來顯示圖像

public View makeView()

android imageview設置圖片、{

ImageView imageView = new ImageView(this);

imageView.setBackgroundColor(0xFF000000);

imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);

imageView.setLayoutParams(new ImageSwitcher.LayoutParams(

LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

android圖片旋轉代碼、return imageView;

}

@Override

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

android可視化界面設計、setContentView(R.layout.main);

gallery = (Gallery) findViewById(R.id.gallery);

imageAdapter = new ImageAdapter(this);

gallery.setAdapter(imageAdapter);

gallery.setOnItemSelectedListener(this);

imageSwitcher = (ImageSwitcher) findViewById(R.id.imageswitcher);

// 設置ImageSwitcher組件的工廠對象

imageSwitcher.setFactory(this);

// 設置ImageSwitcher組件顯示圖像的動畫效果

imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,

android.R.anim.fade_in));??????? imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,

android.R.anim.fade_out));

}

總結

在本文介紹了如何實現可循環顯示的Gallery組件。實際上,這個循環顯示只是一個偽循環,不過由于getCount方法返回的圖像總數很大(超過20億),這就意味著已經非常接近無限循環了。實現循環顯示圖像的關鍵點有如下兩個:

1.? getCount方法返回一個很大的整數值(例如,Integer.MAX_VALUE)。

2.? 在getView方法中通過取余的方法來循環獲得圖像的資源ID。

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/152196.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息