4

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?

Alex K
  • 8,269
  • 9
  • 39
  • 57
Sagar Hatekar
  • 8,700
  • 14
  • 56
  • 72
  • 1
    You could still use a Gallery; just make it full screen and use the larger images in it in the first place. – Yoni Samlan Apr 01 '11 at 16:17
  • thanks Yoni. With the gallery, I am somehow not getting the same fling effect. Besides, on the gallery thumbnail - I need to display text and when the thumbnail is clicked - I need to open a new gallery. – Sagar Hatekar Apr 01 '11 at 17:42
  • Thanks Yoni! I used Gallery, made the images full screen and then used merge tutorial from here: http://developer.android.com/resources/articles/layout-tricks-merge.html – Sagar Hatekar Apr 04 '11 at 14:40

1 Answers1

7

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.

J-Rou
  • 2,216
  • 8
  • 32
  • 39
  • thanks for your efforts! I actually have to use fling since the images will be changed only when the user slides his finger horizontally across the screen. – Sagar Hatekar Apr 01 '11 at 17:21
  • 1
    Gallery has fling. if you want only one image to scroll while lingigng goto http://stackoverflow.com/questions/2373617/how-to-stop-scrolling-in-a-gallery-widget. – J-Rou Apr 01 '11 at 19:52