Android 将视图添加到constraintLayout,其约束类似于另一个子对象

Android 将视图添加到constraintLayout,其约束类似于另一个子对象,android,view,imageview,android-animation,android-constraintlayout,Android,View,Imageview,Android Animation,Android Constraintlayout,我有一个约束布局(alpha9),视图遍布其中,我有一个特定的ImageView,我需要复制并添加更多类似的视图。 布局如下: 主要目的是在按下按钮时,为其中5个“硬币”图像视图设置动画。我需要生成的imageView将在下面的按钮后面具有imageView的精确属性(具有相同的约束) 我试图做的是: private ImageView generateCoin() { ImageView coin = new ImageView(this); constraintLayou

我有一个约束布局(alpha9),视图遍布其中,我有一个特定的ImageView,我需要复制并添加更多类似的视图。 布局如下:

主要目的是在按下按钮时,为其中5个“硬币”图像视图设置动画。我需要生成的imageView将在下面的按钮后面具有imageView的精确属性(具有相同的约束)

我试图做的是:

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方法)

我用动画库轻松制作动画

代码如下:

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();
}
@OnClick(R.id.addCoin)
公共硬币{
//对于(int x=0;x<5;x++){
最终视图newCoin=generateCoin();
sendCoin(newCoin,300,new AnimationListener(){
@凌驾
AnimationEnd(com.easyandroidanimations.library.Animation)上的公共无效{
增加硬币();
constraintLayout.removeView(newCoin);
shakeCoin(targetCoinImage,200,空);
}
});
//        }
}
私有void incrementCoins(){
硬币++;
coinNumber.setText(字符串.valueOf(硬币));
}
私有void sendCoin(视图coinView、长持续时间、AnimationListener侦听器){
新的TransferAnimation(coinView)
.setDestinationView(targetCoinImage)
.setDuration(持续时间)
.setInterpolator(新的AcceleratedAccelerateInterpolator())
.setListener(侦听器)
.animate();
}
私有void shakeCoin(视图coinView、长持续时间、AnimationListener侦听器){
新ShakeAnimation(coinView)
.setDuration(持续时间)
.setShakeDistance(6f)
.setNumOfShakes(5)
.setListener(侦听器)
.animate();
}
活动\u 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>

ConstraintLayout.LayoutParams
缓存其参数,并与使用它的小部件相关联,因此不能简单地将一个小部件的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>

这将使第二个按钮与XML中定义的按钮完全位于同一位置。

当您按下按钮时,您会得到什么作为输出?/如果我理解正确,您希望硬币从底部按钮转到顶部硬币,当动画结束时,硬币数量增加1。因此,您希望将它们增加5,这意味着5个硬币应该从底部到顶部设置动画。我接近了吗?这正是我想要的,但是我应该用一枚硬币来做吗,那么一百万不会有什么不同@Mihanovak1024所以动画应该垂直上升?或者它必须移动还是什么?你想要什么动画?简单的TranslateAnimation可以吗?@mihanovak1024这不是关于动画,而是关于imageview被程序正确放置的问题。Ihi Nicolas Roard,如果我有一个复杂的ui,例如,按钮是动态的,有许多布局参数和许多约束,我想做的是在同一位置添加一个新按钮,我该怎么办?谢谢,不管怎样,谢谢你。我通过以下方式实现它: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\u id、ConstraintSet.TOP、cl.y.toInt())
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);