android 动画后按钮不起作用

2023-12-20

我在 android 中创建了一个视图,我需要从下到上对其进行动画处理,反之亦然。我已经使用 TranslateAnimation 成功地做到了这一点。但问题是我的视图上有几个按钮。当动画出现时,触摸点保留在原始位置并且不会移动到新位置。因此,当我再次单击按钮的原始位置时,从上到下的动画会运行,但按钮不存在。

我在互联网上搜索过,人们说使用参数调用 view.layout ,但我的问题是如何获取视图的最新位置,因为我尝试获取动画开始和动画结束时的位置,但它保持不变。

另外,请不要给出它实际上不会移动它创建副本并移动它等的视图的答案,因为我已经搜索但找不到描述或实现的正确解决方案。

这是代码:

import android.content.Context;
import android.graphics.Matrix;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.Transformation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import com.Card_Android.R;

public class GameMenuScreenAnimated extends LinearLayout {

private final View mainView;

public GameMenuScreenAnimated(Context context,
        final GameViewController dashboardVC) {

    super(context);
    LayoutInflater inflater = LayoutInflater.from(context);
    mainView = inflater.inflate(R.layout.main_menu, this);

    ImageButton btn = (ImageButton) mainView.findViewById(R.id.trade);
    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            System.out.println("yaaaaaay!!!!");

        }
    });

    slideDown(mainView);
}

public View getMainView() {
    return mainView;
}

private void slideUp(final View view) {

    Animation slide = new TranslateAnimation(Animation.RELATIVE_TO_SELF,
            0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
            Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
            0.5f);
    slide.setDuration(1000);
    slide.setFillAfter(true);
    slide.setFillEnabled(true);
    view.startAnimation(slide);
    slide.setAnimationListener(new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
            int[] startPosition = new int[2];
            view.getLocationOnScreen(startPosition);
            System.out.println("onAnimationStart " + startPosition[0]
                    + " , " + startPosition[1]);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
             int[] endPosition = new int[2];
             view.getLocationOnScreen(endPosition);
             System.out.println("onAnimationEnd " + endPosition[0] + " , "
             + endPosition[1]);

        }

    });

}

private final AnimationListener slideDownAnimationListener = new AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) {
    }

    @Override
    public void onAnimationRepeat(Animation animation) {
    }

    @Override
    public void onAnimationEnd(Animation animation) {
        final int left = mainView.getLeft();
        final int top = mainView.getTop();
        final int right = mainView.getRight();
        final int bottom = mainView.getBottom();
        mainView.layout(left, (int) Math.round(top + 0.25 * top), right,
                (int) Math.round(bottom + 0.25 * bottom));
    }
};

private final Animation slideDownAnimation = new TranslateAnimation(
        Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
        Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.25f);

private void slideDown(final View view) {
    slideDownAnimation.setDuration(1000);
    slideDownAnimation.setFillAfter(true);
    slideDownAnimation.setFillEnabled(true);
    slideDownAnimation.setAnimationListener(slideDownAnimationListener);
    view.startAnimation(slideDownAnimation);
}
}

The XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="bottom"
>
 <LinearLayout 
    android:id="@+id/tableLayout1"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:gravity="center"
    >

        <ImageButton
            android:id="@+id/trade"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_margin="0dp"

            android:background="@null"
            android:scaleType="centerCrop"
            android:src="@drawable/upgrade_btn" />

    </LinearLayout>

 <LinearLayout 
    android:id="@+id/tableLayout1"
    android:layout_width="fill_parent"
    android:gravity="center"
    android:layout_height="wrap_content"
    >

        <ImageButton
                    android:id="@+id/evolution"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="0dp"
                    android:background="@null"
                    android:scaleType="centerCrop"
                    android:src="@drawable/sell_btn" />
            <ImageButton
                    android:id="@+id/trade"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="0dp"
                    android:background="@null"
                    android:scaleType="centerCrop"
                    android:src="@drawable/upgrade_btn"
                     />

    </LinearLayout>
</LinearLayout>

可绘制的 xml

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/evolution">
<item android:state_pressed="true" android:drawable="@drawable/menu_off" /> <!-- pressed -->
<item android:state_focused="true" android:drawable="@drawable/menu_on" /> <!-- focused -->
<item android:drawable="@drawable/menu_on" /> <!-- default -->
</selector>

我想做的就是创建一个简单的菜单,其中有几个按钮,单击按钮即可滑入和滑出。


简单的解决方案: 只需使用 AnimationListener 并使用 .clearAnimation() 即可。

Animation animationLeft= AnimationUtils.loadAnimation(getContext(), R.anim.slide_right);
animationLeft.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        imageView.clearAnimation();//This Line Added
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

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

android 动画后按钮不起作用 的相关文章

随机推荐

  • 使用 mysql 别名从 2 个表中选择列

    我有 2 个表 table a 和 table b 两者都包含一个名为 open 的列 table a open 36 99 36 85 36 40 36 33 36 33 table b open 4 27 4 46 4 38 4 22
  • 无法使用 Flask 服务器在 IIS 上运行 dash 应用程序

    我的 IIS Windows Server 2016 上有两个网站 都使用 Dash 和 Flask 第一个是最小的working由 app py 和 web config 组成的示例 由于某种原因 我无法让第二个站点正常工作 下面附有两个
  • 如何处理 d3.layout.stack() 中缺少数据点的图层

    我正在使用 d3 stack 创建堆积面积图 但如果每层中的项目数量不相等 则会出现错误 我从这样的数据数组开始 key Group1 value date key Group1 value date key Group1 value da
  • 响应所有方法调用的 Python 类的实例

    有没有办法创建一个实例响应任意方法调用的类 我知道有一个特殊的方法 getattr self attr 当有人尝试访问实例的属性时会调用它 我正在寻找类似的东西 使我也能够拦截方法调用 期望的行为看起来像这样 class A object
  • 有没有一种算法可以检测两幅图像之间的差异?

    我正在寻找一种算法或库 可以发现两个图像之间的差异 例如在 查找错误 游戏中 并输出包含这些更改的边界框的坐标 我对 Python C 或几乎任何其他语言的算法持开放态度 如果您只是想显示差异 那么您可以使用下面的代码 FastBitmap
  • 如何从 R 中的 xlsx 文件中检测“删除线”样式

    我必须检查包含 的数据删除线 在 R 中导入 excel 文件时的格式 我们有什么方法可以检测到它们吗 欢迎使用 R 和 Python 方法 R 溶液 the tidyxl 包可以帮助你 例如 temp xlsx 其中数据位于第一张纸的 A
  • 如何在 Eclipse 中使用 Android 操作系统 VirtualBox 作为设备

    我在这里找到了有关如何运行 Android 操作系统的教程 http www javacodegeeks com 2010 06 install android os on pc with html http www javacodegee
  • jQuery 文档.ready

    我对 jQuery 中的 document ready 有点困惑 你什么时候在里面定义javascript函数 document ready 什么时候不呢 将所有 javascript 代码放入 document ready 中是否足够安全
  • gnuplot 文件有标准的文件扩展名吗?

    我见过 gnu plt and gplot作为 gnuplot 脚本的文件扩展名 我知道 Linux 不关心文件扩展名 但是什么扩展名最普遍地向人类声明 我是一个 gnuplot 脚本 正如罗曼 珀森博士和尼尔布都指出的那样这篇维基教科书文
  • 使用 XSD 架构进行 Xml 验证

    以下代码帮助我使用 XSD 架构验证 XML 文件 XmlReaderSettings settings new XmlReaderSettings settings Schemas Add null xsdFilePath setting
  • WCF 路由服务 - 动态错误处理

    我正在了解 WCF 路由服务可以做什么 仍处于 摆弄它看看它能做什么 阶段 我对路由服务的理解是 当消息通过时 该服务将尝试将其传递到备份列表中首先出现的端点 如果失败 它将继续尝试下一个 然后再尝试下一个 直到有东西起作用或者没有什么可以
  • opencv中的手动灰度太慢

    注意 我必须手动执行此操作 因此不建议我使用库函数 cvtColor 我是 opencv 的新手 我正在尝试使用以下公式对彩色图像进行灰度化 r g b r g b r g b 3 这是我转换为灰度的方法 C Mat dst src clo
  • 为什么 sizeof(int) 不大于-1? [复制]

    这个问题在这里已经有答案了 这是我的 C 代码 为什么输出为 False 为什么 4 gt 1 code include
  • NoMethodError: # 的未定义方法“[]”

    我是 Ruby on Rails 的新手 我正在使用omni auth 进行 facebook 和 google 身份验证并陷入困境 当我运行 rake db migrate 时 显示以下错误 rake aborted NoMethodEr
  • “Using”关键字调用基类构造函数

    我有以下基类 class Grammateas public Grammateas std string name name name virtual Grammateas private std string name 以及以下派生类 c
  • 从 Linux 用户空间设置 16550A UART 硬件 FIFO 中断级别

    我目前正在使用 16550 兼容的 UART 并且我希望能够更改 FIFO 中断触发级别 我在高 UART 负载下丢失字节 并且我想降低阈值 这是一个动力不足的嵌入式系统 当然 如果我愿意 我可以在 8250 port c 驱动程序中更改它
  • c++ 如何运行内容存储在字符数组中的.exe文件?

    我正在制作一个特定的程序 我只是想知道我是否可以这样做 运行一个文件 其内容存储在 WINDOWS 上的字符数组中 这是读取可执行文件并将其存储在字符数组中的代码 filetoopen open C blahlbah exe ios bin
  • React.memo - 为什么我的相等函数没有被调用?

    我有一个父组件 它根据通过 props 接收到的数组来渲染子组件的集合 import React from react import PropTypes from prop types import shortid from shortid
  • Ruby openssl 中的 AES 等效项?

    Gibberish 库提供了一个很好的 CBC 算法 In Jascascript GibberishAES enc Made with Gibberish n password Outputs U2FsdGVkX1 21O5RB08bav
  • android 动画后按钮不起作用

    我在 android 中创建了一个视图 我需要从下到上对其进行动画处理 反之亦然 我已经使用 TranslateAnimation 成功地做到了这一点 但问题是我的视图上有几个按钮 当动画出现时 触摸点保留在原始位置并且不会移动到新位置 因