开发者

Move an ImageButton after onClick event

开发者 https://www.devze.com 2023-03-17 17:35 出处:网络
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 c

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消