2013-10-21 50 views
0

我想實現一個放大中心圖像的圖庫,比如內置圖庫。在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.所有圖像項目具有相同的大小。這裏有什麼錯誤嗎? 請幫我解決這個問題。

謝謝。

回答

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 S和這一招爲我工作的同時在Android動畫2+和4+。剛剛獲得您想要使用和播放動畫,我在您的情況猜測應該是這樣

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

,並使用此image動畫的視圖的一個實例。

希望這適合你! :)

+0

我試過你的解決方案,但它不能正常工作。圖像放大,但其大小不變。它在邊界內縮放。 –

0

你不應該這樣做,更簡單的方法是重寫ViewGroup.getChildStaticTransformation方法,這樣你就可以完全控制該項目,或者甚至更好的ViewGroup.drawChild,但後者更難以使用