2016-12-16 151 views
0

我有這個非常簡單的代碼循環, 我沒有做任何複雜的事情! 的TextAnimation()方法得到調用,但是,它裏面的動畫沒有開始(我看不到任何日誌)動畫無法在SurfaceView中工作

這是我的鱈魚:

我的主要活動:

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

} 

任何人都可以幫助我嗎?這段代碼有什麼問題? 謝謝:)

回答

0

其實,你所做的一切都是錯誤的。 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) 
    } 
} 
+0

謝謝您的回答,是的,我發現我的問題yesterday.As你說,我膨脹並獲得意見另一個參考,不屏幕上顯示的是真實的。 – MehDi

+0

@MehDi所以如果我是對的,那麼將答案標記爲接受,這樣對其他人可能會有所幫助。 –

+0

但是,如何通過充氣視圖獲得相同的參考?我們誇大viw在代碼上做些事情!有什麼辦法通過膨脹來獲得相同的引用,而不是在Activity中初始化它並通過setter傳遞引用? – MehDi