android:使用gallery和imageSwitch製作可左右迴圈滑動的圖片瀏覽器

來源:互聯網
上載者:User

android:使用gallery和imageSwitch製作可左右迴圈滑動的圖片瀏覽器

為了使圖片瀏覽器左右無限迴圈滑動 我們要自訂gallery的adapter

如果要想自訂adapter首先要瞭解這幾個方法

@Overridepublic int getCount() {// TODO Auto-generated method stubreturn 0;}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn null;}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn 0;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stubreturn null;}

其中getCount方法 是返回資料來源的數量

 

getItem方法 返回的是一個object對象 也就是返回目前容器中資料ID position所對應的對象

getItemId 返回目前容器中的資料ID

getView取得目前要顯示的View

如果要實現左右迴圈滑動 首先我們要返回資料來源的數量為最大值 然後把所有資料的ID對原本資料來源的數量取餘 最後設定gallery初始的位置在0-最大值的中間即可

更改後的adapter就是這樣

package com.example.imageswitcher;import android.content.Context;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageView;import android.widget.ImageView.ScaleType;public class MyAdapter extends BaseAdapter{private int id_image[];private Context contex;public MyAdapter(Context contex,int id_image[]) {this.contex=contex;this.id_image=id_image;}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn Integer.MAX_VALUE;}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn id_image[position%id_image.length];}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn position%id_image.length;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stubImageView imageView=new ImageView(contex);imageView.setBackgroundResource(id_image[position%id_image.length]);imageView.setLayoutParams(new Gallery.LayoutParams(250, 200));imageView.setScaleType(ScaleType.FIT_XY);return imageView;}}
MainActivity
package com.example.imageswitcher;import android.os.Bundle;import android.app.Activity;import android.view.LayoutInflater.Factory;import android.view.Menu;import android.view.View;import android.view.Window;import android.view.animation.AnimationUtils;import android.widget.AdapterView;import android.widget.AdapterView.OnItemSelectedListener;import android.widget.Gallery;import android.widget.ImageSwitcher;import android.widget.ImageView;import android.widget.ImageView.ScaleType;import android.widget.ViewSwitcher.ViewFactory;public class MainActivity extends Activity implements OnItemSelectedListener,ViewFactory{private ImageSwitcher imageSwitcher;private Gallery gallery;private int id_image[] = { R.drawable.beauty1, R.drawable.beauty2,R.drawable.beauty3, R.drawable.beauty4, R.drawable.beauty5,R.drawable.beauty6, R.drawable.beauty7, R.drawable.beauty8,R.drawable.beauty9};private MyAdapter myAdapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_main);gallery = (Gallery) findViewById(R.id.id_gallery);imageSwitcher = (ImageSwitcher) findViewById(R.id.id_imageSwitcher);myAdapter=new MyAdapter(this, id_image);imageSwitcher.setFactory(this);gallery.setOnItemSelectedListener(this);//設定淡入淡出效果imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));gallery.setAdapter(myAdapter);//一定不要忘記 設定gallery的初始位置為中間即可gallery.setSelection(id_image.length*100);}@Overridepublic void onItemSelected(AdapterView parent, View view, int position,long id) {// TODO Auto-generated method stubimageSwitcher.setBackgroundResource(id_image[position%id_image.length]);}@Overridepublic void onNothingSelected(AdapterView parent) {// TODO Auto-generated method stub}@Overridepublic View makeView() {// TODO Auto-generated method stubImageView image=new ImageView(this);image.setScaleType(ScaleType.FIT_CENTER);return image;}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.