android 如何使用ObjectAnimator在圆形路径中使用path.addCircle移动视图对象?

0s0u357o  于 2023-03-11  发布在  Android
关注(0)|答案(2)|浏览(103)

我尝试使用android android.animation.ObjectAnimator 在圆形路径中移动视图对象
我尝试使用此方法,但收到错误消息路径必须从(0,0)开始,并在(1,1)结束
这是特定视图对象的OnClick()方法

public void onPathAnimation(View view){
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
        Path path = new Path();
        path.moveTo(view.getX(), view.getY()+100);
        path.addCircle(view.getX(), view.getY(), 100, Path.Direction.CW);
        PathInterpolator pathInterpolator = new PathInterpolator(path);

        ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationX", 100f);
        animator.setInterpolator(pathInterpolator);
        //  animator.setDuration(2000);
        animator.start();
    }
}

这是我得到的例外

java.lang.IllegalStateException: Could not execute method for android:onClick
                                                                         at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                                                                         at android.view.View.performClick(View.java:5265)
                                                                         at android.view.View$PerformClick.run(View.java:21534)
                                                                         at android.os.Handler.handleCallback(Handler.java:815)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:104)
                                                                         at android.os.Looper.loop(Looper.java:207)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5737)
                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
                                                                      Caused by: java.lang.reflect.InvocationTargetException
                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                         at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                                                                         at android.view.View.performClick(View.java:5265) 
                                                                         at android.view.View$PerformClick.run(View.java:21534) 
                                                                         at android.os.Handler.handleCallback(Handler.java:815) 
                                                                         at android.os.Handler.dispatchMessage(Handler.java:104) 
                                                                         at android.os.Looper.loop(Looper.java:207) 
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5737) 
                                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679) 
                                                                      Caused by: java.lang.IllegalArgumentException: The Path must start at (0,0) and end at (1,1)
                                                                         at android.view.animation.PathInterpolator.initPath(PathInterpolator.java:164)
                                                                         at android.view.animation.PathInterpolator.<init>(PathInterpolator.java:61)
                                                                         at com.kalana.materialtest.MainActivity.onPathAnimation(MainActivity.java:179)
                                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                                         at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                                                                         at android.view.View.performClick(View.java:5265) 
                                                                         at android.view.View$PerformClick.run(View.java:21534) 
                                                                         at android.os.Handler.handleCallback(Handler.java:815) 
                                                                         at android.os.Handler.dispatchMessage(Handler.java:104) 
                                                                         at android.os.Looper.loop(Looper.java:207) 
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5737) 
                                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)

感谢你的建议。

ffscu2ro

ffscu2ro1#

试试这个:

Path path = new Path();
 path.arcTo(50f, 800f, 1000f, 1000f, -180f, 145f, true);
 ObjectAnimator animation = ObjectAnimator.ofFloat(view, View.X, View.Y, path);
 animation.setDuration(2000);  //animation duration
 animation.start();
8ulbf1ek

8ulbf1ek2#

我使用的是addOval方法,不需要定义任何Angular 。

val path: Path = Path()
val ovalRect = RectF(
    left,
    top,
    right,
    bottom)

path.addOval(
    ovalRect,
    Path.Direction.CCW)

oa = ObjectAnimator.ofFloat(
    view,
    View.X,
    View.Y,
    path)
oa?.duration = MOVING_CAMERA_PREVIEW_TIME
oa?.repeatMode = ObjectAnimator.RESTART
oa?.repeatCount = ObjectAnimator.INFINITE
oa?.start()

相关问题