将视图添加到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,其约束类似于另一个子项 的相关文章

  • 如何对这个字符串进行子串化

    我想得到这个字符串的 4 个部分 String string 10 trillion 896 billion 45 million 56873 我需要的4个部分是 10万亿 8960亿 4500万 和 56873 我所做的是删除所有空格 然
  • 如何在 Android 中保存相机的临时照片?

    在尝试从相机拍照并将其保存到应用程序的缓存文件夹中时 我没有得到任何可见的结果 应用程序不会崩溃 但在 LogCat 上 当我尝试将 ImageView src 字段设置为刚刚获取的文件的 URI 时 我收到此消息 09 17 14 03
  • Android Studio 3.0 Canary 9 - 无法解析包

    我在 Android Studio 3 0 Canary 9 中遇到几个错误 这些错误是 无法解析 android 软件包 下面列出了一些错误 我刚刚安装了 SDK 的所有额外软件包 但仍然收到 gradle 构建错误 Error 82 1
  • android xamarin 中的 reCaptcha

    我想在 Xamarin android 应用程序中实现验证码 我抓住了这个在 Android 中集成 googles reCaptcha 验证 https www c sharpcorner com article how to integ
  • 当文本输入聚焦在 React Native for Android 的底部工作表上时,视图移出屏幕

    我正在使用图书馆 https github com osdnk react native reanimated bottom sheet https github com osdnk react native reanimated bott
  • 在画布上绘图

    我正在编写一个 Android 应用程序 它可以在视图的 onDraw 事件上直接绘制到画布上 我正在绘制一些涉及单独绘制每个像素的东西 为此我使用类似的东西 for int x 0 x lt xMax x for int y 0 y lt
  • Android 中 Kotlin 协程的正确使用方式

    我正在尝试使用异步更新适配器内的列表 我可以看到有太多的样板 这是使用 Kotlin 协程的正确方法吗 这个可以进一步优化吗 fun loadListOfMediaInAsync async CommonPool try Long runn
  • CollapsingToolBarLayout - 状态栏稀松布颜色不改变

    几天前我更新了我的 android studio 并开始使用 CoordinatorLayout 和 CollapsingToolbarLayout 只是尝试一些东西 工具栏稀松布颜色似乎覆盖了状态栏初始颜色和状态栏稀松布颜色 从 xml
  • Android SIP 来电使用带有广播接收器的服务

    大家好 其实我正在尝试创建一个应用程序 支持基于 SIP 通过互联网进行音频呼叫 这里使用本机 sip 我遇到了来电问题 我已经完成了服务的注册部分 但是在接听电话时我无法接听电话 请帮助我 Service file package exa
  • 使用 Android 发送 HTTP Post 请求

    我一直在尝试从 SO 和其他网站上的大量示例中学习 但我无法弄清楚为什么我编写的示例不起作用 我正在构建一个小型概念验证应用程序 它可以识别语音并将其 文本 作为 POST 请求发送到 node js 服务器 我已确认语音识别有效 并且服务
  • 尝试将相机切换回前面但出现异常

    尝试将相机切换回前面 但出现异常 找不到 问题请检查并帮助 error 01 27 11 49 00 376 E AndroidRuntime 30767 java lang RuntimeException Unable to start
  • Android MediaExtractor seek() 对 MP3 音频文件的准确性

    我在使用 Android 时无法在eek 上获得合理的准确度MediaExtractor 对于某些文件 例如this one http www archive org download emma solo librivox emma 01
  • Google 云端硬盘身份验证异常 - 需要许可吗? (v2)

    我一直在尝试将 Google Drive v2 添加到我的 Android 应用程序中 但无法获得授权 我收到 UserRecoverableAuthIOException 并显示消息 NeedPermission 我感觉 Google A
  • 如何默认在 ActionOpenDocument 意图中显示“内部存储”选项

    我需要用户选择一个自定义文件类型的文件 并将其从 Windows 文件资源管理器拖到 Android 设备上 但默认情况下内部存储选项不可用 当我使用以下命令启动意图时 var libraryIntent new Intent Intent
  • 在两个活动之间传输数据[重复]

    这个问题在这里已经有答案了 我正在尝试在两个不同的活动之间发送和接收数据 我在这个网站上看到了一些其他问题 但没有任何问题涉及保留头等舱的状态 例如 如果我想从 A 类发送一个整数 X 到 B 类 然后对整数 X 进行一些操作 然后将其发送
  • 尝试在 ubuntu 中编译 android 内核时出错

    我正在尝试从源代码编译 Android 内核 并且我已经下载了所有正确的软件包来执行此操作 但由于某种原因我收到此错误 arm linux androideabi gcc error unrecognized command line op
  • 我的设备突然没有显示在“Android 设备选择器”中

    我正在使用我的三星 Galaxy3 设备来测试过去两个月的应用程序 它运行良好 但从今天早上开始 当我将设备连接到系统时 它突然没有显示在 Android 设备选择器 窗口中 我检查过 USB 调试模式仅在我的设备中处于选中状态 谁能猜出问
  • Android 中麦克风的后台访问

    是否可以通过 Android 手机上的后台应用程序 服务 持续监控麦克风 我想做的一些想法 不断聆听背景中的声音信号 收到 有趣的 音频信号后 执行一些网络操作 如果前台应用程序需要的话 后台应用程序必须能够智能地放弃对麦克风的访问 除非可
  • 如何在Xamarin中删除ViewTreeObserver?

    假设我需要获取并设置视图的高度 在 Android 中 众所周知 只有在绘制视图之后才能获取视图高度 如果您使用 Java 有很多答案 最著名的方法之一如下 取自这个答案 https stackoverflow com a 24035591
  • 将两个文本视图并排放置在布局中

    我有两个文本视图 需要在布局中并排放置 并且必须遵守两条规则 Textview2 始终需要完整显示 如果布局中没有足够的空间 则必须裁剪 Textview1 例子 文本视图1 文本视图2 Teeeeeeeeeeeeeeeeeextview1

随机推荐

  • 根据父属性反序列化 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