Search code examples
androidgestures

sliding between images android


I am relatively new to Android Animation and Gestures.

I have 15 images that I want to slide around. Only one image shows up on the screen at a time and when I slide L->R on the first image, the 2nd image shows up and so on - like a slideshow. I looked at the Android Gallery tutorial but I don't want the thumbnails to be shown. My understanding is to use an ImageView and keep changing the images. Is this the right way or is there a better approach to this?


Solution

  • That way you wont see the fling efect.

    there is one way of doing that with the gallery.

    clreate the galery like this:

    <Gallery xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:id="@+id/HorizontalGallery"
        android:gravity="center_vertical" android:spacing="2px"/>
    

    on the getview you have to:

    public View getView(int position, View convertView, ViewGroup parent) {
    
    ImageView i = new ImageView(_Context);
    
    i.setImageResource(R.drawable.YourPicture);
    i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
    
    //setting the scale:
    
    int viewWidthtmp;
    int viewHeighttmp;
    
    if(getHeight() == 0)
    {
        if(_horizGallery.getWidth() == 0){
            viewWidthtmp = _horizGallery.getWidth();
            viewHeighttmp = _horizGallery.getHeight();
        }
        else
        {
            viewWidthtmp = _screenWidth;
            viewHeighttmp = _screenHeight;
        }
    
    //getting the size of the image.
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true; //returns null, but fills the out methods
    bm = BitmapFactory.decodeResource(getResources(), R.drawable.YourPicture, o);
    if(o.outHeight> viewHeight || o.outWidth> viewWidth) 
       {i.setScaleType(ImageView.ScaleType.CENTER_INSIDE);}
    else
       {i.setScaleType(ImageView.ScaleType.FIT_CENTER);}
    
    //DO NOT ADD the line below
    //i.setBackgroundResource(mGalleryItemBackground);
    
    return i;
    
    }
    

    You also have to declare 2 global variables variables and initialize them in the OnCreate of the activity.

    public class ScrollingGallery extends Activity
    {
        private int _screenWidth;
        private int _screenHeight;
    
    
    ...
    
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.scrollingallery);
    
            Display display = getWindowManager().getDefaultDisplay(); 
            _screenWidth = display.getWidth();
            _screenHeight = display.getHeight();
    
    ...
    

    After that you just have to mak the gallery scroll with a timer.

    if I'm not mistaken, this should work wth a full page gallery. Code is a little long and i just wrote it so might have a bug.