动画在android 4.x上不起作用

时间:2013-10-21 09:57:32

标签: android android-animation

我想实现一个放大中心图像的图库,就像内置图库一样。在onItemSelected监听器上,我加载了一个缩放动画,使中心图像更大。这是我的代码:

joinedProgramImages = new int[] { R.drawable.temp_lotteria_logo, R.drawable.temp_starbucks_logo, R.drawable.temp_cocacola_logo,R.drawable.temp_lotteria_logo, R.drawable.temp_starbucks_logo };
        JoinedProgramsAdapter adapter = new JoinedProgramsAdapter();
        galleryJoinedPrograms.setAdapter(adapter);
        galleryJoinedPrograms.setSpacing(-20);
        galleryJoinedPrograms.setSelection(1);
        galleryJoinedPrograms.setHorizontalFadingEdgeEnabled(true);
        galleryJoinedPrograms.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> viewgroup, View view, int position, long arg3) {
            //zoom out previous center image.
            if (selectedProgram != null) {
                Animation animation = (Animation) AnimationUtils.loadAnimation(getApplicationContext(), R.animator.statelist_zoomout_gallery_image);
                selectedProgram.startAnimation(animation);
                animation.startNow();
            }
            //zoom in the center image.
            Animation animation = (Animation) AnimationUtils.loadAnimation(getApplicationContext(), R.animator.statelist_zoomin_gallery_image);
            view.startAnimation(animation);
            selectedProgram = view;
            animation.startNow();
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {

        }
    });

画廊的适配器:

private class JoinedProgramsAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return joinedProgramImages.length;
        }

        @Override
        public Object getItem(int position) {
            return joinedProgramImages[position];
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.view_joinedprogram_item, null);
            }
            ImageView img = (ImageView) convertView.findViewById(R.id.view_joinedPrograms_imageview_thumbnail);
            img.setImageResource(joinedProgramImages[position]);
            return convertView;
        }

    }

图库图片项目:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >


    <ImageView
        android:id="@+id/view_joinedPrograms_imageview_thumbnail"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/temp_lotteria_logo" />


</LinearLayout> 

放大动画:

<?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false"
    android:fillAfter="true"
>
<scale 
       android:fromXScale="1.0"
       android:toXScale="3.50"
       android:fromYScale="1.0"
       android:toYScale="1.50"
       android:duration="200"
       android:pivotX="50%p"
       android:pivotY="50%p"
       android:fillAfter="true"/>

</set>

和缩小动画:

<?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false"
    android:fillAfter="true"
>
<scale 
       android:fromXScale="1.5"
       android:toXScale="1.0"
       android:fromYScale="1.5"
       android:toYScale="1.0"
       android:duration="200"
       android:pivotX="50%p"
       android:pivotY="50%p"
       android:fillAfter="true"/>

</set>

在Android 2.x上一切正常但它在android 4.x上不起作用。所有图像项目具有相同的大小。这里有什么错吗? 请帮我解决这个问题。

感谢。

2 个答案:

答案 0 :(得分:2)

尝试使用类似的东西:

@Override
    public void onItemSelected(AdapterView<?> viewgroup, View view, int position, long arg3) {
        //zoom out previous center image.
        if (selectedProgram != null) {
            Animation animation = (Animation) AnimationUtils.loadAnimation(getApplicationContext(), R.animator.statelist_zoomout_gallery_image);
            selectedProgram.startAnimation(animation);
            animation.startNow();
        }
        //zoom in the center image.
        Animation animation = (Animation) AnimationUtils.loadAnimation(getApplicationContext(), R.animator.statelist_zoomin_gallery_image);
        ImageView image = (ImageView) view.findViewById(R.id.view_joinedPrograms_imageview_thumbnail);
        image.startAnimation(animation);
        //animation.startNow();
    }

我尝试设置项目为GridView的{​​{1}}项动画时遇到了几乎相同的问题,这个技巧在Android 2+和4+上都适合我。只需获取您要使用的视图实例并播放动画,因为我猜你的情况应该像

ImageView

并将此ImageView image = (ImageView) view.findViewById(R.id.view_joinedPrograms_imageview_thumbnail); 用于动画。

希望这适合你! :)

答案 1 :(得分:0)

你不应该这样做,更简单的方法是覆盖ViewGroup.getChildStaticTransformation方法,这样你可以完全控制项目,甚至更好的ViewGroup.drawChild但后者使用起来要困难得多