I added 2 dummy views at the ends of the Gallery then wrote a
onItemSelectedListener that essentially listens for items 0 and the
last item (the dummy views) and sets the selection to 1 and the last
item -1 respectively.  This creates the bounce me having to do any
animations!  I created a full screen gallery view and each item in the
adapter takes up the entire screen (just like a home screen with
various full screen panels).  I also modified the onFling so the
gallery only moves one screen at a time (much like an Android or
iPhone home screen).

I uploaded the code and a sample movie of what I did on Google Code:

http://code.google.com/p/androidanimations/downloads/detail?name=androidmodifiedgallery.mov&can=2&q=
http://code.google.com/p/androidanimations/downloads/list

Here are some snippets of the modified Gallery:

---------------------
package com.nikb.gallery;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

public class ModifiedGallery extends Gallery {

        private static final boolean ISLOG = true;
        private static final String TAG =
ModifiedGallery.class.getSimpleName();
        private float modifiedVelocityX;

        public ModifiedGallery(Context context) {
                super(context);
                init();
        }

        public ModifiedGallery(Context context, AttributeSet attrs) {
                super(context, attrs);
                init();
        }

        public ModifiedGallery(Context context, AttributeSet attrs, int
defStyle) {
                super(context, attrs, defStyle);
                init();
        }

        private void init() {
                Display display = ((WindowManager)
getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
                int width = display.getWidth();
                int height = display.getHeight();
                if (ISLOG)
                        Log.d(TAG, "Width is " + display.getWidth() + " and 
height is " +
display.getHeight());

                modifiedVelocityX = (float) (width * 1.7);
                setOnItemSelectedListener(new OnItemSelectedListener() {

                        @Override
                        public void onItemSelected(AdapterView<?> arg0, View 
arg1,
                                        int arg2, long arg3) {

                                int pos = getSelectedItemPosition();
                                Log.d(TAG, "Selected item is " + 
getSelectedItemPosition());
                                if (pos == 0) {
                                        Log.d(TAG, "Animating to the first 
item");
                                        setSelection(1);
                                } else {
                                        int count = getCount();
                                        if (pos == count - 1) {
                                                Log.d(TAG, "Animating to " + 
(count - 2));
                                                setSelection(count - 2);
                                        }
                                }
                        }

                        @Override
                        public void onNothingSelected(AdapterView<?> arg0) {
                        }

                });
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float
velocityX,
                        float velocityY) {
                float mod = velocityX < 0 ? -modifiedVelocityX : 
modifiedVelocityX;

                if (getSelectedItemPosition() == 1
                                || getSelectedItemPosition() == 
getAdapter().getCount() - 2) {
                        mod = velocityX < 0 ? -1 : 1;
                }

                if (ISLOG)
                        Log.d(TAG, "Original velocity X was " + velocityX + " & 
modified
velocity is " + mod);
                return super.onFling(e1, e2, mod, velocityY);
        }

        @Override
        public void setAdapter(SpinnerAdapter adapter) {
                super.setAdapter(new BounceGalleryAdapter(adapter));
                setSelection(1); // set it to the first position by default
        }

        class BounceGalleryAdapter extends BaseAdapter {

                private static final int ELASTICITY_DIP = 50;  //how far to 
stretch
at the ends
                private SpinnerAdapter mAdapter;
                private TextView dummyText2;
                private TextView dummyText1;
                private LayoutInflater layoutInflater;


                public BounceGalleryAdapter(SpinnerAdapter adapter) {
                        this.mAdapter = adapter;
                }

                @Override
                public int getCount() {
                        // account for a bounce on both ends so add 2 dummy 
views.
                        return mAdapter.getCount() + 2;
                }

                @Override
                public Object getItem(int position) {
                        return mAdapter.getItem(position);
                }

                @Override
                public long getItemId(int position) {
                        return mAdapter.getItemId(position);
                }

                @Override
                public View getView(int position, View convertView, ViewGroup
parent) {
                        Log.d(TAG, "Getting view for position:" + position);
                        if (layoutInflater == null) {
                                layoutInflater = (LayoutInflater)
getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                        }
                        if (position == 0) // the first position
                        {
                                if (convertView == null || dummyText1 == null) {
                                        dummyText1 = new TextView(getContext());
                                        dummyText1.setLayoutParams(new
Gallery.LayoutParams(ELASTICITY_DIP,
Gallery.LayoutParams.FILL_PARENT));
                                        convertView = dummyText1;
                                }
                                return convertView;
                        } else if (position == getCount() - 1) // the last 
position
                        {
                                if (convertView == null || dummyText2 == null) {
                                        dummyText2 = new TextView(getContext());
                                        dummyText2.setLayoutParams(new
Gallery.LayoutParams(ELASTICITY_DIP,Gallery.LayoutParams.FILL_PARENT));
                                        convertView = dummyText2;
                                }
                                return convertView;
                        } else {
                                return mAdapter.getView(position - 1, 
convertView, parent);
                        }
                }

        }

}

----------------------
package com.nikb.gallery;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class BounceGalleryActivity extends Activity {


    private final class SimpleImageAdapter extends BaseAdapter {
        ImageView one = null;
        ImageView two = null;
        ImageView three = null;

                @Override
                public View getView(int position, View convertView, ViewGroup
parent) {
                        if(position == 0)
                        {
                                if(convertView == null)
                                {
                                one = new ImageView(BounceGalleryActivity.this);
                                one.setBackgroundResource(R.drawable.one);
                                one.setLayoutParams(new
Gallery.LayoutParams(Gallery.LayoutParams.FILL_PARENT,
Gallery.LayoutParams.FILL_PARENT));
                                convertView = one;
                                }
                                return convertView;
                        }
                        else if(position == 1)
                        {
                                if(convertView == null)
                                {
                                two = new ImageView(BounceGalleryActivity.this);
                                two.setBackgroundResource(R.drawable.two);
                                two.setLayoutParams(new
Gallery.LayoutParams(Gallery.LayoutParams.FILL_PARENT,
Gallery.LayoutParams.FILL_PARENT));
                                convertView = two;
                                }
                                return convertView;
                        }
                        else if (position == 2)
                        {
                                if(convertView == null)
                                {
                                three = new 
ImageView(BounceGalleryActivity.this);
                                three.setBackgroundResource(R.drawable.three);
                                three.setLayoutParams(new
Gallery.LayoutParams(Gallery.LayoutParams.FILL_PARENT,
Gallery.LayoutParams.FILL_PARENT));
                                convertView = three;
                                }
                                return convertView;
                        }
                        else
                        {
                                throw new RuntimeException("Incorrect Index " + 
position);
                        }

                }

                @Override
                public long getItemId(int position) {
                        return 0;
                }

                @Override
                public Object getItem(int position) {
                        return null;
                }

                @Override
                public int getCount() {
                        return 3;
                }
        }

        /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
 
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.messages);

        Gallery gallery = (Gallery) findViewById(R.id.gallery);
        gallery.setAdapter(new SimpleImageAdapter());
        gallery.setSpacing(0);
        gallery.setUnselectedAlpha(1);

    }
}


-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to