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; 
              } 
            
      } 
}

 

gallery 循环现实图片。

05-07

public class Gallery_Activity extends Activity rn /** Called when the activity is first created. */rn private Gallery gallery;rn private ImageView imageView;rn private int images[]=R.drawable.r1,R.drawable.r2,R.drawable.r3,R.drawable.r4,R.drawable.r5;rn @Overridern public void onCreate(Bundle savedInstanceState) rn super.onCreate(savedInstanceState);rn setContentView(R.layout.main);rn gallery=(Gallery) findViewById(R.id.gallery1);rn imageView=(ImageView) findViewById(R.id.imageView1);rn gallery.setSpacing(100);rn gallery.setSelection(1);rn MyGallery_Adpter myGallery_Adpter=new MyGallery_Adpter(this, images);rn gallery.setAdapter(myGallery_Adpter);rn gallery.setOnItemSelectedListener(new OnItemSelectedListener() rnrn public void onItemSelected(AdapterView arg0, View arg1,rn int arg2, long arg3) rn imageView.setImageResource(images[arg2%images.length]);rn rnrn public void onNothingSelected(AdapterView arg0) rn // TODO Auto-generated method stubrn rn );rn rnrnrnpublic class MyGallery_Adpter extends BaseAdapter rn private Context context;rn private int image[];rn private ImageView imageView;rn public MyGallery_Adpter(Context context,int image[]) rn this.context=context;rn this.image=image;rn rn rn public int getCount() rn // TODO Auto-generated method stubrn return image.length;rn rnrn public Object getItem(int arg0) rn // TODO Auto-generated method stubrn return image[arg0];rn rnrn public long getItemId(int position) rn // TODO Auto-generated method stubrn return position;rn rnrn public View getView(int position, View convertView, ViewGroup parent) rn // TODO Auto-generated method stubrn imageView=new ImageView(context);rn imageView.setImageResource(image[position%image.length]);rn imageView.setPadding(2, 2, 2, 2);rn imageView.setLayoutParams(new Gallery.LayoutParams(200, 200));rn return imageView;rn rnrn怎么不能达到效果,该怎么写呢,求大神指导

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试