Search code examples
androidanimationimageviewandroid-appwidgetremoteview

How To change the imageview of an appwidget (remoteview) multiple times with a short break between?


I am new to Android Development and im stuck with a Problem. I hope you guys can help me ;) Im am working on an Appwidget, a remoteview with a linearlayout that is containing multiple imageviews. I worked myself through some tutorials and examples and i was able to build up a first app , that detects a clicked imageview to open a specific installed app, or to change the imageresource of some Imageviews.

Now my Problem is that i want to animate the images when the button is clicked. I made a drawableAnimation, but then i read that remoteviews dont support these.

So my Idea was to change the pictures manually with a little break between For Example:

change imageview

0.1 seconds wait

change imageview

0.1 seconds wait

change imageview

0.1 seconds wait

change imageview

So now i read some about Sleep(), handlers, and adapterviewflipper (none of it i was able to implement) and i really dont know which way to go.

Here is the code of my Appwidgetprovider

public class MyWidgetProvider extends AppWidgetProvider {

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {

    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_demo);

    // Button To Change Imageview
    remoteViews.setOnClickPendingIntent(R.id.B1, buildButtonPendingIntent(context));
    //Buttons to open some installed apps
    remoteViews.setOnClickPendingIntent(R.id.T1, getPendingIntent(context, 1));
    remoteViews.setOnClickPendingIntent(R.id.T2, getPendingIntent(context, 2));

    pushWidgetUpdate(context, remoteViews);
}




public static PendingIntent buildButtonPendingIntent(Context context) {
    Intent intent = new Intent();
    intent.setAction("com.appwidgettest.intent.action.UPDATEUI");
    return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}


public static PendingIntent getPendingIntent(Context context, int btnId) {

// starts a htc radio app as standart and if button 2 is clicked it starts
// chrome browser just did this for testing the packagemanager
    PackageManager pm = context.getPackageManager();
    String gg = "com.htc.fm";
    if (btnId==2){gg = "com.android.chrome";
    }

    Intent intentt= pm.getLaunchIntentForPackage(gg);
    PendingIntent pi = PendingIntent.getActivity(context, 0, intentt, 0);

    return pi;
}



public static void pushWidgetUpdate(Context context, RemoteViews remoteViews) {
    ComponentName myWidget = new ComponentName(context, MyWidgetProvider.class);
    AppWidgetManager manager = AppWidgetManager.getInstance(context);
    manager.updateAppWidget(myWidget, remoteViews);     
}

}

and the Broadcastreciever which is working so far.

public class MyWidgetIntentReceiver extends BroadcastReceiver {




@Override
public void onReceive(Context context, Intent intent) {
    if(intent.getAction().equals("com.appwidgettest.intent.action.UPDATEUI")){
        updateWidgetPictureAndButtonListener(context);
    }
}

private void updateWidgetPictureAndButtonListener(Context context) {
    final RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_demo);


    // Here i Change The Imageviews!
    remoteViews.setImageViewResource(R.id.M3, R.drawable.image1);
    //here i need a short time sleep of 0.2 seconds for example
    remoteViews.setImageViewResource(R.id.M2, R.drawable.image2);
    //here too
    remoteViews.setImageViewResource(R.id.M1, R.drawable.image3);





    // Here to set the Button Listeners
    remoteViews.setOnClickPendingIntent(R.id.B1, MyWidgetProvider.buildButtonPendingIntent(context));
    remoteViews.setOnClickPendingIntent(R.id.T2, MyWidgetProvider.getPendingIntent(context,2));
    remoteViews.setOnClickPendingIntent(R.id.T1, MyWidgetProvider.getPendingIntent(context,1));


    MyWidgetProvider.pushWidgetUpdate(context.getApplicationContext(), remoteViews);
}

}

I am really sorry for my bad english^^and i hope you could understand most of it :P

And if you have a better idea for the Title Please tell me

Help me Please!!

Regards T.R.Salvatore


Solution

  • If you are looking for sprite animation when a button is clicked, see this example https://code.google.com/p/mario-coin-block/. It causes a series of images to be displayed like a sprite animation.

    If you simply want something like a slideshow, use the AdapterViewFlipper. I've created a similar app widget: https://github.com/mridang/appwidget-ilmaana