国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android控件之Gallery用法實例分析

2020-04-11 11:26:38
字體:
來源:轉載
供稿:網友

本文實例講述了Android控件之Gallery用法。分享給大家供大家參考。具體如下:
Gallery組件主要用于橫向顯示圖像列表,不過按常規做法。Gallery組件只能有限地顯示指定的圖像。也就是說,如果為Gallery組件指定了10張圖像,那么當Gallery組件顯示到第10張時,就不會再繼續顯示了。這雖然在大多數時候沒有什么關系,但在某些情況下,我們希望圖像顯示到最后一張時再重第1張開始顯示,也就是循環顯示。要實現這種風格的Gallery組件,就需要對Gallery的Adapter對象進行一番改進。

以下通過Gallery模擬循環顯示圖像,在單擊某一個Gallery組件中的圖像時在下方顯示一個放大的圖像(使用ImageSwitcher組件)。

目錄結構

main.xml布局文件:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"  android:layout_width="fill_parent" android:layout_height="fill_parent"> <!-- android:unselectedAlpha: 設置未選中的條目的透明度(Alpha)。該值必須是float類型,比如:“1.2”。 --> <Gallery android:layout_width="fill_parent"  android:layout_height="wrap_content"   android:spacing="10dip"   android:unselectedAlpha="1.2"  android:id="@+id/gallery"  android:layout_marginTop="30dp"/> <ImageSwitcher android:id="@+id/imageSwitcher"  android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:layout_marginTop="30dp" /></LinearLayout>

GalleryActivity類:

package com.ljq.ga;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.animation.AnimationUtils;import android.widget.AdapterView;import android.widget.Gallery;import android.widget.ImageSwitcher;import android.widget.ImageView;import android.widget.AdapterView.OnItemClickListener;import android.widget.AdapterView.OnItemSelectedListener;import android.widget.LinearLayout.LayoutParams;import android.widget.ViewSwitcher.ViewFactory;public class GalleryActivity extends Activity implements ViewFactory { private Gallery gallery = null; private ImageSwitcher imageSwitcher=null; int[] imageIDs={   R.drawable.p1,R.drawable.p2,R.drawable.p3,    R.drawable.p4,R.drawable.p5,R.drawable.p6,   R.drawable.p7,R.drawable.p8 }; @Override public void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.main);  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 = (Gallery) findViewById(R.id.gallery);  ImageViewAdapter adapter=new ImageViewAdapter(GalleryActivity.this, imageIDs);  gallery.setAdapter(adapter);  gallery.setOnItemSelectedListener(new OnItemSelectedListener(){   public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {    //選中Gallery中某個圖像時,在ImageSwitcher組件中放大顯示該圖像    imageSwitcher.setImageResource(imageIDs[position%imageIDs.length]);   }   public void onNothingSelected(AdapterView<?> arg0) {   }  });  gallery.setOnItemClickListener(new OnItemClickListener(){   public void onItemClick(AdapterView<?> parent, View view, int position, long id) {    Log.i("ljq", "parent="+parent.getClass().getName()); //Gallery    Log.i("ljq", "view="+view.getClass().getName()); //ImageView    Log.i("ljq", "position=" + position); //1    Log.i("ljq", "id=" + id);//1    Gallery gl=(Gallery)parent;    ImageView iv=(ImageView)view;   }  }); } // ImageSwitcher組件需要這個方法來創建一個View對象(一般為ImageView對象) // 來顯示圖像 public View makeView() {  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));  return imageView; }}

ImageViewAdapter自定義適配器:

package com.ljq.ga;import android.content.Context;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.LinearLayout;public class ImageViewAdapter extends BaseAdapter{ private int[] imageIDs=null; private Context context=null; public ImageViewAdapter(Context context, int[] imageIDs) {  this.context=context;  this.imageIDs=imageIDs; } //用于返回圖像總數,要注意的是,這個總數不能大于圖像的實際數(可以小于圖像的實際數),否則會拋出越界異常。 public int getCount() {  //優化一  //return imageIDs.length;  return Integer.MAX_VALUE; } public Object getItem(int position) {  return imageIDs[position]; } public long getItemId(int position) {  return position; } //ScaleType的用法 //CENTER/center 按圖片的原來size居中顯示,當圖片長/寬超過View的長/寬,則截取圖片的居中部分顯示 //CENTER_CROP/centerCrop 按比例擴大圖片的size居中顯示,使得圖片長 (寬)等于或大于View的長(寬) //CENTER_INSIDE/centerInside 將圖片的內容完整居中顯示,通過按比例縮小 或原來的size使得圖片長/寬等于或小于View的長/寬 //FIT_CENTER/fitCenter 把圖片按比例擴大/縮小到View的寬度,居中顯示 //FIT_END/fitEnd 把 圖片按比例擴大/縮小到View的寬度,顯示在View的下部分位置 //FIT_START/fitStart 把 圖片按比例擴大/縮小到View的寬度,顯示在View的上部分位置 //FIT_XY/fitXY 把圖片 不按比例 擴大/縮小到View的大小顯示 //MATRIX/matrix 用矩陣來繪制 public View getView(int position, View convertView, ViewGroup parent) {  ImageView iv = new ImageView(context);  //優化二,通過取余來循環取得imageIDs數組中的圖像資源ID,取余可以大大較少資源的浪費  iv.setImageResource(imageIDs[position%imageIDs.length]);  iv.setScaleType(ImageView.ScaleType.CENTER_INSIDE);  iv.setLayoutParams(new LinearLayout.LayoutParams(77,77));//把圖片縮小原來的60%  return iv; }}

運行結果

希望本文所述對大家的Android程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 措美县| 琼中| 海伦市| 公安县| 拜泉县| 东兰县| 中宁县| 墨玉县| 北海市| 安徽省| 香河县| 新疆| 永定县| 金坛市| 许昌县| 西宁市| 扶余县| 山阴县| 文成县| 松溪县| 丹巴县| 皋兰县| 瑞丽市| 江陵县| 谷城县| 商河县| 濮阳县| 东山县| 莱州市| 枣庄市| 娱乐| 库伦旗| 桃源县| 鄯善县| 浙江省| 集贤县| 漳州市| 高邮市| 运城市| 芒康县| 博客|