Gallery循环拖动

 main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <Gallery android:id="@+id/gallery"
            android:layout_width="fill_parent"
           android:layout_height="wrap_content"   />

</LinearLayout>

Activity

package ntc.gallery;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemSelectedListener;

public class GalleryActivity extends Activity {
    /** Called when the activity is first created. */
 
 
        private Gallery gallery; 
           private int[] image = new int[] { R.drawable.audio_player_bg_bottom, R.drawable.audio_player_bg_top, R.drawable.ic_launcher, R.drawable.media_player_next_button, R.drawable.media_player_play_button_on, R.drawable.media_player_play_button_on, R.drawable.media_player_prev_button_on, R.drawable.media_player_progress_button, 
                   R.drawable.media_player_progress_bg, R.drawable.icon, }; 
          
           public void onCreate(Bundle savedInstanceState) 
         { 
               super.onCreate(savedInstanceState); 
               setContentView(R.layout.main); 
                gallery = (Gallery) findViewById(R.id.gallery); 
                 gallery.setAdapter(new ImageAdapter(this,image)); 
                gallery.setSpacing(1); 
               gallery.setSelection(4); 
               gallery.setOnItemSelectedListener(new OnItemSelectedListener() { 
                    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) 
                  {} 
    
                   public void onNothingSelected(AdapterView<?> arg0) 
                   {} 
              }); 
        } 
    
     
    class ImageAdapter extends BaseAdapter 
        { 
     private int[] image;
     
     
     
             private Context context; 
       
              public ImageAdapter(Context c,int[] image) 
             { 
                 context = c; 
                 this.image=image;
             } 
      
            // 获取图片的个数  
            public int getCount() 
        { 
                return Integer.MAX_VALUE; 
            } 
      
            // 获取图片在库中的位置  
             public Object getItem(int position) 
            { 
                  return position; 
              } 
       
             // 获取图片ID  
             public long getItemId(int position) 
              { 
                return position; 
              } 
       
              public View getView(int position, View convertView, ViewGroup parent) 
              { 
                 ImageView imageView = new ImageView(context); 
                  // 给ImageView设置资源  
                  imageView.setImageResource(image[position % image.length]); 
                 // 设置显示比例类型  
                  imageView.setScaleType(ImageView.ScaleType.FIT_XY); 
                 // 设置布局 图片120*80  
                imageView.setLayoutParams(new Gallery.LayoutParams(60, 60)); 
                  return imageView; 
              } 
            
      } 
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值