我想實現一個放大中心圖像的圖庫,比如內置圖庫。在onItemSelected
聽衆,我加載一個縮放動畫,使中心圖像更大。這裏是我的代碼:動畫在android 4.x上無法正常工作
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.所有圖像項目具有相同的大小。這裏有什麼錯誤嗎? 請幫我解決這個問題。
謝謝。
我試過你的解決方案,但它不能正常工作。圖像放大,但其大小不變。它在邊界內縮放。 –