动画列表视图行,这是什么适当的地方?

时间:2014-11-28 13:58:34

标签: android android-listview android-animation android-navigation

我正在尝试为Nav抽屉的行设置动画:

我希望图标旋转30度,我希望整行从左侧滑入。

因此,当行滑动时,图像应旋转30度并滑动。

我有以下方法来执行此操作:

@Override
    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);


        animTranslate = AnimationUtils.loadAnimation(MainActivity.con,
                R.anim.rotate_nav_drawer);
        txtTitle.setTypeface(tf); 
        txtTitle.setTextSize(18);

        Animation rotation = AnimationUtils.loadAnimation(MainActivity.con, R.anim.rotatenav);

        imgIcon.setImageResource(navDrawerItems.get(position).getIcon());    

        imgIcon.startAnimation(rotation);
        imgIcon.startAnimation(animTranslate);
        txtTitle.setAnimation(animTranslate);

        txtTitle.setText(navDrawerItems.get(position).getTitle());

        // displaying count
        // check whether it set visible or not
        if(navDrawerItems.get(position).getCounterVisibility()){
            txtCount.setText(navDrawerItems.get(position).getCount());
        }else{
            // hide the counter view
            txtCount.setVisibility(View.GONE);
        }

        return convertView;
    } 

问题:

图像不会旋转但会滑入。 动画只发生一次,即导航抽屉第一次打开时。

我在这里想念什么?

以下是我的动画xml:

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

    <translate
        android:duration="900"
        android:fromXDelta="30%"
        android:toXDelta="0%" >
    </translate>

</set>

<?xml version="1.0" encoding="UTF-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="600"
    android:fromDegrees="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="infinite"
    android:toDegrees="30" />

虽然我可以在navDrawerOpen上调用这些动画,但我对如何在那里引用各个视图毫无头绪。

编辑:

这是我的完整适配器:

public class NavDrawerListAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<NavDrawerItem> navDrawerItems;
    String fontPath = "fonts/HelveticaNeue-Light.otf";
    Typeface tf; 


    Animation animTranslate;

    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) {
        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);


        animTranslate = AnimationUtils.loadAnimation(MainActivity.con,
                R.anim.rotate_nav_drawer);
        txtTitle.setTypeface(tf); 
        txtTitle.setTextSize(18);

        Animation rotation = AnimationUtils.loadAnimation(MainActivity.con, R.anim.rotatenav);

        imgIcon.setImageResource(navDrawerItems.get(position).getIcon());    

    //  imgIcon.startAnimation(rotation);
    //  imgIcon.startAnimation(animTranslate);
    //  txtTitle.setAnimation(animTranslate);

        txtTitle.setText(navDrawerItems.get(position).getTitle());

        // displaying count
        // check whether it set visible or not
        if(navDrawerItems.get(position).getCounterVisibility()){
            txtCount.setText(navDrawerItems.get(position).getCount());
        }else{
            // hide the counter view
            txtCount.setVisibility(View.GONE);
        }

        return convertView;
    }

}

2 个答案:

答案 0 :(得分:2)

使用插值器并对xml进行一些更改。 另外,如Bruce所述,将translateAnimation添加到rotate_nav_drawer_image.xml xml:

<set xmlns:android="http://schemas.android.com/apk/res/android"
 <!--- android:interpolator="@android:anim/linear_interpolator"
       If you add this intepolator here remove it from translate and rotate-->
 android:fillAfter="true"
 >
<translate
    android:duration="900"
    android:fromXDelta="30%"
    android:toXDelta="0%"
    android:interpolator="@android:anim/linear_interpolator"
      />
<rotate
    android:duration="600"
    android:fromDegrees="0"
    android:toDegrees="30"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatMode="reverse" <!-- "reverse" to have the animation reverse direction with each iteration or "repeat" to have the animation loop from the beginning each time. -->
    android:repeatCount="-1"  <!-- can also use "infinite" -->
    android:interpolator="@android:anim/cycle_interpolator"    <!--You can exeriment with linear_interpolator... -->
          />

将其添加到您的imageView

animTranslateRotImage = AnimationUtils.loadAnimation(myContext,
            R.anim.rotate_nav_drawer_image);
imgIcon.startAnimation(animTranslateRotImage);

如果动画仅在第一次显示导航抽屉时打开。在抽屉listView上调用invalidateViews()以强制重绘它。像

mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerList.setAdater( //.....
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) {


        /** Called when a drawer has settled in a completely open state. */
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            getActionBar().setTitle(mDrawerTitle);
            // Force it to be redrawn.
            mDrawerList.invalidateViews();

            /* If this did not work call
               use mDrawerList.refreshDrawableState() or
               mDrawerAdapter.notifyDataSetChanged();
            */


            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }
    };

答案 1 :(得分:1)

不确定这是否有效,但是txtTitle上的动画工作正常,但不是imgIcon上的动画,也许你可以让imgIcon的代码更像txtTitle。将图标的XML更改为组合两者的集合:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <translate
        android:duration="900"
        android:fromXDelta="30%"
        android:toXDelta="0%" />
    <rotate
        android:duration="600"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="infinite"
        android:toDegrees="30" />
</set>

然后像使用标题一样在图标上设置动画:

imgIcon.setAnimation(rotation);
txtTitle.setAnimation(animTranslate);