动画在 SurfaceView 中不起作用

Animation not working in SurfaceView

我有这个非常简单的代码循环, 我不做任何复杂的事情! TextAnimation() 方法被调用,但是里面的动画没有开始(我看不到任何日志)

这是我的鳕鱼:

我的 Activity 主要的:

public class Main extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}
}

我的主要布局:

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lineralayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff">

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <gamedevelopment.mahdi.nazari.killthemall_training.GameView
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</FrameLayout>

<LinearLayout
    android:id="@+id/Li_ly"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff"
    android:orientation="vertical">

    <TextView
        android:id="@+id/level_text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_marginTop="0dp"
        android:background="#fff"
        android:gravity="center"
        android:text="fdfdsf"
        android:textColor="#000"
        android:textSize="60dp" />
</LinearLayout>

我的游戏视图:

public class GameView extends SurfaceView {

TextView level_text;
LinearLayout ly;

public GameView(Context context, AttributeSet attrs) {
    super(context, attrs);
    start();
}

public GameView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    start();
}

public void start() {
    surfaceHolder = getHolder();
    surfaceHolder.addCallback(new SurfaceHolder.Callback() {

        @Override
        public void surfaceCreated(SurfaceHolder holder) {

            LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View v = inflater.inflate(R.layout.main, null);
            level_text = (TextView) v.findViewById(R.id.level_text);
            ly = (LinearLayout) v.findViewById(R.id.Li_ly);

            TextAnimation();

        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            firstCreacation = false;
            gameLoopThread.setRunning(false);
        }
    });
}


public void TextAnimation() {

    Animation animation = AnimationUtils.loadAnimation(context, R.text_anim);
    animation.setAnimationListener(new Animation.AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
            Log.e("start","");
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            ShowLevelText2();
            Log.e("End","");
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });

    level_text.startAnimation(animation);
}

}

谁能帮帮我?这段代码有什么问题吗? 谢谢:)

其实,你做错了。 SurfaceView 不应该与普通的 View 系统一起工作,至少不低于 Nougat。除此之外,您实际上并没有获得对已经膨胀的视图层次结构的引用,您正在膨胀一个未在任何地方显示的新视图层次结构并且您正在尝试为其设置动画,它可能正在设置动画但您看不到它,因为它是没有显示在屏幕上。为了使此动画起作用,您需要将 TextView 的引用从 Activity 传递到 SurfaceView,从构造函数或使用 setter 方法,然后为该 TextView 引用设置动画。或者更好的是,从 surfaceCreated 回调到 Activity 并从 Activity 内部播放动画。代码可能看起来像这样

public class Main extends AppCompatActivity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      TextView textView = findViewById(R.id.level_text);
      GameView gameView = findViewById(R.id.game_view); //Give an id to your GameView, I'm just using a random id here

      gameView.setTextView(textView);
   }
}

现在进入 GameView

public class GameView extends SurfaceView {
   TextView textView;

   ... // All your code

   public void setTextView(TextView t){
       textView = t;
   }

   public void TextAnimation(){
       ... // All your code

       textView.startAnimation(animation)
   }
}