0

我想实现一个像内置画廊一样放大中心图像的画廊。在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 上不起作用。所有图像项具有相同的大小。这里有什么错误吗?请帮我解决这个问题。

谢谢。

4

2 回答 2

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项目所在项目的动画时,我遇到了几乎相同的问题ImageView,这个技巧在 Android 2+ 和 4+ 上都适用于我。只需获取您要使用的视图实例并播放动画,因为我猜您的情况应该类似于

ImageView image = (ImageView) view.findViewById(R.id.view_joinedPrograms_imageview_thumbnail); 

并将其image用于动画。

希望这对你有用!:)

于 2013-10-21T10:08:39.810 回答
0

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

于 2013-10-21T10:32:12.933 回答