Vivek Kalkur
Vivek Kalkur

Reputation: 2186

How to make the images in the GalleryView come to initial position after completion of Scroll?

I will consider a simple GalleryView which consists of few images.I will scroll from left to right or vice versa and then leave. As of now (as shown in the 2 images below) the gallery stops in the middle of the view, leaving some spaces (marked red colour in the image) from the initial position or from the final position whenever the scrolling is complete. My concern is, I want my gallery to acquire the initial position or final position (scroll dependent) after scrolling. How would I do that?

Images:

When left to right scrolling is completed:

When right to left scrolling is completed:

My GalleryView:

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mAudio =(Button)findViewById(R.id.button1);
    mVideo =(Button)findViewById(R.id.button2);
    mImages=(Button)findViewById(R.id.button3);

    mAudio.setOnClickListener(this);
    mVideo.setOnClickListener(this);
    mImages.setOnClickListener(this);

    Gallery ga = (Gallery) findViewById(R.id.Gallery01);
    galleryAdapter = new ImageAdapter(this); 
    galleryAdapter.setResourseArray(image_pics);
    ga.setAdapter(galleryAdapter);

    imageView = (ImageView) findViewById(R.id.ImageView01);
    ga.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
            Toast.makeText(getBaseContext(), "You have selected picture " + (pos + 1) + " of Gallery", Toast.LENGTH_SHORT)
            .show();
            if(activeGallery==AUDIO){
                imageView.setImageResource(audio_pics[pos]);
            }else if(activeGallery==VIDEO){
                imageView.setImageResource(video_pics[pos]);
            }else if(activeGallery==IMAGE){
                imageView.setImageResource(image_pics[pos]);
            }
        }
    });
}

@Override
public void onClick(View view) {
    if (view == mAudio) {
        if (galleryAdapter != null) {
            galleryAdapter.setResourseArray(audio_pics);
            galleryAdapter.notifyDataSetChanged();
            activeGallery=AUDIO;
        }
    } else if (view == mVideo) {
        if (galleryAdapter != null) {
            activeGallery=VIDEO;
            galleryAdapter.setResourseArray(video_pics);
            galleryAdapter.notifyDataSetChanged();
        }
    } else if (view == mImages) {
        if (galleryAdapter != null) {
            activeGallery=IMAGE;
            galleryAdapter.setResourseArray(image_pics);
            galleryAdapter.notifyDataSetChanged();
        }
    }
}

public class ImageAdapter extends BaseAdapter {

    private Context ctx;
    int imageBackground;
    private int[] resourseArray = null;

    public ImageAdapter(Context c) {
        ctx = c;
        TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1);
        imageBackground = ta.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1);
        ta.recycle();
    }

    @Override
    public int getCount() {
        return resourseArray.length;
    }

    public int[] getResourseArray() {
        return resourseArray;
    }

    public void setResourseArray(int[] resourseArray) {
        this.resourseArray = resourseArray;
    }

    @Override
    public Object getItem(int arg0) {

        return arg0;
    }

    @Override
    public long getItemId(int arg0) {

        return arg0;
    }

    @Override
    public View getView(int position, View arg1, ViewGroup arg2) {
        ImageView iv = new ImageView(ctx);
        iv.setImageResource(resourseArray[position]);
        iv.setScaleType(ImageView.ScaleType.FIT_XY);
        iv.setLayoutParams(new Gallery.LayoutParams(150, 120));
        iv.setBackgroundResource(imageBackground);
        return iv;
    }

}

To be simple, I want the elastic effect to these gallery views.

Upvotes: 2

Views: 1141

Answers (1)

anupam sharma
anupam sharma

Reputation: 1705

I know only how to remove space from left side and image will start from left. A structure describing general information about a display, such as its size, density, and font scaling.To access the DisplayMetrics members, initialize an object like this

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

    Gallery g = (Gallery) findViewById(R.id.gallery);

    // set gallery to left side
    MarginLayoutParams mlp = (MarginLayoutParams) g.getLayoutParams();
    mlp.setMargins(-(metrics.widthPixels / 2 + (imageWidth/2)), mlp.topMargin,
                mlp.rightMargin, mlp.bottomMargin);

Upvotes: 2

Related Questions