android 动画阻塞,android – 当自定义ImageView调用startAnimation(Animation)时,为什么getActivity()在JUnit测试期间阻塞?...

我写了一个Android应用程序,它显示了一个自定义ImageView,它使用startAnimation(动画)定期自行旋转.该应用程序工作正常,但如果我创建一个类型为ActivityInstrumentationTestCase2的JUnit测试并且测试调用getActivity(),则对该getActivity()的调用永远不会返回,直到应用程序进入后台(例如,按下设备的主页按钮).

经过很长时间和挫折之后,我发现如果我在自定义ImageView类中注释掉对startAnimation(Animation)的调用,getActivity()会立即返回.但这会破坏我的自定义ImageView的目的,因为我需要为它设置动画.

任何人都可以告诉我为什么getActivity()在我的JUnit测试期间阻塞但仅在使用startAnimation时?提前感谢能够提出解决方法或告诉我我做错了什么的人.

注意:解决方案需要至少使用Android API级别10.

以下是运行它所需的所有源代码(将任何PNG图像放在res / drawable中并将其命名为the_image.png):

activity_main.xml中:

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context=".MainActivity" >

android:id="@+id/rotatingImageView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@drawable/the_image" />

MainActivity.java:

package com.example.rotatingimageviewapp;

import android.app.Activity;

import android.os.Bundle;

import android.util.Log;

public class MainActivity extends Activity {

private RotatingImageView rotatingImageView = null;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

rotatingImageView = (RotatingImageView) findViewById(

R.id.rotatingImageView);

rotatingImageView.startRotation();

}

@Override

protected void onPause() {

super.onPause();

rotatingImageView.stopRotation();

}

@Override

protected void onResume() {

super.onResume();

rotatingImageView.startRotation();

}

}

RotatingImageView.java(自定义ImageView):

package com.example.rotatingimageviewapp;

import java.util.Timer;

import java.util.TimerTask;

import android.content.Context;

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.util.AttributeSet;

import android.view.animation.Animation;

import android.view.animation.RotateAnimation;

import android.widget.ImageView;

public class RotatingImageView extends ImageView {

private static final long ANIMATION_PERIOD_MS = 1000 / 24;

//The Handler that does the rotation animation

private final Handler handler = new Handler() {

private float currentAngle = 0f;

private final Object animLock = new Object();

private RotateAnimation anim = null;

@Override

public void handleMessage(Message msg) {

float nextAngle = 360 - msg.getData().getFloat("rotation");

synchronized (animLock) {

anim = new RotateAnimation(

currentAngle,

nextAngle,

Animation.RELATIVE_TO_SELF,

.5f,

Animation.RELATIVE_TO_SELF,

.5f);

anim.setDuration(ANIMATION_PERIOD_MS);

/**

* Commenting out the following line allows getActivity() to

* return immediately!

*/

startAnimation(anim);

}

currentAngle = nextAngle;

}

};

private float rotation = 0f;

private final Timer timer = new Timer(true);

private TimerTask timerTask = null;

public RotatingImageView(Context context) {

super(context);

}

public RotatingImageView(Context context, AttributeSet attrs) {

super(context, attrs);

}

public RotatingImageView(Context context, AttributeSet attrs,

int defStyle) {

super(context, attrs, defStyle);

}

public void startRotation() {

stopRotation();

/**

* Set up the task that calculates the rotation value

* and tells the Handler to do the rotation

*/

timerTask = new TimerTask() {

@Override

public void run() {

//Calculate next rotation value

rotation += 15f;

while (rotation >= 360f) {

rotation -= 360f;

}

//Tell the Handler to do the rotation

Bundle bundle = new Bundle();

bundle.putFloat("rotation", rotation);

Message msg = new Message();

msg.setData(bundle);

handler.sendMessage(msg);

}

};

timer.schedule(timerTask, 0, ANIMATION_PERIOD_MS);

}

public void stopRotation() {

if (null != timerTask) {

timerTask.cancel();

}

}

}

MainActivityTest.java:

package com.example.rotatingimageviewapp.test;

import android.app.Activity;

import android.test.ActivityInstrumentationTestCase2;

import com.example.rotatingimageviewapp.MainActivity;

public class MainActivityTest extends

ActivityInstrumentationTestCase2 {

public MainActivityTest() {

super(MainActivity.class);

}

protected void setUp() throws Exception {

super.setUp();

}

protected void tearDown() throws Exception {

super.tearDown();

}

public void test001() {

assertEquals(1 + 2, 3 + 0);

}

public void test002() {

//Test hangs on the following line until app goes to background

Activity activity = getActivity();

assertNotNull(activity);

}

public void test003() {

assertEquals(1 + 2, 3 + 0);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值