简体   繁体   English

KenBurnsView如何设置图像资源?

[英]KenBurnsView how to set image resource?

I'm trying to use Faria's KenBurnsView library in my application. 我正在尝试在应用程序中使用Faria的KenBurnsView库。

KenBurnsView is some extension of ImageView, with a nice animating effect. KenBurnsView是ImageView的扩展,具有良好的动画效果。 You may have seen the effect in Foursquare app's images. 您可能已经在Foursquare应用程序的图像中看到了效果。

For the actual picture, the view uses the XML defined "android:src" property. 对于实际图片,该视图使用XML定义的“ android:src”属性。 But I want to set the image resource during run-time through Java code. 但是我想在运行时通过Java代码设置图像资源。 I have tried setImageResource, setBackgroundResource, set".."drawabale and many other methods. 我尝试了setImageResource,setBackgroundResource,set“ ..” drawabale和许多其他方法。 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. 我已经在网上搜索了两个小时,无法在KenBurnsView上进行任何讨论,但是运气不佳。

My question is: 我的问题是:

How is it possible to set the image property of KenBurnsView on run-time ? 如何在运行时设置KenBurnsView的image属性

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

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. 设置新的drawable之后, mDrawableRect立即更新mDrawableRect

Try changing the implementation of setImageResource() to add call updateDrawableBounds() , ie 尝试更改setImageResource()的实现以添加调用updateDrawableBounds() ,即

@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. 编辑实际上,所有setImage<X>()方法似乎都存在此问题。 So the same fix should be used for setImageDrawable() , setImageBitmap() , and setImageURI() . 因此,应该对setImageDrawable()setImageBitmap()setImageURI()使用相同的修复程序。

NEWEST EDIT This is now fixed, according to @FlávioFaria himself. 最新编辑 ,根据@FlávioFaria本人现已修复。 Just update to version 1.0.3. 只需更新到版本1.0.3。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM