I try to create an animation with windowmanager in a service but when executing it throws me the exception:
FATAL EXCEPTION: main Process: com. {Name}, PID: 24454 java.lang.IllegalStateException: onMeasure () did not set the measured dimension by calling setMeasuredDimension () at android.view.View.measure (View.java:18000) at android.view.ViewRootImpl.performMeasure (ViewRootImpl.java:2438) at android.view.ViewRootImpl.measureHierarchy (ViewRootImpl.java:1418) at android.view.ViewRootImpl.performTraversals (ViewRootImpl.java:1642) at android.view.ViewRootImpl.doTraversal (ViewRootImpl.java:1296) at android.view.ViewRootImpl $ TraversalRunnable.run (ViewRootImpl.java:6742) at android.view.Choreographer $ CallbackRecord.run (Choreographer.java:826) at android.view.Choreographer.doCallbacks (Choreographer.java:629) at android.view.Choreographer.doFrame (Choreographer.java:597) at android.view.Choreographer $ FrameDisplayEventReceiver.run (Choreographer.java:812) at android.os.Handler.handleCallback (Handler.java:815) at android.os.Handler.dispatchMessage (Handler.java:104) at android.os.Looper.loop (Looper.java:194) at android.app.ActivityThread.main (ActivityThread.java:5931) at java.lang.reflect.Method.invoke (Native Method) at java.lang.reflect.Method.invoke (Method.java:372) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:987) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:782)
Here is the code
public class Servicio extends Service {
private LinearLayout ll;
private WindowManager.LayoutParams parameters;
private WindowManager.LayoutParams updateparameters;
private WindowManager wm;
private Bitmap bm;
private int ancho;
private int alto;
public void onCreate(){
super.onCreate();
Toast.makeText(Servicio.this, "onCreate", Toast.LENGTH_SHORT).show();
WindowManager wm1 = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
Display display = wm1.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
alto=size.x/4;
ancho=(size.x/4)*7/5;
try{
bm = BitmapFactory.decodeResource(getResources(), R.drawable.imagen);
}catch(Exception e){}
ll=new LinearLayout(this);
LinearLayout.LayoutParams llParameters=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
ll.setBackground(new BitmapDrawable(this.getResources(), bm));
ll.setLayoutParams(llParameters);
parameters=new WindowManager.LayoutParams(alto,ancho,0,0, WindowManager.LayoutParams.TYPE_PHONE,WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);
updateparameters=new WindowManager.LayoutParams(alto,ancho,0,0, WindowManager.LayoutParams.TYPE_PHONE,WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);
parameters.x=-size.x/2;
parameters.y=0;
wm=(WindowManager)getSystemService(WINDOW_SERVICE);
wm.addView(ll,parameters);
new Thread(new Runnable() {
public void run() {
Looper.prepare();
while(true){
updateparameters.x=a;
updateparameters.y=0;
a++;
update();/*Este metodo me lanza FatalExcepción. Si lo colocase fuera de este nuevo hilo, la excepción no sucede*/
}
}
}).start();
}
public int onStartCommand(Intent intent, int flags, int startId){
return START_STICKY ;
}
public void onDestroy(){
super.onDestroy();
Toast.makeText(Servicio.this, "onDestroy", Toast.LENGTH_SHORT).show();
wm.removeView(ll);
}
public IBinder onBind(Intent intent) {
return null;
}
public void update(){
wm.updateViewLayout(ll,updateparameters);
}
}