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