我的应用程序中有一个导航抽屉,带有以下控件:
1)ImageView和ProgressBar 2)ImageView和ProgressBar下面的ListView。
此列表中的每个元素都在Drawer上打开动画,我按照以下方式设置Listview元素的动画:
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.drawer_list_item, null);
}
ImageView imgIcon = (ImageView) convertView.findViewById(R.id.icon);
TextView txtTitle = (TextView) convertView.findViewById(R.id.title);
TextView txtCount = (TextView) convertView.findViewById(R.id.counter);
AnimatorSet sunSet = (AnimatorSet) AnimatorInflater.loadAnimator(MainActivity.con, R.animator.sun_swing);
AnimatorSet wheelSet = (AnimatorSet) AnimatorInflater.loadAnimator(MainActivity.con, R.animator.wheel_spin);
//set the view as target
sunSet.setTarget(imgIcon);
//start the animation
sunSet.start();
wheelSet.setTarget(imgIcon);
//start the animation
wheelSet.start();
ObjectAnimator textAnim2 = ObjectAnimator.ofFloat(txtTitle, "x",convertView.getWidth()-(txtTitle.getWidth()/2), (convertView.getWidth()/2)-70);
textAnim2.setDuration(1000);
textAnim2.setRepeatCount(0);
textAnim2.start();
txtTitle.setTypeface(tf);
txtTitle.setTextSize(18);
imgIcon.setImageResource(navDrawerItems.get(position).getIcon());
txtTitle.setText(navDrawerItems.get(position).getTitle());
//convertView.startAnimation(getMaximAnim());
if(navDrawerItems.get(position).getCounterVisibility()){
txtCount.setText(navDrawerItems.get(position).getCount());
}else{
// hide the counter view
txtCount.setVisibility(View.GONE);
}
return convertView;
}
这是我的wheel_spin:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:ordering="sequentially" >
<objectAnimator
android:duration="1000"
android:propertyName="rotation"
android:repeatCount="0"
android:repeatMode="reverse"
android:valueFrom="100"
android:valueTo="0"
android:valueType="floatType" />
</set>
这是我的sun_swing:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:ordering="sequentially" >
<objectAnimator
android:duration="1000"
android:propertyName="x"
android:repeatCount="0"
android:valueFrom="230"
android:valueTo="20"
android:valueType="floatType" />
</set>
在清单中:
<application
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/CustomActionBarTheme" >
onDrawerOpen中的
runOnUiThread(run);
我的跑步是:
final Runnable run = new Runnable(){
public void run(){
//reload content
adapter.notifyDataSetChanged();
mDrawerList.invalidateViews();
mDrawerList.refreshDrawableState();
}
};
我正在做以上操作,以便在打开导航抽屉时始终获得动画。我发现我的动画正在大多数手机上运行,但是4.0到4.4之间的一些手机正在运行这个带有抖动的动画。可能的原因是什么以及如何避免/克服相同的原因?
这是导航抽屉打开和关闭呼叫之间的跟踪:
public class NavDrawerListAdapter extends BaseAdapter {
private Context context;
private ArrayList<NavDrawerItem> navDrawerItems;
String fontPath = "fonts/HelveticaNeue-Light.otf";
Typeface tf;
ObjectAnimator textAnim2;
ObjectAnimator spin;
ObjectAnimator swing;
public NavDrawerListAdapter(Context context, ArrayList<NavDrawerItem> navDrawerItems){
this.context = context;
this.navDrawerItems = navDrawerItems;
tf = Typeface.createFromAsset(context.getAssets(), fontPath);
}
@Override
public int getCount() {
return navDrawerItems.size();
}
@Override
public Object getItem(int position) {
return navDrawerItems.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
System.out.println("Position: "+position);
ViewHolder viewHolder = new ViewHolder();
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.drawer_list_item, null);
viewHolder.imgIcon = (ImageView) convertView.findViewById(R.id.icon);
viewHolder.txtTitle = (TextView) convertView.findViewById(R.id.title);
viewHolder.txtCount = (TextView) convertView.findViewById(R.id.counter);
convertView.setTag(viewHolder);
}
viewHolder = (ViewHolder) convertView.getTag();
// if(position == 0){
textAnim2 = ObjectAnimator.ofFloat(viewHolder.txtTitle, "x",convertView.getWidth()-(viewHolder.txtTitle.getWidth()/2), ((viewHolder.imgIcon.getWidth())+30));
textAnim2.setDuration(1000);
textAnim2.setRepeatCount(0);
spin = ObjectAnimator.ofFloat(viewHolder.imgIcon, "rotation", 70f , 0f);
spin.setDuration(1000);
spin.setRepeatCount(0);
swing = ObjectAnimator.ofFloat(viewHolder.imgIcon, "x",convertView.getWidth()-(viewHolder.txtTitle.getWidth()+80), 30);
swing.setDuration(1000);
swing.setRepeatCount(0);
/* }else if(position == 1){
textAnim2 = ObjectAnimator.ofFloat(viewHolder.txtTitle, "x",convertView.getWidth()-(viewHolder.txtTitle.getWidth()/2), ((viewHolder.imgIcon.getWidth())+30));
textAnim2.setDuration(1000);
textAnim2.setRepeatCount(0);
spin = ObjectAnimator.ofFloat(viewHolder.imgIcon, "rotation", 70f , 0f);
spin.setDuration(1000);
spin.setRepeatCount(0);
swing = ObjectAnimator.ofFloat(viewHolder.imgIcon, "x",convertView.getWidth()-(viewHolder.txtTitle.getWidth()+120), 30);
swing.setDuration(1000);
swing.setRepeatCount(0);
}else if(position == 2){
textAnim2 = ObjectAnimator.ofFloat(viewHolder.txtTitle, "x",convertView.getWidth()-(viewHolder.txtTitle.getWidth()/2), ((viewHolder.imgIcon.getWidth())+30));
textAnim2.setDuration(1000);
textAnim2.setRepeatCount(0);
spin = ObjectAnimator.ofFloat(viewHolder.imgIcon, "rotation", 70f , 0f);
spin.setDuration(1000);
spin.setRepeatCount(0);
swing = ObjectAnimator.ofFloat(viewHolder.imgIcon, "x",convertView.getWidth()-(viewHolder.txtTitle.getWidth()+140), 30);
swing.setDuration(1000);
swing.setRepeatCount(0);
} */
swing.start();
textAnim2.start();
spin.start();
viewHolder.txtTitle.setTypeface(tf);
viewHolder.txtTitle.setTextSize(18);
viewHolder.imgIcon.setImageResource(navDrawerItems.get(position).getIcon());
viewHolder.txtTitle.setText(navDrawerItems.get(position).getTitle());
if(navDrawerItems.get(position).getCounterVisibility()){
viewHolder.txtCount.setText(navDrawerItems.get(position).getCount());
}else{
viewHolder.txtCount.setVisibility(View.GONE);
}
return convertView;
}
static class ViewHolder {
ImageView imgIcon;
TextView txtTitle;
TextView txtCount;
}
答案 0 :(得分:1)
首先,findViewById很昂贵,这就是为什么使用ViewHolder模式总是一个好主意。
其次,您在每次getView()调用时都会进行2 xml动画曝光。 XML通胀甚至是更昂贵的操作。
考虑重写你的getView方法。将Animator膨胀移动到适配器构造函数并在getView()中重用它们还添加了一个viewHolder来改善列表滚动性能(如果你有一个很长的菜单列表,这很有用):
private Activity ctx;
private AnimatorSet sunSet;
private AnimatorSet wheelSet;
public MyAdapter(Activity ctx){
this.ctx = ctx;
this.sunSet = (AnimatorSet) AnimatorInflater.loadAnimator(ctx, R.animator.sun_swing);
this.wheelSet = (AnimatorSet) AnimatorInflater.loadAnimator(ctx, R.animator.wheel_spin);
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = new ViewHolder();
if (convertView == null) {
convertView = ctx.getLayoutInflater().inflate(R.layout.drawer_list_item, parent, false);
viewHolder.imgIcon = (ImageView) convertView.findViewById(R.id.icon);
viewHolder.txtTitle = (TextView) convertView.findViewById(R.id.title);
viewHolder.txtCount = (TextView) convertView.findViewById(R.id.counter);
convertView.setTag(viewHolder);
}
viewHolder = (ViewHolderItem) convertView.getTag();
AnimatorSet sunSetClone = sunSet.clone();
sunSetClone.setTarget(viewHolder.imgIcon);
sunSetClone.start();
AnimatorSet wheelSetClone = wheelSet.clone();
wheelSetClone.setTarget(viewHolder.imgIcon);
wheelSetClone.start();
ObjectAnimator textAnim2 = ObjectAnimator.ofFloat(viewHolder.txtTitle, "x",convertView.getWidth()-(viewHolder.txtTitle.getWidth()/2), (convertView.getWidth()/2)-70);
textAnim2.setDuration(1000);
textAnim2.setRepeatCount(0);
textAnim2.start();
viewHolder.txtTitle.setTypeface(tf);
viewHolder.txtTitle.setTextSize(18);
viewHolder.imgIcon.setImageResource(navDrawerItems.get(position).getIcon());
viewHolder.txtTitle.setText(navDrawerItems.get(position).getTitle());
if(navDrawerItems.get(position).getCounterVisibility()){
viewHolder.txtCount.setText(navDrawerItems.get(position).getCount());
}else{
viewHolder.txtCount.setVisibility(View.GONE);
}
return convertView;
}
static class ViewHolder {
ImageView imgIcon;
TextView txtTitle;
TextView txtCount;
}
您可以添加的其他一项性能改进是使用imageView.setImageResource(navDrawerItems.get(position).getIcon());
正如Android documentation所述:
setImageResource:这会在UI上读取和解码 线程,这可能会导致延迟打嗝。如果这是一个问题, 考虑使用setImageDrawable(android.graphics.drawable.Drawable)或 改为使用setImageBitmap(android.graphics.Bitmap)和BitmapFactory。
Personaly我喜欢使用AsyncDrawable
编辑:刚刚意识到它只会为一个列表项设置动画,因为我们总是覆盖目标视图。幸运的是,AnimatorSet将clone()实现为“深层复制”,即使您克隆正在运行的动画,所有值也会重置,因此我更新了代码而不是:
sunSet.reset();
sunSet.setTarget(viewHolder.imgIcon);
sunSet.start();
wheelSet.reset();
wheelSet.setTarget(viewHolder.imgIcon);
wheelSet.start();
是这样的:
AnimatorSet sunSetClone = sunSet.clone();
sunSetClone.setTarget(viewHolder.imgIcon);
sunSetClone.start();
AnimatorSet wheelSetClone = wheelSet.clone();
wheelSetClone.setTarget(viewHolder.imgIcon);
wheelSetClone.start();
克隆仍然比XML通胀更快。