欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android控件之Gallery用法實例分析

 更新時間:2015年09月08日 22:35:04   作者:Ruthless  
這篇文章主要介紹了Android控件之Gallery用法,以完整實例形式較為詳細(xì)的分析了Gallery控件實現(xiàn)圖像顯示的技巧,具有一定參考借鑒價值,需要的朋友可以參考下

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

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

目錄結(jié)構(gòu)

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: 設(shè)置未選中的條目的透明度(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);
  // 設(shè)置ImageSwitcher組件的工廠對象
  imageSwitcher.setFactory(this);
  // 設(shè)置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組件需要這個方法來創(chuàng)建一個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;
 }
 //用于返回圖像總數(shù),要注意的是,這個總數(shù)不能大于圖像的實際數(shù)(可以小于圖像的實際數(shù)),否則會拋出越界異常。
 public int getCount() {
  //優(yōu)化一
  //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居中顯示,當(dāng)圖片長/寬超過View的長/寬,則截取圖片的居中部分顯示
 //CENTER_CROP/centerCrop 按比例擴大圖片的size居中顯示,使得圖片長 (寬)等于或大于View的長(寬)
 //CENTER_INSIDE/centerInside 將圖片的內(nèi)容完整居中顯示,通過按比例縮小 或原來的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);
  //優(yōu)化二,通過取余來循環(huán)取得imageIDs數(shù)組中的圖像資源ID,取余可以大大較少資源的浪費
  iv.setImageResource(imageIDs[position%imageIDs.length]);
  iv.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
  iv.setLayoutParams(new LinearLayout.LayoutParams(77,77));//把圖片縮小原來的60%
  return iv;
 }
}

運行結(jié)果

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

相關(guān)文章

  • Android記事本項目開發(fā)

    Android記事本項目開發(fā)

    這篇文章主要為大家詳細(xì)介紹了Android記事本項目開發(fā)的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Android應(yīng)用更新之自動檢測版本及自動升級

    Android應(yīng)用更新之自動檢測版本及自動升級

    這篇文章主要為大家詳細(xì)介紹了Android應(yīng)用更新之自動檢測版本及自動升級,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-09-09
  • Android游戲源碼分享之2048

    Android游戲源碼分享之2048

    本文主要是給大家分享了安卓版的游戲2048的源碼,以及制作思路,是篇非常不錯的文章,有需要的朋友可以參考下
    2014-10-10
  • Android實現(xiàn)圖片文字識別

    Android實現(xiàn)圖片文字識別

    這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)圖片文字識別,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • Android 自定義彈出菜單和對話框功能實例代碼

    Android 自定義彈出菜單和對話框功能實例代碼

    Android 開發(fā)當(dāng)中,可能會存在許多自定義布局的需求,比如自定義彈出菜單(popupWindow),以及自定義對話框(Dialog)。下面通過本文給大家介紹Android 自定義彈出菜單和對話框功能實例代碼,感興趣的朋友一起看看吧
    2017-08-08
  • android 修改launcher行數(shù)和列數(shù)的方法

    android 修改launcher行數(shù)和列數(shù)的方法

    這篇文章主要介紹了android 修改launcher行數(shù)和列數(shù)的方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-07-07
  • Android之ArcSlidingHelper制作圓弧滑動效果

    Android之ArcSlidingHelper制作圓弧滑動效果

    這篇文章主要介紹了Android之ArcSlidingHelper制作圓弧滑動效果,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-08-08
  • Android中Activity和Fragment傳遞數(shù)據(jù)的兩種方式

    Android中Activity和Fragment傳遞數(shù)據(jù)的兩種方式

    本篇文章主要介紹了Android中Activity和Fragment傳遞數(shù)據(jù)的兩種方式,非常具有實用價值,需要的朋友可以參考下
    2017-09-09
  • Android?Xml轉(zhuǎn)換為View過程詳解

    Android?Xml轉(zhuǎn)換為View過程詳解

    這篇文章主要為大家介紹了Android?Xml轉(zhuǎn)換為View實現(xiàn)過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • Android 仿淘寶商品屬性標(biāo)簽頁

    Android 仿淘寶商品屬性標(biāo)簽頁

    這篇文章主要介紹了Android 仿淘寶商品屬性標(biāo)簽頁的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-11-11

最新評論