Luis A. Florit
Luis A. Florit

Reputation: 2599

Once again: Why my ImageView is not refreshing?

I know this question has been answered several times before, but this newbie could not find an answer that worked.

I tried to implement a panorama viewer using a TouchImageView (extends ImageView). First I tried with TranslateAnimation, but also failed (my question got no answers, so I presume this is not possible with TouchImageView).

Then I tried the hard way, using ImageView.matrix, moving a little bit, pausing, moving a little bit, pausing... Well, at least that was my idea.

The problem is that I only see the panorama in its final position after 4 or 5 seconds showing nothing. I even put two ImageView.invalidate();, without any success. I tried using Threads and AsyncTasks without success either (problem with those is that they has to update the main UI, otherwise I get an Exception; but it looks wrong to me to use AsyncTask to update the UI thread). Well, I tried everything I can think of.

Anyway, below is my simple code (the AsyncTask try, but with everything I do inside). Observe the foto.invalidate(); in the onPreExecute method that is not obeyed (why?). There are some lines commented for you to see what else I tried.

My simple code, where foto is the (Touch)ImageView:

class animatepanorama extends AsyncTask<Void, Void, Void> {
    float scale = foto.cropScale * foto.baseScale;
    Matrix m = foto.matrix;
    protected void onPreExecute(Void arg0) {
        m.setScale(scale, scale);
        foto.Scale = foto.cropScale;
        foto.setImageMatrix(m);
        foto.invalidate();
    }
    @Override
    protected Void doInBackground(Void... arg0) {
        // new Thread(new Runnable() { public void run() {
        //synchronized(foto) {
        runOnUiThread(new Runnable() {
            public void run() {
                for (int i = 0; i < 100; i++) {

                    try {
                        Thread.sleep(25);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    m.setScale(scale, scale);
                    m.postTranslate(-i * scale * 8, 0);
                    foto.setImageMatrix(m);
                    foto.invalidate();
                }
            }
        });
        //}
        // }}).start();
        return null;
    }
    protected void onPostExecute(Void arg0) {
        foto.invalidate();
    }
}

Thanks!!

Upvotes: 1

Views: 2276

Answers (1)

AndroidPenguin
AndroidPenguin

Reputation: 3453

Try instead of running it from doinbackground on the UI thread, use publishProgress and onProgressUpdate? The problem is doinbackground does not run on the uithread but "in the background" so for all UI updates it has to pass this to onprogressupdate.

Change your Async to this:

class animatepanorama extends AsyncTask<Void, Integer, Void> {

The Integer parameter can then be used to call onProgressUpdate

 for (int i = 0; i < 100; i++) {

        try {
            Thread.sleep(25);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        publishProgress(0);

    }

And override this:

@Override
protected void onProgressUpdate(Integer... values) {
    if (values[0] == 0) {
        m.setScale(scale, scale);
        m.postTranslate(-i * scale * 8, 0);
        foto.setImageMatrix(m);
        foto.invalidate();
    }
}

Upvotes: 1

Related Questions