我在 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/