开始一个简单的动画

时间:2012-07-06 15:40:49

标签: android animation

作为更复杂的功能的一部分,我已经陷入了在屏幕的某个位置显示圆圈变得不透明的能力。

我已经创建了一个测试项目,我的动画代码如下:

View mLongPressImage;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mLongPressImage = findViewById(R.id.onPressImage);

     findViewById(R.id.button).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                startAnimation(v.getX(), v.getY());
            }
        });
}

private AlphaAnimation onPressAnimation;

private void startAnimation(final float f, final float g) {
    Log.v("", "START");
    mLongPressImage.setX(f);
    mLongPressImage.setY(g);
    onPressAnimation = new AlphaAnimation(0f, 1f);
    onPressAnimation.setDuration(1000);
    onPressAnimation.setFillAfter(true);
    onPressAnimation.setFillEnabled(true);
    mLongPressImage.startAnimation(onPressAnimation);
}

记录显示正在发生的正确事件。

查看如下:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="PRESS ME" />

    <ImageView
        android:id="@+id/onPressImage"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="#000000"
        android:scaleType="fitXY"
        android:src="@drawable/shape_circle_onpress" />

</FrameLayout>

现在,图像只在1秒钟后出现在坐标处。

有人能指出这个非常简单的功能中隐藏的缺陷吗?

2 个答案:

答案 0 :(得分:1)

上面的代码在技术上似乎是正确的,这就是为什么我在解决它时遇到如此大的问题。

问题是由于我一起使用setX()setY()以及Animation造成的 - 在我的Xoom平板电脑(运行3.2 API 13)上,即使切换到{后,这也会导致一些非常时髦的行为{1}}。

删除ScaleAnimationsetX()会消除毛刺行为,就像切换到Galaxy Nexus(运行API 4.0冰淇淋三明治)一样。将setY()设置为hardwareAccelerated而不是删除truesetX()也改善了问题(但没有完全解决)。

如果有人知道为什么,那么我会非常乐意在这一方面进行投票并更改已接受的答案。在那之前,我将把它归结为一个简单的Android API。

答案 1 :(得分:0)

如果您对圈子周围的黑色背景不满意,可以将ImageView背景更改为以下(注意两个附加零来描述Alpha通道值):

android:background="#00000000"