【问题标题】:Once again: Why my ImageView is not refreshing?再一次:为什么我的 ImageView 不刷新?
【发布时间】:2012-12-08 13:24:01
【问题描述】:

我知道这个问题之前已经回答过好几次了,但是这个新手找不到有效的答案。

我尝试使用 TouchImageView(扩展 ImageView)实现全景查看器。首先我尝试使用 TranslateAnimation,但也失败了(我的问题是 no answers,所以我认为这对于 TouchImageView 是不可能的)。

然后我尝试了艰难的方式,使用 ImageView.matrix,移动一点,暂停,移动一点,暂停......好吧,至少这是我的想法。

问题是我只能在 4 或 5 秒后看到全景图的最终位置,什么也没有。我什至放了两个ImageView.invalidate();,没有任何成功。我尝试使用 Threads 和 AsyncTasks 也没有成功(问题是它们必须更新主 UI,否则我会得到一个异常;但在我看来使用 AsyncTask 来更新 UI 线程是错误的)。好吧,我尝试了我能想到的一切。

无论如何,下面是我的简单代码(AsyncTask 尝试,但我在里面做的一切)。观察onPreExecute 方法中的foto.invalidate(); 没有被遵守(为什么?)。有几行注释让你看看我还尝试了什么。

我的简单代码,其中foto 是 (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();
    }
}

谢谢!!

【问题讨论】:

    标签: android imageview invalidation translate-animation


    【解决方案1】:

    尝试使用 publishProgress 和 onProgressUpdate,而不是在 UI 线程上从 doinbackground 运行它?问题是 doinbackground 不在 uithread 上运行,而是在“后台”运行,因此对于所有 UI 更新,它必须将其传递给 onprogressupdate。

    将您的异步更改为:

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

    然后可以使用 Integer 参数调用 onProgressUpdate

     for (int i = 0; i < 100; i++) {
    
            try {
                Thread.sleep(25);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    
            publishProgress(0);
    
        }
    

    并覆盖这个:

    @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();
        }
    }
    

    【讨论】:

    • 好主意!好的,按照你的建议,它种类的作品。我的意思是“善良”,因为出于某种原因它不可靠。有时它会播放全景图,有时则不会。不知道为什么。我在看logcat,什么都没有!我会继续考虑这个,但你给了我一个好主意。谢谢!!
    • 那么您真正应该使用的是表面视图。观看此视频:youtube.com/watch?v=wUmId0rwsBQ&list=PL2F07DBCDCC01493A
    • 我的代码似乎发生的事情是我无法停止异步任务,即使使用.cancel(true) 也无法停止(我认为我的异步任务确实是一个无限循环,但它应该以.cancel(true) 停止,我认为. 即使在我退出我的应用程序后,它仍会继续运行(我可以看到它占用了 10% 的 CPU)。-我会查看 SurfaceView,但看起来过于复杂,因为我认为这是一个简单的 TranslateAnimation东西……
    • 您也许可以扩展更简单的视图类。另外要取消,您可以让您的 i 值 100 或其他值...
    • 即使是无限循环,我也设法停止了异步任务,如果任务被取消,我会调用break(从这里的代码中得到这个想法)。所以我更近了。无论如何,我会看看surfaceview。再次感谢!!
    猜你喜欢
    • 2015-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多