0

Ich habe eine Constraint-Layout (Alpha9) mit Ansichten über sie verteilt, und ich habe eine bestimmte ImageView, die ich replizieren müssen und fügen Sie mehr von wie es. Das Layout ist in etwa so: Basic layoutAnsicht ConstraintLayout hinzufügen mit Einschränkungen ähnlich wie bei einem anderen Kind

Der Hauptzweck ist 5 jenes „Münze“ imageViews zu animieren, wenn die Taste gedrückt wird. Die imageViews i generiert müssen sha'll haben genaue Eigenschaften der die Imageview hinter dem Knopf unten (mit gleichen Einschränkungen) The imageView behind the button

Was ich versucht, Folgendes zu tun ist:

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

Und es gescheitert. EDIT: Es konnte nicht zeigen, was ich wollte, manchmal zeigt es eine Münze in der oberen linken Ecke des Bildschirms, manchmal zeigt es nichts, aber so oder so, die Anzahl der Münzen inkrementiert (was bedeutet, die Animation versucht tatsächlich zu tun) etwas und dann die onAnimationFinished Methode aufrufen)

ich für die Animation verwendet die Bibliothek EasyAndroidAnimations

der Code wie folgt:

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

Was erhalten Sie als Ausgabe, wenn Sie die Taste drücken?/ –

+0

Wenn ich richtig verstanden habe, möchten Sie, dass die Münze vom unteren Knopf zur obersten Münze wechselt und wenn die Animation endet, erhöht sich die Anzahl der Münzen um eins. Sie möchten sie also um fünf erhöhen, was bedeutet, dass 5 der Münzen von unten nach oben animiert werden sollen. Bin ich in der Nähe? – mihanovak1024

+0

Genau was ich will, aber soll ich es mit einer Münze machen, dann wird eine Million keinen Unterschied machen @ mihanovak1024 – Seaskyways

Antwort

6

ConstraintLayout.LayoutParams Cache seine Parameter, und ist mit dem Widget können Sie es auf verwenden verbunden sind, so dass Sie kann nicht einfach ein widget layoutParams zu einem anderen übergeben.

Sie müssen stattdessen ein neues layoutParams für Ihr neues Objekt erstellen und die entsprechenden Felder kopieren.

Zum Beispiel, wenn Sie so etwas wie:

<?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> 

Dann könnten Sie tun:

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

Diese auf die zweite Schaltfläche genau an der gleichen Stelle wie die in XML definiert setzen würden.

+0

Hallo Nicolas Roard, Wenn ich eine komplexe ui, zum Beispiel, der Button ist dynamisch und hat viele Layout-Parameter und viele Einschränkungen, was ich tun möchte, ist das Hinzufügen eines neuen Button an der gleichen Position, wie soll ich tun? Vielen Dank. – Changwei

+0

Wie auch immer, danke. ich es durch die folgende Art und Weise implementieren: 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

Verwandte Themen