在listView中为图像创建淡入动画

时间:2013-11-05 12:45:52

标签: android listview android-animation

我目前正在尝试在列表视图中设置我的图像,以便在加载时加载动画淡入淡出。

我尝试过this this method here但是当我加载ListView并且错误“java.lang.RuntimeException:未知的动画名称:animator”时,我的应用程序崩溃了 “

如果有人可以给我一些关于淡入我的列表视图图像的指导,那就太棒了。

类别:

public class Fragment2 extends ListFragment {

public class MyListAdapter extends ArrayAdapter<String> {

    Context myContext;

    public MyListAdapter(Context context, int textViewResourceId,
            String[] objects) {
        super(context, textViewResourceId, objects);
        myContext = context;
    }



    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // return super.getView(position, convertView, parent);

        LayoutInflater inflater = (LayoutInflater) myContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.row, parent, false);
        TextView label = (TextView) row.findViewById(R.id.month);
        label.setText(month[position]);
        ImageView icon = (ImageView) row.findViewById(R.id.icon);

        Animation fadeInAnimation = AnimationUtils.loadAnimation(getContext(), R.animator.animation);
        //Now Set your animation
        icon.startAnimation(fadeInAnimation );

        // Customize your icon here
        icon.setImageResource(drawables[position]);



        return row;
    }

}

int[] drawables = { R.drawable.transparent_picture, R.drawable.abstract_icon,
        R.drawable.animal_icon, R.drawable.anime_icon,
        R.drawable.artistic_icon, R.drawable.cartoon_icon,
        R.drawable.comic_icon, R.drawable.icon_celeberity,
        R.drawable.icon_cars, R.drawable.icon_fantasy,
        R.drawable.icon_food,

};

String[] month = { " ", "Abstract", "Animal", "Anime", "Artistic",
        "Cartoon", "Comics", "Celeberity", "Cars", "Fantasy", "Food" };

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    MyListAdapter myListAdapter = new MyListAdapter(getActivity(),
            R.layout.row, month);
    setListAdapter(myListAdapter);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragments_layout2, container, false);
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    Toast.makeText(getActivity(),
            getListView().getItemAtPosition(position).toString(),
            Toast.LENGTH_LONG).show();
}

}

动画XML:

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

<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <alpha
        android:duration="200"
        android:fromAlpha="0.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:repeatCount="infinite"
        android:toAlpha="1.0" />
</set>

</animator>

1 个答案:

答案 0 :(得分:0)

问题是alpha动画元素不是animator的一部分,是animation元素的一部分,要将res动画加载到Android代码,必须使用{{1 }}类和Animator动画,你必须使用res类。有关详细信息,请参阅Android Animation resources