When I create an application, that uses View to create basic animation, the use of invalidate kind of loops it again and again to give a continuous animation. But, while using SurfaceView, the use of invalidate gives an error. Can someone help me out with it?
package com.example.newapplication;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class SurfaceAnimation extends SurfaceView implements Runnable{
SurfaceHolder ourHolder;
Thread ourThread = null;
boolean isRunning = true;
public SurfaceAnimation(Context context) {
super(context);
// TODO Auto-generated constructor stub
ourHolder = getHolder();
ourThread = new Thread(this);
ourThread.start();
}
@Override
public void run() {
// TODO Auto-generated method stub
while(isRunning){
if(!ourHolder.getSurface().isValid())
continue;
else{
Canvas canvas = ourHolder.lockCanvas();
canvas.drawColor(Color.CYAN);
ourHolder.unlockCanvasAndPost(canvas);
// invalidate();
}
}
}
}
The logcat for above is:
05-30 04:41:47.240: E/AndroidRuntime(1869): FATAL EXCEPTION: Thread-120
05-30 04:41:47.240: E/AndroidRuntime(1869): Process: com.example.newapplication, PID: 1869
05-30 04:41:47.240: E/AndroidRuntime(1869): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
05-30 04:41:47.240: E/AndroidRuntime(1869): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6094)
05-30 04:41:47.240: E/AndroidRuntime(1869): at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:857)
05-30 04:41:47.240: E/AndroidRuntime(1869): at android.view.ViewGroup.invalidateChild(ViewGroup.java:4320)
05-30 04:41:47.240: E/AndroidRuntime(1869): at android.view.View.invalidate(View.java:10935)
05-30 04:41:47.240: E/AndroidRuntime(1869): at android.view.View.invalidate(View.java:10890)
05-30 04:41:47.240: E/AndroidRuntime(1869): at com.example.newapplication.SurfaceAnimation.run(SurfaceAnimation.java:35)
05-30 04:41:47.240: E/AndroidRuntime(1869): at java.lang.Thread.run(Thread.java:841)
You have to use postInvalidate()
instead of invalidate()
if you want to update a view from non-ui thread. That is what your logcat says:
Only the original thread that created a view hierarchy can touch its views.