1

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:)

MehDi
  • 504
  • 6
  • 20

1 Answers1

2

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)
   }
}
Samvid Mistry
  • 783
  • 1
  • 8
  • 14
  • Thanks for your answer, and yes I found my problem yesterday.As you said, I inflate and get another reference of views,not the true one that is shown in the screen. – MehDi Dec 17 '16 at 12:11
  • But how can I get same reference by inflate view? We inflate a viw to do something on it in code! Is there any way to getting same reference by inflating, instead of init it in Activity and pass references by a setter ? – MehDi Dec 17 '16 at 12:14
  • Well, as I said, SurfaceView is not meant to be working with the standard view hierarchy, it has its own drawing system. So as far as I know, inflating the views anywhere else except the Activity would not work. You can inflate the view in SurfaceView but then you cannot set it on activity layout, because as far as I know, a SurfaceView has its own thread for drawing rather than main thread, and only the thread which created the view can change the properties of view. It's a little bit complex, but hope you are getting it. – Samvid Mistry Dec 17 '16 at 12:19
  • Yes I got it:) if you know a useful reference please give me,or I'll search about it later. Thank you very much my friend – MehDi Dec 17 '16 at 12:28