首页 > 代码库 > 王立平--Gallery:实现图片的左右滑动

王立平--Gallery:实现图片的左右滑动

技术分享


<span style="font-size:18px;color:#330033;">package com.main;

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

@SuppressWarnings("deprecation")
public class MainActivity extends Activity {
     Gallery gallery;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gallery = (Gallery)findViewById(R.id.gallery);
        gallery.setAdapter(new ImageAdapter(this));
        gallery.pointToPosition(0, 0);
   
    }

    
    class ImageAdapter extends BaseAdapter{

          private Context context;
          //图片源数组
          private Integer[] imageInteger={

                 R.drawable.ic_launcher,
                 R.drawable.bg3,
                 R.drawable.ic_launcher,
                 R.drawable.bg3,
                 R.drawable.ic_launcher,
                 R.drawable.bg3,
                 R.drawable.ic_launcher,
                 R.drawable.bg3
          };

          public ImageAdapter(Context c){

             context = c;

          }

          @Override

          public int getCount() {

             return imageInteger.length;

          }

          @Override

          public Object getItem(int position) {

             return position;

          }

          @Override

          public long getItemId(int position) {

             // TODO Auto-generated method stub

             return position;

          }

          @Override

          public View getView(int position, View convertView, ViewGroup parent) {

             ImageView imageView = new ImageView(context);

             imageView.setImageResource(imageInteger[position]);

             imageView.setScaleType(ImageView.ScaleType.FIT_XY);

              return imageView;

          }}


}</span>
-----------------------------------------------------------------------------------------
<span style="font-size:18px;color:#330033;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 
    
   <Gallery 
       android:id="@+id/gallery" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_alignParentTop="true" 
       android:gravity="fill_vertical" 
       android:spacing="10dp" 
       android:background="#0000FF" 
       android:padding="0dp" 
       /> 
   
</RelativeLayout>

 </span>






王立平--Gallery:实现图片的左右滑动