2017-10-18 49 views
1

我想要動畫卡翻轉,然後用動畫把它扔掉。大部分我已經完成了。問題是我無法將卡片可靠地移除。Android onAnimationEnd刪除查看(自己)不刪除

問題:如何在動畫完成後可靠地刪除視圖? (同樣我使用AnimationSet,所以這可能會改變一切)

這是我的代碼的樣子。

flingCardAway(問題區域)

private void flingCardAway(){ 
    final View cardContainer = findViewById(R.id.card_container); 
    ViewCompat.setTranslationZ(cardContainer, 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); 

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



    anim.setAnimationListener(new Animation.AnimationListener(){ 
     @Override 
     public void onAnimationStart(Animation arg0) { 
     } 
     @Override 
     public void onAnimationRepeat(Animation arg0) { 
     } 
     @Override 
     public void onAnimationEnd(Animation arg0) { 
      final View cardContainer2 = findViewById(R.id.card_container); 
      ((ViewGroup)cardContainer2.getParent()).removeView(cardContainer2); 
     } 
    }); 
} 

參考滿級 Study.java

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 Study extends AppCompatActivity { 
    LinearLayout mFlipCardLinearLayout; 
    LinearLayout mCardFlippedButtons; 

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

     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/Home 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(){ 
     final View cardContainer = findViewById(R.id.card_container); 
     ViewCompat.setTranslationZ(cardContainer, 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); 

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



     anim.setAnimationListener(new Animation.AnimationListener(){ 
      @Override 
      public void onAnimationStart(Animation arg0) { 
      } 
      @Override 
      public void onAnimationRepeat(Animation arg0) { 
      } 
      @Override 
      public void onAnimationEnd(Animation arg0) { 
       final View cardContainer2 = findViewById(R.id.card_container); 
       ((ViewGroup)cardContainer2.getParent()).removeView(cardContainer2); 
      } 
     }); 
    } 

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

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

} 

FlipAnimation.java(並不重要,只是一個參考)

package com.example.trevorwood.biggles.study; 

import android.graphics.Camera; 
import android.graphics.Matrix; 
import android.view.View; 
import android.view.animation.AccelerateDecelerateInterpolator; 
import android.view.animation.Animation; 
import android.view.animation.Transformation; 

public class FlipAnimation extends Animation { 
    private Camera camera; 
    private View fromView; 
    private View toView; 

    private float centerX; 
    private float centerY; 

    private boolean forward = true; 

    /** 
    * Creates a 3D flip animation between two views. 
    * 
    * @param fromView First view in the transition. 
    * @param toView Second view in the transition. 
    */ 
    public FlipAnimation(View fromView, View toView) { 
     this.fromView = fromView; 
     this.toView = toView; 

     setDuration(300); 
     setFillAfter(false); 
     setInterpolator(new AccelerateDecelerateInterpolator()); 
    } 

    public void reverse() { 
     forward = false; 
     View switchView = toView; 
     toView = fromView; 
     fromView = switchView; 
    } 

    @Override 
    public void initialize(int width, int height, int parentWidth, int parentHeight) { 
     super.initialize(width, height, parentWidth, parentHeight); 
     centerX = width/2; 
     centerY = height/2; 
     camera = new Camera(); 
    } 

    @Override 
    protected void applyTransformation(float interpolatedTime, Transformation t) { 
     // Angle around the y-axis of the rotation at the given time 
     // calculated both in radians and degrees. 
     final double radians = Math.PI * interpolatedTime; 
     float degrees = (float) (180.0 * radians/Math.PI); 

     // Once we reach the midpoint in the animation, we need to hide the 
     // source view and show the destination view. We also need to change 
     // the angle by 180 degrees so that the destination does not come in 
     // flipped around 
     if (interpolatedTime >= 0.5f) { 
      degrees -= 180.f; 
      fromView.setVisibility(View.GONE); 
      toView.setVisibility(View.VISIBLE); 
     } 

     if (forward) 
      degrees = -degrees; //determines direction of rotation when flip begins 

     final Matrix matrix = t.getMatrix(); 
     camera.save(); 
     camera.translate(0, 0, Math.abs(degrees)*6); 
     camera.getMatrix(matrix); 
     camera.rotateY(degrees); 
     camera.getMatrix(matrix); 
     camera.restore(); 
     matrix.preTranslate(-centerX, -centerY); 
     matrix.postTranslate(centerX, centerY); 
    } 
} 

我看到這個,它幫助,但沒有解決我的問題。 End animation event android

我也看到了這一點,但我不知道如何將它實現到我自己的項目中。 android animation is not finished in onAnimationEnd

谷歌文檔都指向this link,但這在快速點擊下不可靠。

+0

我還不確定爲什麼,但這似乎已經解決了問題。 'mCardFrame.removeAllViews();' –

回答

0

這對我有效。即使你想考慮一下,它可能不會起作用。 (如果轉換爲JavaScript,100%將無法工作,因爲視圖在動畫結束之前已被刪除。)也許某些具有更多Android體驗的人可以解釋其原因。

我爲什麼會這樣做的假設(請記住,這只是一個猜測):是Android的動畫實際上將視圖完全轉化爲其他東西,而原始視圖變爲透明。然後,當動畫結束時,動畫視圖將被刪除,並且原始視圖將變回可見狀態。

private void flingCardAway(){ 

    View cardContainer = findViewById(R.id.card_container); 
    ViewCompat.setTranslationZ(cardContainer, 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(10); 
    rotate1.setDuration(500); 
    anim.addAnimation(rotate1); 

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

    TranslateAnimation trans2 = 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); 
    trans2.setStartOffset(100); 
    trans2.setDuration(100); 
    anim.addAnimation(trans2); 

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

    cardContainer.setAnimation(anim); 


    mCardFrame.removeAllViews(); 

}