Android框架动画无效

时间:2013-10-12 15:00:39

标签: android animation frame animationdrawable

我正在尝试使用片段中的AnimationDrawables在Android中创建动画。每当我尝试运行它时,它只会填满整个图像视图,我无法判断它是否是动画。

显然,我一定是做错了,但我似乎无法找到原因。有没有人有什么建议?

我的代码片段如下所示:

public class RelationshipZoom extends Fragment implements View.OnClickListener {

private Heart mHeart = new Heart(getActivity());

@Override
public void onActivityCreated (Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    final View v = inflater.inflate(R.layout.relationshipzoom, container, false);
    final ImageView test = (ImageView) v.findViewById(R.id.test_Animation);

    Button button = (Button)v.findViewById(R.id.showchange_button);
    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {

            AnimationDrawable HeartAnimation= new AnimationDrawable();

            HeartAnimation.addFrame(getResources().getDrawable(R.drawable.smsheart50), 5);
            HeartAnimation.addFrame(getResources().getDrawable(R.drawable.smsheart55), 5);
            HeartAnimation.addFrame(getResources().getDrawable(R.drawable.smsheart60), 5);
            HeartAnimation.addFrame(getResources().getDrawable(R.drawable.smsheart65), 5);
            HeartAnimation.addFrame(getResources().getDrawable(R.drawable.smsheart70), 5);
            HeartAnimation.addFrame(getResources().getDrawable(R.drawable.smsheart75), 5);
            HeartAnimation.addFrame(getResources().getDrawable(R.drawable.smsheart80), 5);
            HeartAnimation.addFrame(getResources().getDrawable(R.drawable.smsheart85), 5);

            HeartAnimation.setOneShot(true);
            test.setBackground(HeartAnimation);
            HeartAnimation.start();

        }
    });

我的XML看起来像这样:

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

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Show change"
    android:id="@+id/showchange_button"
    android:layout_below="@+id/start_date"
    android:layout_alignParentLeft="true"
    android:layout_alignRight="@+id/end_date" />

<ImageView
    android:layout_width="200px"
    android:layout_height="200px"
    android:id="@+id/test_Animation"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="29dp" />
   </RelativeLayout>

0 个答案:

没有答案