溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Android使用gallery和imageSwitch制作可左右循環(huán)滑動(dòng)的圖片瀏覽器

發(fā)布時(shí)間:2020-09-19 01:33:11 來源:腳本之家 閱讀:150 作者:甄情 欄目:移動(dòng)開發(fā)

效果圖:

Android使用gallery和imageSwitch制作可左右循環(huán)滑動(dòng)的圖片瀏覽器

為了使圖片瀏覽器左右無限循環(huán)滑動(dòng) 我們要自定義gallery的adapter

如果要想自定義adapter首先要了解這幾個(gè)方法

@Override 
 public int getCount() { 
  // TODO Auto-generated method stub 
  return 0; 
 } 

 @Override 
 public Object getItem(int position) { 
  // TODO Auto-generated method stub 
  return null; 
 } 

 @Override 
 public long getItemId(int position) { 
  // TODO Auto-generated method stub 
  return 0; 
 } 
 @Override 
 public View getView(int position, View convertView, ViewGroup parent) { 
  // TODO Auto-generated method stub 
  return null; 
 } 

其中g(shù)etCount方法 是返回?cái)?shù)據(jù)源的數(shù)量

getItem方法 返回的是一個(gè)object對(duì)象 也就是返回目前容器中數(shù)據(jù)ID position所對(duì)應(yīng)的對(duì)象

getItemId 返回目前容器中的數(shù)據(jù)ID

getView取得目前要顯示的View

如果要實(shí)現(xiàn)左右循環(huán)滑動(dòng) 首先我們要返回?cái)?shù)據(jù)源的數(shù)量為最大值 然后把所有數(shù)據(jù)的ID對(duì)原本數(shù)據(jù)源的數(shù)量取余  最后設(shè)置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; 
 } 

 @Override 
 public int getCount() { 
  // TODO Auto-generated method stub 
  return Integer.MAX_VALUE; 
 } 

 @Override 
 public Object getItem(int position) { 
  // TODO Auto-generated method stub 
  return id_image[position%id_image.length]; 
 } 

 @Override 
 public long getItemId(int position) { 
  // TODO Auto-generated method stub 
  return position%id_image.length; 
 } 

 @Override 
 public View getView(int position, View convertView, ViewGroup parent) { 
  // TODO Auto-generated method stub 
  ImageView 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; 
 @Override 
 protected 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); 
  //設(shè)置淡入淡出效果 
  imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in)); 
  imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out)); 
  gallery.setAdapter(myAdapter); 
  //一定不要忘記 設(shè)置gallery的初始位置為中間即可 
  gallery.setSelection(id_image.length*100); 
 } 
 @Override 
 public void onItemSelected(AdapterView<?> parent, View view, int position, 
   long id) { 
  // TODO Auto-generated method stub 
  imageSwitcher.setBackgroundResource(id_image[position%id_image.length]); 
 } 
 @Override 
 public void onNothingSelected(AdapterView<?> parent) { 
  // TODO Auto-generated method stub 
 } 
 @Override 
 public View makeView() { 
  // TODO Auto-generated method stub 
  ImageView image=new ImageView(this); 
  image.setScaleType(ScaleType.FIT_CENTER); 
  return image; 
 } 
} 

以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持億速云!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI