在 onClick 事件后移动 ImageButton
我在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您应该首先决定您的目标平台是哪个平台,因为 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 callpublic 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.