android學習筆記15——Galley

來源:互聯網
上載者:User

標籤:

Gallery==>畫廊視圖

Gallery和Spinnery父類相同——AbsSpinner,表明Garrey和Spinner都是一個列表框。

兩者之間的區別是:Spinner顯示的是一個垂直列表框,Gallery顯示的是一個水平列表框

           Spinner的作用是供使用者選擇,而Gallery則允許使用者通過拖動來查看上一個、下一個清單項目。

Garrey常用XML屬性:

 android:animationDuration  setAnimationDuration(int) 設定清單項目切換時的動畫幀期間
 android:gravity  setGravity(int)  設定對其方式
 android:spacing  setSpacing(int)  設定Gallery內清單項目之間的間距
 android: unselectedAlpha setUnselectedAlpha(float)  設定沒有選中的清單項目的透明度

 

 

 

注意:

Gallery用法類似Spinner,使用Adapter提供資料來源,Adapter的getView()所返回的View將作為Gallery列表的清單項目;

通過OnItemSelectedListener監聽器監聽選擇項的改變。

執行個體一

布局檔案==》<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity" >    <ImageSwitcher        android:id="@+id/switcher"        android:layout_width="320dp"        android:layout_height="320dp" />    <Gallery        android:id="@+id/gallery"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:spacing="3pt"        android:unselectedAlpha="0.6" /></LinearLayout>代碼實現==》package com.example.mygrallery;import android.os.Bundle;import android.annotation.SuppressLint;import android.app.Activity;import android.app.ActionBar.LayoutParams;import android.content.res.TypedArray;import android.view.Menu;import android.view.View;import android.view.ViewGroup;import android.view.animation.AnimationUtils;import android.widget.AdapterView;import android.widget.AdapterView.OnItemSelectedListener;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageSwitcher;import android.widget.ImageView;import android.widget.ViewSwitcher.ViewFactory;@SuppressWarnings("deprecation")@SuppressLint("InlinedApi")public class MainActivity extends Activity{private int[] ImageIds = new int[]{ R.drawable.one, R.drawable.tw, R.drawable.th, R.drawable.eight, R.drawable.ele,R.drawable.five, R.drawable.four, R.drawable.nice, R.drawable.seven, R.drawable.six,R.drawable.sl, R.drawable.ss, R.drawable.sw, R.drawable.ten, R.drawable.tw,R.drawable.oneowne };@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);final ImageSwitcher switcher = (ImageSwitcher) this.findViewById(R.id.switcher);final Gallery gallery = (Gallery) this.findViewById(R.id.gallery);switcher.setFactory(new ViewFactory(){@Overridepublic View makeView(){ImageView img = new ImageView(MainActivity.this);img.setBackgroundColor(0xff0000);img.setScaleType(ImageView.ScaleType.FIT_CENTER);img.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));return img;}});// 設定圖片更換的動畫效果switcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));switcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));// 建立DataAdapter對象,為gallery提供資料BaseAdapter adapter = new BaseAdapter(){@Overridepublic int getCount(){// TODO Auto-generated method stubreturn ImageIds.length;}@Overridepublic Object getItem(int position){// TODO Auto-generated method stubreturn position;}@Overridepublic long getItemId(int position){// TODO Auto-generated method stubreturn position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent){ImageView img = new ImageView(MainActivity.this);img.setImageResource(ImageIds[position % ImageIds.length]);// 設定ImageView的縮放類型img.setScaleType(ImageView.ScaleType.FIT_XY);img.setLayoutParams(new Gallery.LayoutParams(75, 110));// TypedArray arr= obtainStyledAttributes(R.)// img.setBackgroundResource(resid);return img;}};gallery.setAdapter(adapter);gallery.setOnItemSelectedListener(new OnItemSelectedListener(){@Overridepublic void onItemSelected(AdapterView<?> parent, View view, int position, long id){// TODO Auto-generated method stubswitcher.setImageResource(ImageIds[position % ImageIds.length]);}@Overridepublic void onNothingSelected(AdapterView<?> parent){// TODO Auto-generated method stub}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu){// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}

實現效果如下:

 

android學習筆記15——Galley

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.