2016-12-16 11 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以下では動作しません。それ以外にも、すでに膨らんだビュー階層への参照は実際には得られていません。どこにも表示されていない新しいビュー階層を膨らませています。アニメーション化しようとしています。アニメーションは可能ですが、画面には表示されません。このアニメーションを機能させるには、ActivityからSurfaceView、コンストラクタ、またはsetterメソッドからTextViewの参照を渡し、そのTextView参照をアニメーション化する必要があります。またはそれよりも優れているのは、surfaceCreatedからActivityへのコールバックを持ち、Activity内からアニメーションを再生することです。コードはGameView内部の今、この

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

のようにあなたの答えのための

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を膨らませます!アクティビティでinitを起動するのではなく、セッターで参照を渡す代わりに、同じリファレンスを取得する方法はありますか? – MehDi

関連する問題