【问题标题】:Animation not working in SurfaceView动画在 SurfaceView 中不起作用
【发布时间】:2016-12-16 13:08:51
【问题描述】:

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

这是我的鳕鱼:

我的主要活动:

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

}

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

【问题讨论】:

    标签: android android-animation surfaceview surfaceholder


    【解决方案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)
       }
    }
    

    【讨论】:

    • 感谢您的回答,是的,我昨天发现了我的问题。正如您所说,我膨胀并获得了另一个视图参考,而不是屏幕上显示的真实视图。
    • 但是如何通过膨胀视图获得相同的参考?我们膨胀一个视图以在代码中对它做一些事情!有没有办法通过膨胀来获得相同的引用,而不是在 Activity 中初始化它并通过 setter 传递引用?
    • 好吧,正如我所说,SurfaceView 并不是要使用标准视图层次结构,它有自己的绘图系统。据我所知,在除 Activity 之外的任何其他地方膨胀视图是行不通的。您可以在 SurfaceView 中膨胀视图,但不能在活动布局上设置它,因为据我所知,SurfaceView 有自己的绘图线程而不是主线程,只有创建视图的线程才能更改的属性看法。这有点复杂,但希望你能明白。
    • 是的,我知道了:) 如果您知道有用的参考资料,请给我,否则我稍后再搜索。非常感谢我的朋友?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-03
    • 1970-01-01
    • 2013-02-18
    • 2012-08-06
    • 2015-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多