1

It's been a long time since I have worked on an android app. I am looking for the opinions and suggestions from all of yinz who develop on android more often than I do. I am trying to make an app that allows users browse thru an image gallery using Next/Previous buttons. The gallery will be housed on my webserver. I already have a way to send the server the ID number of the current image and which direction (next/previous) it is moving. This will return some json for the current ID and the url. My question is what viewer object should I use. I want to be able to do still, Animated gif/png and eventually load youtube vids or other vids on the site. I know that WebView can do it all to an extent. but if i was gonna make a mobile site, I would just do that. The Imageview doesn't do animations. Where do I even start with the whole thing? Can someone out there give me some suggestions or point me to a tutorial or something that would help me decide on where I can start.

Thanks ahead of time.

Guy Cothal
  • 1,268
  • 1
  • 10
  • 20

2 Answers2

0

I spent this past weekend trying to get animated GIFs working on my android app, and let me tell you: you can get them working, but never well. Be prepared for lots of OutOfMemory fatal exceptions and horribly laggy UI performance. I ended up streaming WebM files into a VideoView instead, which is incredibly fast and efficient. Since it sounds like you are curating some set of images on your server, I highly advise you convert all your animated GIFs to WebM format, and use a VideoView to display them.

Here's some of my code to get you started:

VideoView videoView = (VideoView) layout.findViewById(R.id.videoView);
videoView.setVideoPath(url);
videoView.start();
videoView.setOnPreparedListener(new OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer mp) {
        mp.setLooping(true);
    }
});
// videoView.setMediaController(new MediaController(singleton));

Use the looper without the MediaController (as shown above) to emulate normal animated GIF behavior. Alternatively, uncomment the MediaController code and get rid of the looper to display more like a traditional video view.

And if you must absolutely without any exception use actual animated GIFs, here's some good libraries to check out:

GifAnimationDrawable is a wrapper around Android's Movie class and AnimationDrawable. This is the easiest to implement and fast, but is error prone because every single frame of the GIF is loaded into memory upon animation.

android-gif-drawable is probably the best solution you can find. It is compiled with a C module using the Native Development Kit, so the performance is better, but it's just a tad more difficult to implement.

Hope all this info helps, and let me know if you have any more questions!

Mr. Kevin Thomas
  • 837
  • 2
  • 13
  • 21
0
Integer[] imageIDs = {
        R.drawable.elphanto,
        R.drawable.large,
        R.drawable.night,
        R.drawable.anim_flag_iceland,
        R.drawable.galaxy

        };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gallary_view);

    Gallery gallery = (Gallery) findViewById(R.id.gallery1);
    gallery.setAdapter(new ImageAdapter(this));
    gallery.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position,long id)
{

            if(position==3||position==4){
                GifImageView gifImageView =(GifImageView)findViewById(R.id.giv_demo);
                gifImageView.setVisibility(View.VISIBLE);
                ((ImageView) findViewById(R.id.image1)).setVisibility(View.GONE);
                try {
                    GifDrawable gifDrawable = new GifDrawable(getResources(), R.drawable.anim_flag_iceland);
                    gifImageView.setImageDrawable(gifDrawable);

                    Toast.makeText(getBaseContext(),"pic" + (position + 1) + " selected",
                            Toast.LENGTH_SHORT).show();
                    gifImageView.setImageResource(imageIDs[position]);

                } catch (NotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
            else{
                ((GifImageView)findViewById(R.id.giv_demo)).setVisibility(View.GONE);
                ((ImageView) findViewById(R.id.image1)).setVisibility(View.VISIBLE);
                Toast.makeText(getBaseContext(),"pic" + (position + 1) + " selected",
                        Toast.LENGTH_SHORT).show();
                // display the images selected
                ImageView imageView = (ImageView) findViewById(R.id.image1);
                imageView.setImageResource(imageIDs[position]);
            }


    }
});
}


public class ImageAdapter extends BaseAdapter {
    private Context context;
    private int itemBackground;
    public ImageAdapter(Context c)
    {
        context = c;
        // sets a grey background; wraps around the images
        TypedArray a =obtainStyledAttributes(R.styleable.HelloGallery);
        itemBackground = a.getResourceId(R.styleable.HelloGallery_android_galleryItemBackground, 0);
        a.recycle();
    }
    // returns the number of images
    public int getCount() {
        return imageIDs.length;
    }
    // returns the ID of an item
    public Object getItem(int position) {
        return position;
    }
    // returns the ID of an item
    public long getItemId(int position) {
        return position;
    }                                                                                              
    // returns an ImageView view
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(context);
        imageView.setImageResource(imageIDs[position]);
        imageView.setLayoutParams(new Gallery.LayoutParams(250, 250));
        imageView.setBackgroundResource(itemBackground);
        return imageView;
    }
}


@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

}