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

2024-01-11

I have a constraint layout (alpha9) with views spread all over it, and I have one particular ImageView that I need to replicate and add more of like it. The layout is like so : Basic layout

The main purpose is to animate 5 of those "coin" imageViews when the button is pressed. The imageViews i need to generated sha'll have exact properties of the the imageView behind the Button down below (with same constraints) 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;
}

但它失败了。EDIT:它未能显示我想要的内容,有时它在屏幕的左上角显示一枚硬币,有时它不显示任何内容,但无论哪种方式,硬币的数量都会增加(意味着动画正在尝试实际执行某些操作,然后调用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();
}

活动主文件

<?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,并复制相应的字段。

例如,如果您有类似的内容:

<?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 中定义的按钮完全相同的位置。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将视图添加到constraintLayout,其约束类似于另一个子项 的相关文章

  • 没有这样的属性:类的 useLibrary

    我的项目在Android Studio上使用ndk 所以 build gradle如下 dependencies classpath com android tools build gradle experimental 0 4 0 app
  • 动态添加 TextView - Android

    如何动态添加 TextView 到此 注释掉的代码不起作用 public class myTextSwitcher extends Activity private TextView myText public myTextSwitcher
  • 检索特定联系人的组

    我想检索联系方式及其所属的组 我得到了列出手机中所有联系人组的代码 Cursor groupC getContentResolver query ContactsContract Groups CONTENT URI null null n
  • Android应用程序是否动态更改其目标API级别

    我有一个针对 Android API 级别 30 Android 11 的 Xamarin Forms 应用程序 其中安装了 Xamarin Twilio AudioSwitch v1 1 3 该应用程序可在 Play 商店中使用 并且在
  • 使用应用程序上下文滑动图像加载

    我在我的 Android 应用程序中使用 glide 进行图像加载 为了避免任何崩溃 我正在使用应用程序上下文加载图像 这对应用程序和内存的性能有何影响 这对应用程序和内存的性能有何影响 Glide提供了这么多 with 方法是有原因的 它
  • 在 CustomAdapters 中使用条件 if(view==null)

    我正在为 ListView 编写一个自定义适配器 它扩展了 BaseAdapter 并在此方法中 Override public View getView int position View convertView ViewGroup pa
  • ZipResourceFile 无法解析为类型

    我正在尝试重写我的应用程序以使用 APK 扩展文件 我一直在关注这里的文档http developer android com google play expansion files html http developer android
  • 呼叫转移

    我想将所有拨打我号码的呼叫转接至新的预定义号码 自动地 可以转接来电吗 也许至少对于 Froyo 来说是可能的 我找到了名为 Easy Call Forwarding 的应用程序 http www appstorehq com easyca
  • 获取当前 GPS 时出现 NullPointerException

    我有一个测试屏幕 其中有一个按钮 按下它会调用该服务 我正在尝试实现一种方法来获取当前用户的当前 GPS 位置 但在尝试调用时它崩溃了 谁能告诉我问题是什么吗 package com example whereyouapp import j
  • 如何以编程方式检测 Android 设备是否与 USB OTG 连接

    我正在使用定制 OTG 指纹扫描仪 我想检查 OTG 是否已连接到我的 Android 设备或未在特定的 Android 活动中 public class BootUpReceiver extends BroadcastReceiver p
  • Android WebView setCertificate 问题 SSL 问题

    我看过很多关于 SSL 错误的帖子和信息 并且我自己也偶然发现了一个 我尝试使用 GlobalSign CA BE 证书通过 Android WebView 访问网页 但收到不受信任的错误 对于大多数手机来说 处理这个问题效果很好 只需告诉
  • 返回 RxJava 的 Completable 的方法的命名约定

    我有一个带有视图类的 Android 应用程序 Fragment Activity 观察其ViewModel The ViewModel公开方法 例如getUserName返回Observable
  • 调用 MediaScannerConnection.scanFile 后,MediaStore 内的 Android 缩略图不会刷新

    我正在尝试构建一个类似画廊的应用程序 它将在外部存储上执行以下功能 列出所有包含图像的文件夹 列出所有可供公众使用的图像 不会探测里面的文件Android data 到目前为止 我可以列出所有图像以及包含图像的文件夹 然而 我后来发现这些图
  • 回购:找不到命令?

    我是 git 和 repo 的新手 我使用的是window 7 所以我使用cygwin 我已经从 cygwin 安装程序安装了 git 之后我尝试在 cygwin 中使用以下命令进行存储 repo init u git android gi
  • API级别29 Intent.ACTION_GET_CONTENT从下载文件夹返回错误的ID

    我正在尝试查找从文件选择器意图返回的 URI 的完整文件路径 我从互联网下载了一张图像 该图像保存在浏览器默认下载文件夹中 问题是 DocumentsContract getDocumentId content describer 返回的
  • Android中计算两个时间之间的差异

    我有两个字符串变量 例如 StartTime 和 EndTime 我需要通过用 StartTime 减去 EndTime 来计算 TotalTime StartTime和EndTime的格式如下 StartTime 08 00 AM End
  • java.io.IOException:Android Firebase 中的 FIS_AUTH_ERROR 但调试模式正常

    我在检索 firebase 令牌时遇到以下问题 FirebaseMessaging getInstance getToken 在调试模式下 我获取令牌并将其发送到服务器 在运行模式下 应用程序工作正常 它已发布 但我无法获取令牌 因为我已经
  • 无法放置双重 SharedPreferences

    出现错误 这种类型的共享首选项编辑器的 put double 方法未定义 Eclipse 提供了一种快速修复方法 将强制类型转换添加到编辑器 但是当我这样做时 它仍然给出错误 为什么我不能 put double 代码 Override pr
  • Android Studio 3.0 中的 Gradle 构建错误

    您能帮我解决 Android 3 0 中的 Gradle 构建问题吗 我是 Android Studio 的新手 以下是我在 AS 3 0 中的配置 gradle gt wrapper gt gradle wrapper propertie
  • 只有创建视图层次结构的原始线程才能触摸其视图。在安卓上[重复]

    这个问题在这里已经有答案了 我只是一个初学者 所以请原谅我问一个可能愚蠢的问题 我不明白只有创建视图层次结构的原始线程才能触摸其视图的含义 请有人告诉我为什么会发生此错误以及如何解决此问题 ThankYou 这是我的班级 public cl

随机推荐

  • 根据父属性反序列化 json 子类型

    我有一个带有动态的 jsonattribute孩子 如下所示 label Some label attribute lt Dynamic attribute object type TEXT lt Field used to map att
  • 如何用简单的 HTML DOM 来模拟子选择器?

    Fellas 我有一个令人讨厌的页面需要解析 但无法弄清楚如何使用它从中提取正确的数据块简单的 HTML DOM http simplehtmldom sourceforge net 因为它没有 CSS 子选择器支持 HTML ul cla
  • Android 模拟器 SD 卡映像已在使用中

    我已经关注了以下答案this https stackoverflow com questions 9913247 android virtual device问题没有成功 我无法回复发布的答案 缺乏声誉 所以我不得不提出一个新问题 全部清除
  • 自定义 SONOS 根浏览容器

    Sonos Labs 目前提供的文档 自定义根浏览容器 http musicpartners sonos com node 478 指出它可以使用 EDITORIAL GRID 或 LIST DisplayMode 有没有关于如何实现 Ap
  • 类验证器 - 验证对象数组

    我正在使用带有 NestJS 的类验证器包 并且我希望验证需要恰好有 2 个具有相同布局的对象的对象数组 到目前为止我有 import IsString IsNumber from class validator export class
  • Google 地图 API - 获取街道坐标

    Google Maps API 有没有办法获取某个位置的街道坐标 我想获取最近的街道坐标 例如 为了得到这个 我需要组成街道的所有坐标 有这样的事吗 您可以使用directionService 传递给定地址 或位置 作为来源and目的地到d
  • 使用CATransform3D创建翻转动画

    我正在尝试重新创建 UIViewAnimationTransitionFlipFromRight 和左 我这样做的原因如下所示 是在动画中间当图层被遮挡时对 AVCaptureVideoPreviewLayer 进行更改 UIViewAni
  • xcode 10.3 损坏的 xib

    将 xcode 更新到 10 3 版本后无法查看或操作所有 xib 文件 有什么解决办法吗 我的操作系统版本 10 14 4 18E226 删除派生数据 不起作用 从首选项中完全删除派生数据 然后重新启动计算机
  • jquery中如何获取textarea的值?

    如果我使用的是jquery 如何获取Textarea值
  • 让 Swift 相信函数由于抛出异常而永远不会返回

    因为 Swift 没有抽象方法 所以我创建了一个方法 其默认实现无条件地引发错误 这会强制任何子类重写抽象方法 我的代码如下所示 class SuperClass func shouldBeOverridden gt ReturnType
  • 触发子元素的 onclick 事件,但不触发父元素的 onclick 事件

    我有一些嵌套元素 每个元素都有一个 onclick 事件 在大多数情况下 我希望当用户单击子事件时触发这两个事件 父事件和子事件都会被触发 默认行为 但是 至少在一种情况下 我想触发孩子的 onclick 事件 来自 javascript
  • 推荐的 Android 音乐格式 - mp3、ogg 还是其他? [关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 我被问到我的项目需要哪种格式的音乐 通过查看文档 Android 平台似乎提供了一个不错的选择 音频当然不是我的强项 所以我想知道是否有一种最适
  • 为什么 du 或 echo 流水线不起作用?

    我正在尝试对当前目录中的每个目录使用 du 命令 所以我尝试使用这样的代码 ls du sb 但它没有按预期工作 它仅输出当前 的大小目录仅此而已 echo 也是同样的情况 ls echo 输出空行 为什么会发生这种情况 使用管道发送输出
  • 如何在 Java 中创建 PKI

    我想创建存储在数据库中的证书 但我不知道如何做到这一点 如果退出 API 或库可以帮助我做到这一点 谢谢 公钥基础设施不仅仅是签名公钥的数据库 例如 PKI 最重要的部分之一是使用 OCSP 协议撤销证书的能力 简而言之 用 java 构建
  • 将曲线拟合到数据集

    我有一个包含两个数据集的图 它产生轻微的梯度 其中最佳拟合曲线可能会被过度绘制 目前我只能得到一条最适合的直线 我明白scipy optimize curve fit应该能够帮助我 但这需要我知道我想要过度绘制的函数 我认为 下面是我的代码
  • 如何以编程方式隐藏/禁用 Android 软键盘上的表情符号

    是否可以隐藏特定的键盘按钮 我有一个EditText在某些设备上 其键盘上有笑脸 而在其他设备上则没有 我想在所有设备上隐藏它 下面是我的 XMLEditText android id id text editor android layo
  • 我应该如何在我的 ApplicationController 中使用 Draper?

    我的问题涉及以下开发堆栈 轨道3 2 1 德雷珀 0 14 血统1 2 5 我想做的是将导航传递到我的布局 所以我在我的过滤器中定义了一个之前的过滤器ApplicationController class ApplicationContro
  • MySQL 8 创建新用户,密码不起作用

    我使用 MySQL 已经好几年了 创建新用户直到 MySQL 5 x 版本的命令如下 GRANT ALL PRIVILEGES ON TO username localhost IDENTIFIED BY password 最近我安装了 M
  • 如何设置 Spring Boot 来运行 HTTPS / HTTP 端口

    Spring Boot 有一些属性来配置 Web 端口和 SSL 设置 但是一旦设置了 SSL 证书 http 端口就会变成 https 端口 那么 如何让两个端口同时运行 例如 80 和 443 正如您所看到的 只有一个端口的属性 在本例
  • 将视图添加到constraintLayout,其约束类似于另一个子项

    I have a constraint layout alpha9 with views spread all over it and I have one particular ImageView that I need to repli