我在使用 Android 动画 API 时遇到了很多麻烦。
我需要在屏幕(600px * 1024px)上从左下角到顶部动画图像(400px * 600px)。此外,动画开始时,大部分图像应位于屏幕之外。
这是我的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/common_background">
<ImageView android:id="@+id/hand"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="matrix"
android:src="@drawable/howto_throw_hand" />
</RelativeLayout>
活动逻辑:
public class MyActivity extends Activity
{
ImageView hand;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
hand = (ImageView)findViewById(R.id.hand);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
//initial positioning
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
new ViewGroup.MarginLayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
)
);
layoutParams.setMargins(400, 600, 0, 0);
hand.setLayoutParams(layoutParams);
//making animation
TranslateAnimation animation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0, Animation.ABSOLUTE, -100,
Animation.RELATIVE_TO_SELF, 0, Animation.ABSOLUTE, -1000
);
animation.setInterpolator(new LinearInterpolator());
animation.setDuration(3000);
//applying animation
hand.startAnimation(animation);
}
}
并带有初始状态的屏幕截图:
问题是在动画图像的过程中像这个视频一样被裁剪:video with animation
我错过了很多时间,但一直未能解决问题 =(
可以做些什么来解决问题?
先感谢您!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
正如 ben75 所说的“......问题是初始位置。当你做一个 TranslateAnimation 时:图像不要移动:它用不同的翻译重绘”和“动画只是翻译这个第一个裁剪的图像。”
我是如何解决这个问题的:
布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/common_background">
<ImageView android:id="@+id/hand"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
android:src="@drawable/howto_throw_hand" />
</RelativeLayout>
活动逻辑:
public class MyActivity extends Activity
{
ImageView hand;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
hand = (ImageView)findViewById(R.id.hand);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
//initial positioning — removed
//making animation
TranslateAnimation animation = new TranslateAnimation(400, 300, 600, 0);
animation.setInterpolator(new LinearInterpolator());
animation.setDuration(3000);
//applying animation
hand.startAnimation(animation);
}
}
答对了!