0

可能重复:
如何在 android 的图库中设置三乘三图像?

我是安卓新手。我必须在我的应用程序中添加画廊。这个画廊使用数组列表从可绘制文件夹中获取图像并且有两个按钮“下一个”和“上一个”。现在我的问题是首先从可绘制文件夹的数组列表中显示三个图像,然后当我按下下一个按钮时,再显示另外三个图像,依此类推。同样在上一个按钮上。这在纵向和横向模式下显示相同。

请帮忙....?

谢谢..

这是我的代码请帮助我....

package com.chirag.AppGallery;

import android.R.color;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.Gallery.LayoutParams;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;

public class ViewAppGalleryActivity extends Activity {
    // take an array of integer type and set its value
    Integer[] mImageIds = { 
        R.drawable.gallery_photo_19,
        R.drawable.gallery_photo_20,
        R.drawable.gallery_photo_21,
        R.drawable.gallery_photo_22, 
        R.drawable.gallery_photo_23,
        R.drawable.gallery_photo_24
        R.drawable.gallery_photo_25,
        R.drawable.gallery_photo_15,
        R.drawable.gallery_photo_16,
        R.drawable.gallery_photo_18,
        R.drawable.gallery_photo_9,
        R.drawable.gallery_photo_10,
        R.drawable.gallery_photo_11,
        R.drawable.gallery_photo_12,
        R.drawable.gallery_photo_26,
        R.drawable.gallery_photo_27, 
        R.drawable.gallery_photo_28,
        R.drawable.gallery_photo_29, 
        R.drawable.gallery_photo_30
                };


    private int imageWidth;
    private ImageView leftArrowImageView;
    private int selectedImagePosition = 0;
    private ImageView rightArrowImageView;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // take an instance of Gallery

         DisplayMetrics metrics = new DisplayMetrics();
         getWindowManager().getDefaultDisplay().getMetrics(metrics);


         final ExtendedGallery g = (ExtendedGallery)findViewById(R.id.gallery);
       //  g.setScrollingEnabled(false);
         MarginLayoutParams mlp = (MarginLayoutParams)g.getLayoutParams();
         mlp.setMargins(-(metrics.widthPixels/2 +60 ), mlp.topMargin,
                     mlp.rightMargin, mlp.bottomMargin);
        // set adapter to gallery calling ImageAdapter class
        g.setAdapter(new ImageAdapter(this, mImageIds));

        g.setOnItemClickListener(new OnItemClickListener() {



            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                            ImageView imgView=(ImageView)findViewById(R.id.imageView1);
                            imgView.setImageResource(mImageIds [position]);


       if (selectedImagePosition > 0 && selectedImagePosition <mImageIds.length - 3)
                   {

                    leftArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.                                                                                                                               arrow_left_enabled));
                    rightArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.                                                                     arrow_right_enabled));

                  }
         else if (selectedImagePosition == 0) {

                    leftArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.                                                                     arrow_left_disabled));
                    rightArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.                                                                      arrow_right_enabled));
                        } 
            else if (selectedImagePosition == mImageIds.length - 3 || selectedImagePosition == mImageIds.                                           length - 2 || selectedImagePosition == mImageIds.length - 1) 
                  {
                    leftArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.                                                                        arrow_left_enabled));
                    rightArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.                                                                      arrow_right_disabled));

                    }

            }
        });

        g.setOnItemSelectedListener(new OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {

                     selectedImagePosition = pos;

                if (selectedImagePosition > 0 && selectedImagePosition <mImageIds.length - 3) 
                {

                    leftArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.                                                                          arrow_left_enabled));
                    rightArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.                                                                         arrow_right_enabled));

                } 
                else if (selectedImagePosition == 0)

                 {

                    leftArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.                                                                          arrow_left_disabled));
                    rightArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.                                                                           arrow_right_enabled));
                } 
                else if
                 (selectedImagePosition == mImageIds.length - 3 || selectedImagePosition == mImageIds.                                          length - 2 || selectedImagePosition == mImageIds.length - 1 )
                  {
                    //leftArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_left_enabled));
                    rightArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.                                                                         arrow_right_disabled));

                }


            }

            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });


        leftArrowImageView = (ImageView) findViewById(R.id.left_arrow_imageview);
        rightArrowImageView = (ImageView) findViewById(R.id.right_arrow_imageview);

        leftArrowImageView.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                if (selectedImagePosition > 2) {

                    --selectedImagePosition;
                    --selectedImagePosition;
                    --selectedImagePosition;

                }else if(selectedImagePosition == 2)
                {
                    --selectedImagePosition;
                    --selectedImagePosition;
                }else if(selectedImagePosition == 1)
                {
                    --selectedImagePosition;
                }


                 g.setSelection(selectedImagePosition, false);
                //g.scrollBy(20, 0);

                    //leftArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_left_disabled));



            }
        });

        rightArrowImageView.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {




                if (selectedImagePosition < mImageIds.length-3 ) {
                    ++selectedImagePosition;
                    ++selectedImagePosition;
                    ++selectedImagePosition;
                }
                g.setSelection(selectedImagePosition, false);

            }
        });



    }



    public class ImageAdapter extends BaseAdapter{
        // Take variable of type Context and String Array because we
        // get the context and array from called class
        Context c;
        Integer[] ImageIds;
        // Take INT variable for saving background id
        int mGalleryItemBackground;
        public ImageAdapter(Context c ,Integer[] imageIds){
            // pass the value to local variable of this class
            this.c = c;
            this.ImageIds = imageIds;

            // get an instance of TypedArray and Retrieve styled attribute
            // information in this Context's theme
            TypedArray a = c.obtainStyledAttributes(R.styleable.GalleryExample);
            // get ResourceId from attr and set it to mGalleryItemBackground
            mGalleryItemBackground = a.getResourceId(
                    R.styleable.GalleryExample_android_galleryItemBackground, 0);
          //Give back a previously retrieved StyledAttributes, for later re-use. 
            a.recycle();
        }
        public int getCount() {
            // TODO Auto-generated method stub
            // This will return the length of array

            return ImageIds.length;
        }

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

        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            // Take an ImageView of Context c
            ImageView mImageSelected = new ImageView(c);
            // set ImageView Property

        /*  if (position >= mImageIds.length)
            { 
                position = position % mImageIds.length; 
            }
*/
            mImageSelected.setImageResource(ImageIds[position]);
            //mImageSelected.setBackgroundResource(mGalleryItemBackground);
            mImageSelected.setScaleType(ImageView.ScaleType.FIT_XY);
            //mImageSelected.setLayoutParams(new                               Gallery.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

                mImageSelected.setLayoutParams(new Gallery.LayoutParams(200,170));

            // return ImageView
            return mImageSelected;
        }


    }


}

这是我的 xml 文件

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

      <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        >



    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/gallery" >

        <ImageView
            android:id="@+id/left_arrow_imageview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/relativeLayout1"
            android:layout_marginTop="50dp"
            android:src="@drawable/arrow_left_disabled" />

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/relativeLayout2"
     android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/gallery"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="50dp"
        android:layout_toLeftOf="@+id/relativeLayout1" >

        <com.chirag.AppGallery.ExtendedGallery
            android:id="@+id/gallery"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:spacing="10px" />

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@+id/gallery" >

        <ImageView
            android:id="@+id/right_arrow_imageview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/relativeLayout1"
            android:layout_marginTop="50dp"
            android:src="@drawable/arrow_right_enabled" />

    </RelativeLayout>
</RelativeLayout>

      <RelativeLayout
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_below="@+id/relativeLayout1"
          android:layout_marginTop="14dp" >

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_alignParentBottom="true"
                android:layout_alignParentLeft="true"
                android:layout_marginBottom="15dp"
                android:layout_marginTop="10dp"
                android:src="@drawable/gallery_photo_19" />

      </RelativeLayout>

</RelativeLayout>
4

1 回答 1

0

在此处输入图像描述http://www.java-samples.com/showtutorial.php?tutorialid=1519

在此图像中查看并在名为 layout-land 的 res 中创建新文件夹,并在其中创建与布局中文件名称相同的 xml 文件。并在横向模式下根据需要设计此文件。使用此代码并放入画廊视图下方的按钮。

使用此代码根据您的需要在按钮单击上移动图库。

Gallery gv;
gv.setSelection(position);
于 2012-09-28T09:26:17.627 回答