2

I made a panorama app that scrolls the image automatically, from left to right to left and so on. It works nicely, except for the fact that it doesn't quite scrolls as smoothly (meaning constant velocity) as I would like. It sometimes 'jumps' a bit. It shouldn't be in a device with a core of 1.4 GHz...

I implemented this just by translating the image, then waiting a bit with a Thread.sleep(panoanimspeed); (where 1/1000s <= panoanimspeed <= 1/50s), and so on. Probably not the smartest thing to do...?

Do you know how can I make this smoother? Below is the code of the relevant AsyncTask.

Thanks!

class AnimatePanorama extends AsyncTask<Void, Integer, Void> {
    private float scale = foto.getCropScale() * foto.getBaseScale();
    private Matrix m = foto.matrix;
    @Override
    protected void onProgressUpdate(Integer... values) {
        if (values[0] == 0) {
            m.setScale(scale, scale);
            m.postTranslate(values[1], 0);
            foto.setImageMatrix(m);
        }
    }
    @Override
    protected Void doInBackground(Void... arg) {
        dir = -dir;
        int l, ini = 0;
        float lim = foto.getImageWidth()*scale - foto.getViewWidth();
        float[] mm = new float[9];
        foto.matrix.getValues(mm);
        if (foto.getScale() == foto.getCropScale()) 
               ini =  (int) (dir*(lim*(dir+1)/2 + mm[2]));

        while (true) {
            if(isCancelled()) break;
            l = (int) (lim*(dir+1)/2);
            for (int i = ini; i < lim; i++) {
                if(isCancelled()) break;
                try {
                    Thread.sleep(panoanimspeed);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                publishProgress(0, dir * i - l);
            }
            ini = 0;
            dir = -dir;
        }
        return null;
    }
}
Luis A. Florit
  • 2,169
  • 1
  • 33
  • 58

2 Answers2

0

Create an animation by extending Animation class (http://developer.android.com/reference/android/view/animation/Animation.html) and override the applyTransformation(float interpolatedTime, Transformation t) method.

Ovi
  • 60
  • 8
  • The first thing I tried was with animations, and I thought it will be a very easy thing. The problem is that when you animate an image that does not fit in the view (like a panorama), the animation moves only the portion of the image that is visible. In other words, image pixels are substituted by black pixels, not the pixels of the image. Hope I made myself clear... – Luis A. Florit Jan 03 '13 at 21:00
0

Base your image translation on a "real time" based translation, like X pixels per second.

On the first cycle, save the system time as a base time stamp.

On future loop cycles, calculate the new location based on the time since the base time stamp.

This will avoid the unreliability of the Thread.sleep "real world" timing.

starvingmind
  • 290
  • 3
  • 12
  • Nice idea... Will think about this. Thanks! – Luis A. Florit Jan 03 '13 at 21:01
  • Unfortunately your idea didn't "quite" work. Since my `Thread.sleep` is small (6 to 8 ms gives a nice scrolling), sometimes the cycle takes much longer than this. Then implies several `Thread.sleep(0)` in a row, and the image jumps. However, I can see the sleep value being well compensated many times. There should be a way of getting "priority". Or a standard animation feature that takes into account the image out of the screen (this would be perfect). – Luis A. Florit Jan 04 '13 at 02:31
  • The problem is that `Thread.sleep()` is TOO inaccurate for my needs. My sleep needs around 5 ms, while `Thread.sleep()` may be as inaccurate as 50 or 100 ms, which coincides with the latency you reported here: http://stackoverflow.com/questions/7340302/how-to-reduce-thread-sleep-latency-in-android . I had the very same problem happen with `wait()`. So what I ended up doing is a simple `future = System.currentTimeMillis() + something; while (System.currentTimeMillis() < future) {//empty loop}`. This is of course more CPU intensive, but at least it now runs quite smoothly. – Luis A. Florit Dec 08 '13 at 22:45