AuthorJohannes Borchardt

Tutorial: How to play animated GIFs in Android – Part 1

Animated GIFs in Android is a difficult topic. It is an issue that has been discussed heavily and still, it is very difficult for developers to bring GIFs to life. There are three ways to animate GIFs on Android, each of them has its pros and cons. Each part of this series will cover one of these approaches.

Getting started

For this example, we will use an image I found on gifs.net, it’s this one:

I will store it in our project’s asset folder and name it ‘piggy.gif’. We will also use an Activity to set the views we define as content views.

Approach 1: Using Movie

Android provides the class android.graphics.Movie. This class is capable of decoding and playing InputStreams. So for this approach, we create a class GifMovieView and let it inherit from View:

Public class GifMovieView extends View

Now we create a constructor that receives a Context object and an InputStream. We provide our class a member variable which is an instance of the Movie class. We initialize this member by calling Movie.decodeStream(InputStream):

    private Movie mMovie;

    public GifMovieView(Context context, InputStream stream) {
        super(context);

        mStream = stream;
        mMovie = Movie.decodeStream(mStream);        
    }

Now that our Movie-object is initialized with our InputStrem, we just need to draw it. We can do this by calling draw(Canvas, int, int) on our Movie-object. Because we need a Canvas, we should do this in onDraw(). But before we drawing, we have to tell our object what to render. For that, we need a simple calculation to determine how much time has passed since we started the Movie. To do that, we need another member of the primitive type long, I named it mMoviestart. Now we get a timestamp, for example by calling SystemClock.uptimeMillis() or System.currentTimeMillis(). We determine how much time went by since our movie started and tell our movie to play draw the according frame. After that, we invalidate our view so that it’s redrawn:

private long mMoviestart;

    @Override
    protected void onDraw(Canvas canvas) {
       canvas.drawColor(Color.TRANSPARENT);
       super.onDraw(canvas);
       final long now = SystemClock.uptimeMillis();
       if (mMoviestart == 0) {
          mMoviestart = now;
       }
       final int relTime = (int)((now - mMoviestart) % mMovie.duration());
       mMovie.setTime(relTime);
       mMovie.draw(canvas, 10, 10);
       this.invalidate();
    }

All we have to do now is initialize our new View with a Context and an InputStream and set it as content view, we can do this in our Activity like this:

    // ...
    InputStream stream = null;
    try {
       stream = getAssets().open("piggy.gif");
    } catch (IOException e) {
      e.printStackTrace();
    }
    GifMovieView view = new GifMovieView(this, stream);
    setContentView(view);
    // ...

That was easy, right? So where’s the contra? Well, here it is:

As you can see, the Movie-class is not able to deal with every type of animated GIFs. For some formats, the first frame will be drawn well but every other won’t. So when you walk this route, make sure your GIFs are displayed correctly.
If you want to know another, maybe better way of playing animated GIFs, stay tuned: The next part of this series will come tomorrow.

 

You can checkout the code of the three parts of this series at http://code.google.com/p/animated-gifs-in-android/.

 

As always, please feel free to leave your thoughts in the comments.

Evolution of CPC (Cost per click)

Cost per click or CPC – that’s what its all about when comparing ad networks. There’s a tendency when reading through forums, that the average CPC has dropped noticeable in the last year. I’ve been a user of AdMob and madvertise for some time now, here are my average CPCs:

AdMob

To make a long story short, here’s a diagram:

Evolution of AdMob's CPC

The evolution of AdMob's CPC

As you can see there are ups and downs in the curve but currently, it seems we are more in a boom than in a recession.  The average CPC has been around 4 US-cents, that’s an acceptable value.

madvertise

Here’s the diagram:

The evolution of madvertise's CPC

The evolution of madvertise's CPC

Also madvertise had it’s ups and downs. whil around may this year, CPC dropped to about 10 US-cents, it has increased to almost 20 US-cents since then. It’s remarkable that madvertise’s average CPC was, even at its lowest, still almost twice as high as AdMobs best rate. If they had a better fillrate outside of Europe, I probably wouldn’t had to work anymore.

 

What are your experiences with CPCs? Did you experience some remarkable changes? Please feel free to tell us in the comments.

Steve Jobs has passed away

Yesterday, one of the world’s most innovative people, Steven Paul “Steve” Jobs has passed away. His perfectionism, openness and hunger has made him a inspiration for many. Many of us, including me, would not be where they are today without him.

Here is his inspiring speech at Stanford University 2005:

Thank you Steve, may you rest in peace.

Android Income Report #5: September 11

Another month is over, it’s time for income facts again. Please excuse the delay, I’ll try to be more on time next month.

If you are new to this series, let me explain it to you: Since Android is an open platform, I decided to be open about the income I’m making with my private Android apps too. In the last report I aimed to reach $1,100 for the last month. You will see if it worked out.

For all income reports, please click here.

Which Apps?

3D Invaders – about 107,000 installs (+7k), 15% active

AL Voice Recorder – about 399,000 installs (+17k), 24% active

AL Voice Recorder Ad Free – 747 installs (+27), 41% active

Droid-Blog.net Android App – 235 installs (+44), 26% active

SmsToSpeech full – 686 installs (+11), 42% active

What did I do?

At about the 20th of September it was clear that I wouldn’t hit my goals. Fortunately I was just in between two projects and was able to invest a day. In that time I reworked the AL Voice Recorder technically and added two new features: A seekbar (finally!) and speech to text for the naming of records. Additionally I did something I should have done a long time ago: I integrated madvertise. I also published a minor update for 3D Invaders, but added no big features.

Advertising Stats

Enough talking, lets start with the facts!

Here are some statistics from the two advertising networks I’m using, AdMob and Madvertise. Please read the second income report for an explanation of the following numbers.

AdMob:

Requests: 348,715 (-90k)

Impressions: 324,318 (-96k)

Fill Rate: 93% (-2.79%)

Clicks: 7,390 (-1k)

CTR: 2.28% (+0.28%)

eCPM: $1.04 (+$0.13)

House Ads: 9,620 (-12.7k)

Adjusted Requests:  358,335 (-102k)

Adjusted Fill Rate: 90,51 (-0,65%)

Madvertise

As I’m not using house ads in Madvertise, no adjusted requests and fill rates are shown here.

Requests: 291,556 (+40k)

Impressions: 53,706 (+300)

Fill Rate: 18% (-3%)

Clicks: 2,292(-582)

CTR: 4,27% (-1.1%)

eCPM: $8,19 (-$2.09)

Performances of both networks were not as good as in the previous month. Since I added madvertise to the voice recorder who worse CTR than 3D Invaders, I can probably expect madvertise’s CTR to drop a little further this month.

How much?

This time I split up my ad revenue to give you a better overview of the potential performance of games vs. tools:

Madvertise:

3D Invaders: ~$398,72

AL Voice Recorder: ~$42,01

Madvertise Total: ~$440.73 (-$109.62)

 

AdMob:

3D Invaders: $134.13

AL Voice Recorder: $204.61

AdMob Total: $338,74 (-$41,94)

 

Market sales: ~$65,32 (-$24,08)

In-App purchases: ~$4.46 (-$3.77)

 

Total: ~$849.25 (-179.32)

I missed my goal big time. The retrogressive growth of my app downloads were clearly visible in my income this time. I hope the changes I made and will make will lead my income into the right direction for this month.

What’s next?

In the second half I hope I’ll be able to do a bit more development on my own apps so that I’ll be able to make some new releases soon. We will see.

Since I missed my goal for the last month, this time it again is $1,100.

 

Please feel free to share your own experiences and hints in the comments. Please also don’t hesitate to tell me if there is anything else you’d like to get some information about.

© 2024 Droid-Blog

Theme by Anders NorenUp ↑