在 onClick 事件后移动 ImageButton

发布于 2024-11-18 23:43:23 字数 1587 浏览 7 评论 0原文

我在RelativeLayaut 中有一个imageButton。 我的 imageButton 为 300x350px,位于屏幕外部(顶部)-300px,单击按钮时向下移动 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循环中使用睡眠函数。欢迎您的帮助

I have an imageButton inside a RelativeLayaut.
My imageButton is 300x350px and positioned outside the screen (on top) -300px onClick the button go down 300px and go back to the initial position when click again. The effect is like a popup window.

I could obtain this working code.

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>

CODE

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;
            }
        }   
    });

NOW I wont to add a smooth transition to the final position. I think in a FOR cycle using a sleep function. Your HELP is welcome

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

相思故 2024-11-25 23:43:23

我认为您应该首先决定您的目标平台是哪个平台,因为 Honeycomb 中有一个新的动画框架。请查看这篇文章

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

也请查看动画资源

I think you should start with deciding which platform you are targeting as there is a new animation framework in Honeycomb. Have a look at this article.

If however you are targeting pre-3.0 versions then the simplest way is to define your animation in anim.xml and load it in your activity by using android.view.animation.AnimationUtils. Then once you've set your new layout params on the view, simply call public void startAnimation (Animation animation) on the view and it will animate it for you. Two lines in Java and a few more in XML.

Take a look at Animation Resources too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文