动画在SurfaceView

时间:2016-12-16 13:08:51

标签: android android-animation surfaceview surfaceholder

我有这个非常简单的代码周期, 我不做任何复杂的事情! TextAnimation()方法得到调用,但是,里面的动画没有启动(我看不到任何Log)

这是我的鳕鱼:

我的活动主页:

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>

我的GameView:

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);
}

}

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

1 个答案:

答案 0 :(得分:1)

实际上,你做错了。 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)
   }
}