Search code examples
android-animationandroidandroid-kenburnsview

KenBurnsView how to set image resource?


I'm trying to use Faria's KenBurnsView library in my application.

KenBurnsView is some extension of ImageView, with a nice animating effect. You may have seen the effect in Foursquare app's images.

For the actual picture, the view uses the XML defined "android:src" property. But I want to set the image resource during run-time through Java code. I have tried setImageResource, setBackgroundResource, set".."drawabale and many other methods. But no matter what, my image setting command always makes my app crash at run-time.

I've been searching the web for a couple of hours for any trace of discussion on KenBurnsView but have had no luck whatsoever.

My question is:

How is it possible to set the image property of KenBurnsView on run-time?

EDIT :

Here's the stack trace:

06-16 01:26:43.062: E/AndroidRuntime(16712): FATAL EXCEPTION: main
06-16 01:26:43.062: E/AndroidRuntime(16712): Process: com.flaviofaria.kenburnsview.sample, PID: 16712
06-16 01:26:43.062: E/AndroidRuntime(16712): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.flaviofaria.kenburnsview.sample/com.flaviofaria.kenburnsview.sample.SingleImageActivity}: java.lang.NullPointerException
06-16 01:26:43.062: E/AndroidRuntime(16712):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
06-16 01:26:43.062: E/AndroidRuntime(16712):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
06-16 01:26:43.062: E/AndroidRuntime(16712):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-16 01:26:43.062: E/AndroidRuntime(16712):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-16 01:26:43.062: E/AndroidRuntime(16712):    at android.os.Handler.dispatchMessage(Handler.java:102)
06-16 01:26:43.062: E/AndroidRuntime(16712):    at android.os.Looper.loop(Looper.java:136)
06-16 01:26:43.062: E/AndroidRuntime(16712):    at android.app.ActivityThread.main(ActivityThread.java:5001)
06-16 01:26:43.062: E/AndroidRuntime(16712):    at java.lang.reflect.Method.invokeNative(Native Method)
06-16 01:26:43.062: E/AndroidRuntime(16712):    at java.lang.reflect.Method.invoke(Method.java:515)
06-16 01:26:43.062: E/AndroidRuntime(16712):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
06-16 01:26:43.062: E/AndroidRuntime(16712):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
06-16 01:26:43.062: E/AndroidRuntime(16712):    at dalvik.system.NativeStart.main(Native Method)
06-16 01:26:43.062: E/AndroidRuntime(16712): Caused by: java.lang.NullPointerException
06-16 01:26:43.062: E/AndroidRuntime(16712):    at com.flaviofaria.kenburnsview.MathUtils.getRectRatio(MathUtils.java:44)
06-16 01:26:43.062: E/AndroidRuntime(16712):    at com.flaviofaria.kenburnsview.RandomTransitionGenerator.generateRandomRect(RandomTransitionGenerator.java:93)
06-16 01:26:43.062: E/AndroidRuntime(16712):    at com.flaviofaria.kenburnsview.RandomTransitionGenerator.generateNextTransition(RandomTransitionGenerator.java:64)
06-16 01:26:43.062: E/AndroidRuntime(16712):    at com.flaviofaria.kenburnsview.KenBurnsView.startNewTransition(KenBurnsView.java:207)
06-16 01:26:43.062: E/AndroidRuntime(16712):    at com.flaviofaria.kenburnsview.KenBurnsView.handleImageChange(KenBurnsView.java:280)
06-16 01:26:43.062: E/AndroidRuntime(16712):    at com.flaviofaria.kenburnsview.KenBurnsView.setImageResource(KenBurnsView.java:126)
06-16 01:26:43.062: E/AndroidRuntime(16712):    at com.flaviofaria.kenburnsview.sample.SingleImageActivity.onCreate(SingleImageActivity.java:36)
06-16 01:26:43.062: E/AndroidRuntime(16712):    at android.app.Activity.performCreate(Activity.java:5231)
06-16 01:26:43.062: E/AndroidRuntime(16712):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-16 01:26:43.062: E/AndroidRuntime(16712):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
06-16 01:26:43.062: E/AndroidRuntime(16712):    ... 11 more

Solution

  • From the call stack, I'd say this is a bug in the library. It's not updating the mDrawableRect immediately after a new drawable is set.

    Try changing the implementation of setImageResource() to add call updateDrawableBounds(), i.e.

    @Override
    public void setImageResource(int resId) {
        super.setImageResource(resId);
        updateDrawableBounds(); // <- added line
        handleImageChange();
    }
    

    Looks like this should fix the problem (but I haven't tested it).

    EDIT Actually, all the setImage<X>() methods seem to have this issue. So the same fix should be used for setImageDrawable(), setImageBitmap(), and setImageURI().

    NEWEST EDIT This is now fixed, according to @FlávioFaria himself. Just update to version 1.0.3.