android - 在 onClick 事件后 move 一个 ImageButton

标签 android move imagebutton

我在 RelativeLayaut 中有一个 imageButton。 我的 imageButton 是 300x350px 并且位于屏幕外(顶部)-300px onClick 按钮向下 300px 并在再次单击时返回到初始位置。效果就像一个弹出窗口。

我可以获得这个工作代码。

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/pag1" android:id="@+id/relativeLayout1"
android:drawingCacheQuality="high" android:layout_width="1024px"
android:layout_height="600px">
<ImageButton android:id="@+id/imageButton8" 
android:layout_width="300px" 
android:layout_height="350px" 
android:layout_marginLeft="720px" 
android:background="@drawable/popup" 
android:layout_marginTop="-300px">
</ImageButton>
</RelativeLayout>

代码

import android.widget.RelativeLayout.LayoutParams;
...

//activity declarations
protected static final int WIDTH = 300;
protected static final int HEIGHT = 350;
int count=0;
...

///click
    final ImageButton pop=(ImageButton) findViewById (R.id.imageButton8);
    pop.setOnClickListener(new OnClickListener(){

        public void onClick(View v) {
            count++;

            if (count==1){  
                LayoutParams lp = new LayoutParams(WIDTH,HEIGHT);
                lp.setMargins(720, -20, 4, 0);
                pop.setLayoutParams(lp);
            }
            else{
                LayoutParams lp = new LayoutParams(WIDTH,HEIGHT);
                lp.setMargins(720, -300, 4, 0);
                pop.setLayoutParams(lp);
                count=0;
            }
        }   
    });

现在我不会添加到最终位置的平滑过渡。我认为在 FOR 循环中使用 sleep 功能。欢迎您的帮助

最佳答案

我认为您应该首先确定您的目标平台,因为 Honeycomb 中有一个新的动画框架。看看this article .

如果您的目标是 3.0 之前的版本,那么最简单的方法是在 anim.xml 中定义动画并使用 android.view.animation.AnimationUtils 将其加载到您的 Activity 中。然后,一旦您在 View 上设置了新的布局参数,只需在 View 上调用 public void startAnimation(动画动画),它就会为您制作动画。 Java 中的两行和 XML 中的更多行。

看看Animation Resources也是。

关于android - 在 onClick 事件后 move 一个 ImageButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6588138/

相关文章:

android - 退出/关闭应用程序

Java Android - ImageButton 显示但不允许我单击它

android - 如何将 4 张图像加入一个图像按钮 (android)

java - Android 文件浏览器抛出 IO 异常?

java - 如何在 Android OKHTTPClient 请求上设置(OAuth token )授权 header

android - 动态背景图像在 Android 3.1 + PhoneGap 上不起作用

C++ 在技术上如何在没有拷贝的情况下 move 函数返回值?

python - 如何使用 Python 在挂载之间 move 文件?

c++ - 将局部变量 move 到不同类型的返回值

html - 当我按下静音按钮时,文本会突出显示