androidandroid-animationsurfaceviewsurfaceholder

Animation not working in SurfaceView


I have this very simple code cycle, I don't do any complicate thing! The TextAnimation() method get call,but, the animation inside it doesn't start(I can't see any Log)

this is my cod:

my Activity Main:

public class Main extends AppCompatActivity {

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

my 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>

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

}

Can anyone help me please?what's wrong with this code? Thanks:)


Solution

  • Actually, you're doing it all wrong. SurfaceView is not supposed to work with the normal View system, at least not below Nougat. Aside from that, you are not actually getting a reference to the already inflated view hierarchy, you are inflating a new view hierarchy which is not shown anywhere and you are trying to animate it, it may be animating but you cannot see it because it is not shown on screen. In order for this animation to work, you need to pass the reference of the TextView from the Activity to the SurfaceView, from the constructor or with setter methods, and then animate that TextView reference. Or better than that, have a callback from surfaceCreated to the Activity and play the animation from inside the Activity. The code may look something like this

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

    Now inside 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)
       }
    }