简体   繁体   English

如何在点击事件结束之前添加图片?

[英]How to add the image before the on click event ends?

I am a beginner in this android studio and I am trying to insert an image for 1 second to a constraint layout by clicking on it and then deleting all the views that have that constraint layout, I attach the code I have to facilitate reading.我是这个 android 工作室的初学者,我试图通过单击将图像插入约束布局 1 秒,然后删除所有具有该约束布局的视图,我附上了我必须方便阅读的代码。

constraintLayoutNosotros.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View view) {
        ImageView img = new ImageView(activity)

        img.setId(View.generateViewId());
        img.setImageResource(R.mipmap.palito2);
        constraintLayout.addView(img);

    }
}
);

I want to clarify that the image adds it correctly only that I want it to add it for a period of time and then remove it, but when entering those inner classes I lose control over that.我想澄清一下,图像正确添加它只是我希望它添加一段时间然后删除它,但是当进入那些内部类时,我失去了对它的控制。
I was investigating if there is any Listener at the end of the onclick event or something like this and I didn't find anything, I already tried addOnLayoutChangeListener , setOnHierarchyChangeListener and setOnTouchListener and in all of them it always adds the image out of my reach.我正在调查 onclick 事件或类似事件的末尾是否有任何侦听器,但我没有找到任何东西,我已经尝试过addOnLayoutChangeListenersetOnHierarchyChangeListenersetOnTouchListener ,并且在所有这些事件中,它总是添加我无法触及的图像。
The last thing that comes to mind is with some animation but I'm not sure it's the correct way.最后想到的是一些 animation 但我不确定这是正确的方法。 sorry for my english, I'm from uruguay对不起我的英语,我来自乌拉圭

You can use a Handler with an amount of seconds you want to stay before removing the view from the layout在从布局中删除视图之前,您可以使用 Handler 并保留几秒钟

Here I am using 3 seconds.在这里,我使用 3 秒。

constraintLayoutNosotros.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View view) {
        ImageView img = new ImageView(activity)

        img.setId(View.generateViewId());
        img.setImageResource(R.mipmap.palito2);
        constraintLayout.addView(img);


        new Handler(getMainLooper()).postDelayed(new Runnable() {
            @Override
            public void run() {
                constraintLayout.removeView(image)
            }
        }, 3000); // 3 seconds
        
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM