2017-11-10 10 views
-2

私はアニメーションを行うビューを持っているので、それを削除したり、少なくともバックに送って再利用することができます。リスナーを付けても問題ありません。アニメーション後のビューを削除する

私は約2週間これで立ち往生しているので、どんなヘルプも本当に感謝しています。

私は、カードをスクリーンから飛ばして消してから削除しようとしています。

package com.example.trevorwood.biggles.study; 

import android.content.Context; 
import android.content.Intent; 
import android.graphics.Color; 
import android.graphics.PorterDuff; 
import android.graphics.drawable.Drawable; 
import android.os.Bundle; 
import android.support.v4.view.ViewCompat; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.animation.AlphaAnimation; 
import android.view.animation.Animation; 
import android.view.animation.AnimationSet; 
import android.view.animation.RotateAnimation; 
import android.view.animation.TranslateAnimation; 
import android.widget.FrameLayout; 
import android.widget.LinearLayout; 

import com.example.trevorwood.biggles.R; 


public class StudyActivity extends AppCompatActivity { 
    LinearLayout mFlipCardLinearLayout; 
    LinearLayout mCardFlippedButtons; 
    FrameLayout mCardFrame; 
    View mCurrentAnimCardContainer; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_study); 
     Intent intent = getIntent(); 
     // String value = intent.getStringExtra("key"); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.study_toolbar); 
     toolbar.setTitleTextColor(Color.WHITE);//0xAARRGGBB 
     setSupportActionBar(toolbar); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

     mFlipCardLinearLayout = (LinearLayout) findViewById(R.id.flip_card_linear_layout); 
     mCardFlippedButtons = (LinearLayout) findViewById(R.id.card_flipped_buttons); 
     mCardFrame = (FrameLayout) findViewById(R.id.card_frame); 

     final Drawable upArrow = getResources().getDrawable(R.drawable.ic_back_arrow); 
     upArrow.setColorFilter(getResources().getColor(R.color.colorWhite), PorterDuff.Mode.SRC_ATOP); 
     getSupportActionBar().setHomeAsUpIndicator(upArrow); 

     makeNewCard(); 

    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 

     switch (item.getItemId()) { 
      // Respond to the action bar's Up/HomeFragment button 
      case android.R.id.home: 
       onBackPressed(); 
       return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    @Override 
    public void onBackPressed() { 
     super.onBackPressed(); 
    } 

    public void onCardClick(View view) { 
     flipCard(); 
    } 

    public void onCardFlippedButtonsClick(View view) { 
     Integer numberPressed; 
     switch (view.getId()){ 
      case R.id.color_button_1:numberPressed = 1;break; 
      case R.id.color_button_2:numberPressed = 2;break; 
      case R.id.color_button_3:numberPressed = 3;break; 
      case R.id.color_button_4:numberPressed = 4;break; 
      case R.id.color_button_5:numberPressed = 5;break; 
      default:numberPressed = 0; 
     } 

     saveCardStats(numberPressed); 
     flingCardAway(); 
     resetForNewCard(); 
     makeNewCard(); 
    } 

    private void flipCard() { 
     FrameLayout cardFrame = (FrameLayout) findViewById(R.id.card_frame); 
     Integer childCount = cardFrame.getChildCount(); 
     Log.d("Simple","childCount: "+childCount); 

     View cardContainer = findViewById(R.id.card_container); 
     View cardFace = findViewById(R.id.card_front); 
     View cardBack = findViewById(R.id.card_back); 

     FlipAnimation flipAnimation = new FlipAnimation(cardFace, cardBack); 

     if (cardFace.getVisibility() == View.GONE) { 
      mFlipCardLinearLayout.setVisibility(View.VISIBLE); 
      mCardFlippedButtons.setVisibility(View.GONE); 
      flipAnimation.reverse(); 
     }else{ 
      mFlipCardLinearLayout.setVisibility(View.GONE); 
      mCardFlippedButtons.setVisibility(View.VISIBLE); 
     } 
     cardContainer.startAnimation(flipAnimation); 
    } 

    private void saveCardStats(Integer numberPressed){ 

    } 

    private void flingCardAway(){ 
     mCurrentAnimCardContainer = findViewById(R.id.card_container); 
     ViewCompat.setTranslationZ(mCurrentAnimCardContainer, 1.0f); 

     AnimationSet anim = new AnimationSet(true); 

     RotateAnimation rotate1 = new RotateAnimation(0,-45, Animation.RELATIVE_TO_SELF,0.5f , Animation.RELATIVE_TO_SELF,0.5f); 
     rotate1.setStartOffset(100); 
     rotate1.setDuration(500); 
     anim.addAnimation(rotate1); 

     TranslateAnimation trans1 = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, -0.1f, Animation.RELATIVE_TO_PARENT, -0.1f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f); 
     trans1.setDuration(600); 
     anim.addAnimation(trans1); 

     AlphaAnimation opacity1 = new AlphaAnimation(1.0f, 0.0f); 
     opacity1.setDuration(400); 
     opacity1.setStartOffset(200); 
     anim.addAnimation(opacity1); 

     mCurrentAnimCardContainer.setAnimation(anim); 
     mCurrentAnimCardContainer.setVisibility(View.VISIBLE); 

     final FrameLayout cardFrame = (FrameLayout) findViewById(R.id.card_frame); 

     anim.setAnimationListener(new Animation.AnimationListener(){ 
      @Override 
      public void onAnimationStart(Animation arg0) { 
      } 
      @Override 
      public void onAnimationRepeat(Animation arg0) { 
      } 
      @Override 
      public void onAnimationEnd(Animation arg0) { 
//The problem area, also this is not always called 
//    ((ViewGroup)mCurrentAnimCardContainer.getParent()).removeView(mCurrentAnimCardContainer); 
//     cardFrame.removeAllViews(); 
      } 
     }); 
    } 

    private void resetForNewCard(){ 
     mFlipCardLinearLayout.setVisibility(View.VISIBLE); 
     mCardFlippedButtons.setVisibility(View.GONE); 
    } 

    private void makeNewCard(){ 
     Integer childCount = mCardFrame.getChildCount(); 
     Log.d("Simple","childCount: "+childCount); 
     LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     inflater.inflate(R.layout.study_card, mCardFrame); 
    } 

} 

あなたが川の流れに対して常に泳いでいるように、それは私だけか、Androidアプリを感じさせていますか?

編集:本当の改善を見たい場合は、なぜこれが悪い質問ではないかと投票してください。

+0

'setAnimation'変更してみてください - >' startAnimation'を残念ながら – pskink

+0

@pskinkではなく何が起こるか見て、setAnimationとstartAnimationは同じ結果を持っているようです。助けてくれてありがとう –

答えて

0

私はonAnimationEndを取り出し、removeAllViewメソッドをその外に置いて動作させました。正直なところ私はこれがどういう仕組みか分かりません。あなたがビューを削除すると、アニメーションの作業ができなくなると思います。

private void flingCardAway(){ 
    View currentAnimCardContainer = findViewById(R.id.card_container); 
    ViewCompat.setTranslationZ(currentAnimCardContainer, 1.0f); 

    AnimationSet anim = new AnimationSet(true); 

    RotateAnimation rotate1 = new RotateAnimation(0,-45, Animation.RELATIVE_TO_SELF,0.5f , Animation.RELATIVE_TO_SELF, 0.5f); 
    rotate1.setStartOffset(1); 
    rotate1.setDuration(400); 
    anim.addAnimation(rotate1); 

    TranslateAnimation trans1 = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, -0.0f, Animation.RELATIVE_TO_PARENT, -1.5f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, -0.5f); 
    trans1.setStartOffset(1); 
    trans1.setDuration(550); 
    anim.addAnimation(trans1); 

    AlphaAnimation opacity1 = new AlphaAnimation(1.0f, 0.0f); 
    opacity1.setStartOffset(300); 
    opacity1.setDuration(600); 
    anim.addAnimation(opacity1); 

    currentAnimCardContainer.startAnimation(anim); 

    mCardFrame.removeAllViews(); 

} 
関連する問題