0

我正在尝试在单击时在 ImageView 中制作帧动画。但是 ImageView 的图像重叠了这个动画。可能是什么问题?和setimagerresource(0)不起作用。这是代码:

ImageView iv = (ImageView) v.findViewById(R.id.imageView1);
iv.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        v.setBackgroundResource(R.drawable.reloadanim);
        AnimationDrawable frameAnimation = (AnimationDrawable) v.getBackground();
        frameAnimation.start();

    }
});

布局:

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

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView2"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:paddingLeft="5dp"
        android:paddingTop="5dp"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:paddingLeft="5dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView3"
        android:layout_alignParentRight="true"
        android:layout_marginRight="25dp"
        android:src="@drawable/reload"/>

</RelativeLayout>

没有这条线android:src="@drawable/reload"动画根本看不到

动画:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/spin_refresh"
    android:oneshot="false" >

    <item
        android:drawable="@drawable/reload1"
        android:duration="400"/>
    <item
        android:drawable="@drawable/reload2"
        android:duration="400"/>
    <item
        android:drawable="@drawable/reload3"
        android:duration="400"/>
    <item
        android:drawable="@drawable/reload"
        android:duration="400"/>

</animation-list>
4

1 回答 1

0

用我的代码编辑你的代码。动画将添加到 ImageView 中,然后在您触摸屏幕时进行动画处理。

MainActivity.java

AnimationDrawable frameAnimation;

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  ImageView iv = (ImageView) v.findViewById(R.id.imageView1);
  iv.setBackgroundResource(R.drawable.reload);
  frameAnimation = (AnimationDrawable) iv.getBackground();
}

public boolean onTouchEvent(MotionEvent event) {
  if (event.getAction() == MotionEvent.ACTION_DOWN) {
    frameAnimation.start();
    return true;
  }
  return super.onTouchEvent(event);
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView2"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:text="TextView" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView1"
    android:paddingLeft="5dp"
    android:paddingTop="5dp"
    android:text="TextView" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:paddingLeft="5dp"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView3"
    android:layout_alignParentRight="true"
    android:layout_marginRight="25dp"/>

</RelativeLayout>

重载.xml

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false" >
<item
    android:drawable="@drawable/reload1"
    android:duration="400"/>
<item
    android:drawable="@drawable/reload2"
    android:duration="400"/>
<item
    android:drawable="@drawable/reload3"
    android:duration="400"/>
<item
    android:drawable="@drawable/reload"
    android:duration="400"/>
</animation-list>
于 2013-12-17T09:04:19.883 回答