我的ActionBarSherlock操作栏中有一个菜单选项,用于启动AsyncTask。我正在尝试在后台任务运行时让图标变为动画。我有它工作,除非我点击图标,图标本身将“跳”几个像素到右边,这是非常明显的。不完全确定导致这种情况发生的原因。
这是我到目前为止所做的:
private MenuItem refreshItem;
private void DoRefresh()
{
final LayoutInflater inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final ImageView ivRefresh = (ImageView)inflater.inflate(R.layout.refresh_view, null);
final Animation rotation = AnimationUtils.loadAnimation(activity, R.anim.refresh);
ImageView ivRefresh.startAnimation(rotation);
refreshItem.setActionView(ivRefresh);
//AsyncTask is kicked off here
}
@Override
public boolean onOptionsItemSelected(final MenuItem item)
{
if (item.getItemId() == R.id.refresh) {
refreshItem = item;
this.DoRefresh();
return true;
} else {
return super.onOptionsItemSelected(item);
}
}
refresh_view.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/refresh"
/>
refresh.xml
<rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:interpolator="@android:anim/linear_interpolator"
android:duration="1100"
/>
更新:
一直在胡闹,没有运气。它与动画无关,因为如果我禁用动画,图标仍会跳到右侧。如果我注释掉setActionView
,图标就不会跳转,所以它与此有关。毫无头绪,让我疯了。
答案 0 :(得分:0)
我不知道这是否会产生任何影响,但如果您使用pivot
值,则可能会解决此问题。也许只能在一个轴上旋转?
答案 1 :(得分:0)
Alex Fu,你的文章非常有帮助,但是可以做一些改进。
每次在onAnimationEnd中重新启动动画都是低效的。相反,在启动之前将动画repeatcount设置为无限,然后当动作完成时,将repeatcount设置为0.动画将完成当前循环,然后调用onAnimationEnd,您可以在其中调用setActionView(null)。
我还遇到了一个问题(至少在模拟器中,没有在真实设备上测试,因为我没有测试),同时包含ActionBarSherlock和原生ICS ActionBar,即最后几帧调用setActionView(null)后,动画与静态按钮重叠。我的解决方案是将在LinearLayout中设置动画的ImageView包装起来,然后在LinearLayout上调用setVisibility(View.GONE)。如果为视图设置动画,则使用setVisibility隐藏它将无效,但隐藏其父视图。
以下是所有相关代码:
layout / actionbar_progress.xml(注意我添加了id,尽管只有ImageView需要一个):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/actionbar_progress_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/actionbar_progress_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:src="@drawable/ic_action_refresh"
style="@style/Widget.Sherlock.ActionButton"
/>
</LinearLayout>
动画/ refresh_rotate.xml:
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="1000"
android:interpolator="@android:anim/linear_interpolator"
android:repeatCount="infinite">
</rotate>
repeatCount设置为无限,但这不是必需的,因为java代码必须将其设置为无限。
在onCreateOptionsMenu中我有
...
menuInflater.inflate(R.menu.main, menu);
mRefreshItem = menu.findItem(R.id.menu_refresh);
...
这只是为了避免每次单击刷新按钮时都执行findItem。
在onOptionsItemSelected:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
...
case R.id.menu_refresh:
refresh();
break;
...
}
}
刷新方法只是一个虚拟的5秒延迟帖子;它调用refreshAnim来启动动画,然后调用refreshAnimEnd来完成停止动画:
private void refresh() {
refreshAnim();
getWindow().getDecorView().postDelayed(
new Runnable() {
@Override
public void run() {
refreshAnimEnd();
}
}, 5000);
}
以下是最重要的部分,并附有评论:
private void refreshAnim() {
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//The inflated layout and loaded animation are put into members to avoid reloading them every time.
//For convenience, the ImageView is also extracted into a member
if (mRefreshIndeterminateProgressView == null || mRefreshRotateClockwise == null) {
mRefreshIndeterminateProgressView = inflater.inflate(R.layout.actionbar_progress, null);
mRefreshRotateClockwise = AnimationUtils.loadAnimation(this, R.anim.refresh_rotate);
mRefreshImage = mRefreshIndeterminateProgressView.findViewById(R.id.actionbar_progress_image);
}
//reset some stuff - make the animation infinite again,
//and make the containing view visible
mRefreshRotateClockwise.setRepeatCount(Animation.INFINITE);
mRefreshIndeterminateProgressView.setVisibility(View.VISIBLE);
mRefreshRotateClockwise.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {}
@Override
public void onAnimationRepeat(Animation animation) {}
@Override
public void onAnimationEnd(Animation animation) {
//This is important to avoid the overlapping problem.
//First hide the animated icon
//setActionView(null) does NOT hide it!
//as long as the animation is running, it will be visible
//hiding an animated view also doesn't work
//as long as the animation is running
//but hiding the parent does.
mRefreshIndeterminateProgressView.setVisibility(View.GONE);
//make the static button appear again
mRefreshItem.setActionView(null);
}
});
mRefreshItem.setActionView(mRefreshIndeterminateProgressView);
//everything is set up, start animating.
mRefreshImage.startAnimation(mRefreshRotateClockwise);
}
private void refreshAnimEnd() {
//sanity
if ( mRefreshImage == null || mRefreshItem == null ) {
return;
}
Animation anim = mRefreshImage.getAnimation();
//more sanity
if (anim != null) {
//let the animation finish nicely
anim.setRepeatCount(0);
} else {
//onAnimationEnd won't run in this case, so restore the static button
mRefreshItem.setActionView(null);
}
}
答案 2 :(得分:0)
如果您使用的是普通的ActionBar,只需将其添加到 img_layout.xml :
style="?android:attr/actionButtonStyle"
或者使用SherLock ActionBar:
style="@style/Widget.Sherlock.ActionButton"
答案 3 :(得分:-1)
如果菜单选项是操作项,则应将样式Widget.Sherlock.ActionButton应用于ImageView以保持一致性。阅读这篇文章,我写了如何正确地动作动作项目。 http://alexfu.tumblr.com/post/19414506327/android-dev-animate-action-items