gpt4 book ai didi

java - NineOldAndroids 动画不适用于 API > 10

转载 作者:行者123 更新时间:2023-11-30 03:18:30 26 4
gpt4 key购买 nike

我正在使用 NineOldAndroid 库来执行动画。动画适用于 API<=10。但对于 API>10,应用程序强制关闭。这是我的代码:

import static com.nineoldandroids.view.ViewPropertyAnimator.animate;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.view.MenuItem;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.Animator.AnimatorListener;
import com.nineoldandroids.animation.ObjectAnimator;

public class ActivityActualMain extends SherlockActivity {
LinearLayout container1, container2;
RelativeLayout viewTree;
ImageView image, image1, image2;
TextView tv, tv1, tv2, tv3, tv4;
ObjectAnimator anim;



@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final int duration = 2000;
setContentView(R.layout.activity_actual_main);

ActionBar bar = getSupportActionBar();
bar.setDisplayHomeAsUpEnabled(true);
bar.setBackgroundDrawable(getResources().getDrawable(
R.drawable.red_actionbar));
viewTree = (RelativeLayout) findViewById(R.id.viewTree);
container1 = (LinearLayout) findViewById(R.id.linearLayout1);
container2 = (LinearLayout) findViewById(R.id.linearLayout2);
image = (ImageView) findViewById(R.id.imageView1);
image1 = (ImageView) findViewById(R.id.imageView2);
image2 = (ImageView) findViewById(R.id.imageView3);
tv = (TextView) findViewById(R.id.text_tech_des);

viewTree.getViewTreeObserver().addOnGlobalLayoutListener(//to check if the layout has been placed in activity
new OnGlobalLayoutListener() {
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT < 16) {
viewTree.getViewTreeObserver().removeGlobalOnLayoutListener(this);}
else{
viewTree.getViewTreeObserver()
.removeOnGlobalLayoutListener(this);
}

anim = ObjectAnimator.ofFloat(image, "y", 0f,
image.getTop());

anim.addListener(new AnimatorListener() {

@Override
public void onAnimationStart(Animator arg0) {
// TODO Auto-generated method stub
ObjectAnimator.ofFloat(tv, "alpha", 1, 0, 1)
.setDuration(duration).start();//line no 82
ObjectAnimator.ofFloat(container1, "x", 0f,
container1.getLeft()).setDuration(1000).start();
ObjectAnimator.ofFloat(container2, "x", 0f,
container2.getLeft()).setDuration(1000).start();
}

@Override
public void onAnimationRepeat(Animator arg0) {
// TODO Auto-generated method stub

}

@Override
public void onAnimationEnd(Animator arg0) {
// TODO Auto-generated method stub
}

@Override
public void onAnimationCancel(Animator arg0) {
// TODO Auto-generated method stub

}
});
anim.setDuration(duration).start();//line no 106
}
});

}
}

这是我的堆栈跟踪:

    10-26 19:23:15.203: E/AndroidRuntime(21541): FATAL EXCEPTION: main
10-26 19:23:15.203: E/AndroidRuntime(21541): java.lang.NullPointerException
10-26 19:23:15.203: E/AndroidRuntime(21541): at com.nineoldandroids.animation.PropertyValuesHolder.setupSetterAndGetter(PropertyValuesHolder.java:523)
10-26 19:23:15.203: E/AndroidRuntime(21541): at com.nineoldandroids.animation.ObjectAnimator.initAnimation(ObjectAnimator.java:410)
10-26 19:23:15.203: E/AndroidRuntime(21541): at com.nineoldandroids.animation.ValueAnimator.setCurrentPlayTime(ValueAnimator.java:538)
10-26 19:23:15.203: E/AndroidRuntime(21541): at com.nineoldandroids.animation.ValueAnimator.start(ValueAnimator.java:928)
10-26 19:23:15.203: E/AndroidRuntime(21541): at com.nineoldandroids.animation.ValueAnimator.start(ValueAnimator.java:951)
10-26 19:23:15.203: E/AndroidRuntime(21541): at com.nineoldandroids.animation.ObjectAnimator.start(ObjectAnimator.java:385)
10-26 19:23:15.203: E/AndroidRuntime(21541): at com.vishalaksh.technex.ActivityActualMain$2$1.onAnimationStart(ActivityActualMain.java:82)
10-26 19:23:15.203: E/AndroidRuntime(21541): at com.nineoldandroids.animation.ValueAnimator.start(ValueAnimator.java:937)
10-26 19:23:15.203: E/AndroidRuntime(21541): at com.nineoldandroids.animation.ValueAnimator.start(ValueAnimator.java:951)
10-26 19:23:15.203: E/AndroidRuntime(21541): at com.nineoldandroids.animation.ObjectAnimator.start(ObjectAnimator.java:385)
10-26 19:23:15.203: E/AndroidRuntime(21541): at com.vishalaksh.technex.ActivityActualMain$2.onGlobalLayout(ActivityActualMain.java:106)
10-26 19:23:15.203: E/AndroidRuntime(21541): at android.view.ViewTreeObserver.dispatchOnGlobalLayout(ViewTreeObserver.java:808)
10-26 19:23:15.203: E/AndroidRuntime(21541): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1768)
10-26 19:23:15.203: E/AndroidRuntime(21541): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
10-26 19:23:15.203: E/AndroidRuntime(21541): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
10-26 19:23:15.203: E/AndroidRuntime(21541): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
10-26 19:23:15.203: E/AndroidRuntime(21541): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
10-26 19:23:15.203: E/AndroidRuntime(21541): at android.view.Choreographer.doFrame(Choreographer.java:532)
10-26 19:23:15.203: E/AndroidRuntime(21541): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
10-26 19:23:15.203: E/AndroidRuntime(21541): at android.os.Handler.handleCallback(Handler.java:730)
10-26 19:23:15.203: E/AndroidRuntime(21541): at android.os.Handler.dispatchMessage(Handler.java:92)
10-26 19:23:15.203: E/AndroidRuntime(21541): at android.os.Looper.loop(Looper.java:137)
10-26 19:23:15.203: E/AndroidRuntime(21541): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-26 19:23:15.203: E/AndroidRuntime(21541): at java.lang.reflect.Method.invokeNative(Native Method)
10-26 19:23:15.203: E/AndroidRuntime(21541): at java.lang.reflect.Method.invoke(Method.java:525)
10-26 19:23:15.203: E/AndroidRuntime(21541): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-26 19:23:15.203: E/AndroidRuntime(21541): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-26 19:23:15.203: E/AndroidRuntime(21541): at dalvik.system.NativeStart.main(Native Method)

最佳答案

我在 API < 10 的设备上遇到这个库的问题,它给出了相同的堆栈跟踪。鉴于我使用的是 NineOldAndroids 的源代码,我决定调查库中的问题。

经过一些测试后,我注意到发生此错误是因为库试图调用 View 中不存在的一些方法(因为它是旧的 API 级别)。再搜索一下,我发现了 AnimatorProxy 类,它有一个名为“wrap”的静态方法。此方法用于在旧版 Android 中封装 View 对象,模拟某些动画方法的存在,例如 setScaleX/Y、setTransalationX/Y。

为了解决这个问题,我不得不打开 ObjectAnimator 类并搜索此行的所有匹配项(在我的库代码中我发现了 4 个匹配项):

mTarget = target;

在该类中我创建了以下方法:

private void setTarget(Object obj) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB && obj instanceof View) {
mTarget = AnimatorProxy.wrap((View) obj);
} else {
mTarget = obj;
}
}

并将上面的行替换为:

setTarget(target);

不知道它是否可以解决您的问题,因为您说它发生在 API 10+(与我的相反)上,但这是一个很好的起点。

关于java - NineOldAndroids 动画不适用于 API > 10,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19607703/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com