0

我有一個約束佈局(alpha9)與視圖蔓延到它,我有一個特定的ImageView,我需要複製和添加更多喜歡它。 佈局是像這樣: Basic layout將約束添加到constraintLayout約束類似於另一個孩子

的主要目的是在按下按鈕時進行動畫那些「硬幣」 imageViews 5。我需要產生的imageViews sha'll有下面的按鈕向下拋在腦後了ImageView的確切性質(有相同的約束) The imageView behind the button

什麼,我試圖做的是以下幾點:

private ImageView generateCoin() { 
    ImageView coin = new ImageView(this); 
    constraintLayout.addView(coin,-1,originCoinImage.getLayoutParams());//originCoinImage is the imageView behind the button 
    coin.setImageResource(R.drawable.ic_coin); 
    coin.setScaleType(ImageView.ScaleType.FIT_XY); 
    coin.setVisibility(View.VISIBLE); 
    return coin; 
} 

它失敗。 編輯:它沒有顯示我想要的東西,有時它顯示在屏幕左上角的硬幣,有時它不顯示任何東西,但無論如何,硬幣數量增加(意味着動畫試圖實際上的東西,然後調用onAnimationFinished方法)

我用於動畫庫EasyAndroidAnimations

代碼如下:

MainActivity.java

@OnClick(R.id.addCoin) 
public void addCoin() { 
//  for (int x = 0 ; x < 5 ; x++){ 
    final View newCoin = generateCoin(); 

    sendCoin(newCoin, 300, new AnimationListener() { 
     @Override 
     public void onAnimationEnd(com.easyandroidanimations.library.Animation animation) { 
      incrementCoins(); 
      constraintLayout.removeView(newCoin); 
      shakeCoin(targetCoinImage, 200, null); 
     } 
    }); 
//  } 
} 

private void incrementCoins() { 
    coins++; 
    coinNumber.setText(String.valueOf(coins)); 
} 

private void sendCoin(View coinView, long duration, AnimationListener listener) { 
    new TransferAnimation(coinView) 
      .setDestinationView(targetCoinImage) 
      .setDuration(duration) 
      .setInterpolator(new AccelerateDecelerateInterpolator()) 
      .setListener(listener) 
      .animate(); 
} 

private void shakeCoin(View coinView, long duration, AnimationListener listener) { 
    new ShakeAnimation(coinView) 
      .setDuration(duration) 
      .setShakeDistance(6f) 
      .setNumOfShakes(5) 
      .setListener(listener) 
      .animate(); 
} 

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="seaskyways.canvasanddrawtest.MainActivity"> 

    <TextView 
     android:id="@+id/textView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="16dp" 
     android:layout_marginStart="16dp" 
     android:layout_marginTop="16dp" 
     android:text="Coins : " 
     android:textSize="30sp" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintTop_toTopOf="parent" /> 

    <TextView 
     android:id="@+id/coinNumber" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginEnd="16dp" 
     android:layout_marginLeft="8dp" 
     android:layout_marginRight="16dp" 
     android:layout_marginStart="8dp" 
     android:text="0" 
     android:textColor="@color/colorPrimary" 
     android:textSize="50sp" 
     android:textStyle="normal|bold" 
     app:layout_constraintBottom_toBottomOf="@+id/textView" 
     app:layout_constraintLeft_toRightOf="@+id/textView" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="@+id/textView" /> 

    <ImageView 
     android:id="@+id/coinOrigin" 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:scaleType="fitXY" 
     app:layout_constraintBottom_toBottomOf="@+id/addCoin" 
     app:layout_constraintLeft_toLeftOf="@+id/addCoin" 
     app:layout_constraintTop_toTopOf="@+id/addCoin" 
     app:srcCompat="@drawable/ic_coin" 
     app:layout_constraintRight_toRightOf="@+id/addCoin" /> 

    <ImageView 
     android:id="@+id/coinTarget" 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:layout_marginLeft="16dp" 
     android:layout_marginStart="16dp" 
     android:scaleType="fitXY" 
     app:layout_constraintBottom_toBottomOf="@+id/coinNumber" 
     app:layout_constraintLeft_toRightOf="@+id/coinNumber" 
     app:layout_constraintTop_toTopOf="@+id/coinNumber" 
     app:srcCompat="@drawable/ic_coin" /> 

    <Button 
     android:id="@+id/addCoin" 
     android:layout_width="0dp" 
     android:layout_height="75dp" 
     android:layout_marginBottom="16dp" 
     android:layout_marginEnd="16dp" 
     android:layout_marginLeft="16dp" 
     android:layout_marginRight="16dp" 
     android:layout_marginStart="16dp" 
     android:text="Button" 
     android:textAlignment="center" 
     android:textSize="36sp" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" /> 
</android.support.constraint.ConstraintLayout> 
+0

當你按下按鈕時,你會得到什麼輸出?/ –

+0

如果我理解正確,你希望硬幣從底部按鈕到頂部硬幣,當動畫結束時,硬幣數量增加1。所以你想把它們增加5,這意味着5個硬幣應該從底部到頂部硬幣動畫。我關門了嗎? – mihanovak1024

+0

正是我想要的,但是我應該用一枚硬幣來完成,那麼一百萬就不會有所作爲@ mihanovak1024 – Seaskyways

回答

6

ConstraintLayout.LayoutParams緩存它的參數,並關聯到你使用它的小部件,所以你不能簡單地傳遞一個widget的的LayoutParams到另一個。

您必須爲您的新對象生成一個新的layoutParams,並複製相應的字段。

舉例來說,如果你有這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/content_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <Button 
     android:text="Button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/button" 
     android:layout_marginTop="16dp" 
     app:layout_constraintTop_toTopOf="parent" 
     android:layout_marginStart="16dp" 
     app:layout_constraintLeft_toLeftOf="parent" 
     android:layout_marginLeft="16dp" /> 

</android.support.constraint.ConstraintLayout> 

然後,你可以這樣做:

ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.content_main); 
ConstraintLayout.LayoutParams params = 
       (ConstraintLayout.LayoutParams) button.getLayoutParams(); 

Button anotherButton = new Button(this); 

ConstraintLayout.LayoutParams newParams = new ConstraintLayout.LayoutParams(
       ConstraintLayout.LayoutParams.WRAP_CONTENT, 
       ConstraintLayout.LayoutParams.WRAP_CONTENT); 

newParams.leftToLeft = params.leftToLeft; 
newParams.topToTop = params.topToTop; 
newParams.leftMargin = params.leftMargin; 
newParams.topMargin = params.topMargin; 

layout.addView(anotherButton, -1, newParams); 

這將使第二個按鈕正好在同一個地方,如XML定義的。

+0

嗨尼古拉斯羅德, 如果我有一個複雜的用戶界面,例如,按鈕是動態的,有許多佈局參數和許多約束,我想要做的是在同一位置添加一個新的按鈕,我該怎麼辦?謝謝。 – Changwei

+0

無論如何,謝謝。 我通過以下方式實現: VAL screenshotViewLayoutParams = ConstraintLayout.LayoutParams(cl.width, cl.height) screenshotView.layoutParams = screenshotViewLayoutParams cs.connect(screenshotView.id,ConstraintSet.START,ConstraintSet.PARENT_ID, ConstraintSet.START,cl.x.toInt()) cs.connect(screenshotView.id,ConstraintSet.TOP,ConstraintSet.PARENT_ID,ConstraintSet.TOP,cl.y.toInt()) – Changwei