IT/Android

안드로이드 갤러리로 커버플로우 효과 내기

UIMarvel 2012. 2. 16. 20:46

package lowmans.test;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
public class GalleryViewTest extends Activity implements AnimationListener{
 private MyGallery mGallery;
 Animation a;
 /** Called when the activity is first created. */

 


 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  
  mGallery = (MyGallery)findViewById(R.id.Gallery01);
  mGallery.setAdapter(new ImageAdapter(this));
  mGallery.setOnItemClickListener(new OnItemClickListener(){
   public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
     Animation animation = AnimationUtils.loadAnimation(GalleryViewTest.this, R.anim.anim);
     animation.setAnimationListener(GalleryViewTest.this);
     view.startAnimation(animation);
   }
  });
 }
 @Override
 public void onAnimationEnd(Animation animation) {
  Log.i("GalleryViewTest" , "onAnimationEnd");
 }
 @Override
 public void onAnimationRepeat(Animation animation) {}
 @Override
 public void onAnimationStart(Animation animation) {}
}
 
===========================================================================================================================
 
package lowmans.test;
import android.content.Context;
import android.graphics.Camera;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Transformation;
import android.widget.Gallery;

public class MyGallery extends Gallery{
 private final static String TAG = "MyGallery";
 private Context mContext;
 private static Camera mCamera;
 public MyGallery(Context context) {
   this(context, null);
 }
 public MyGallery(Context context, AttributeSet attrs) {
   this(context, attrs, 0);
 }
 public MyGallery(Context context, AttributeSet attrs, int defStyle) {
   super(context, attrs, defStyle);
   mContext = context;
   mCamera = new Camera();
   setSpacing(-30);  // child view 의 간격을 줄여 겹치는 듯한 효과를 준다
    }
  protected boolean getChildStaticTransformation(View child, Transformation t) {
  
  final int mCenter =(getWidth() - getPaddingLeft() - getPaddingRight()) / 2 + getPaddingLeft();
  final int childCenter = child.getLeft() + child.getWidth() / 2;
  final int childWidth = child.getWidth();
  
  t.clear();
  t.setTransformationType(Transformation.TYPE_MATRIX);
  float rate = Math.abs((float)(mCenter - childCenter)/ childWidth);
  
  mCamera.save();
  final Matrix matrix = t.getMatrix();
  float zoomAmount = (float) (rate * 200.0);
  mCamera.translate(0.0f, 0.0f, zoomAmount);        
  mCamera.getMatrix(matrix);    
  matrix.preTranslate(-(childWidth/2), -(childWidth/2));   
  matrix.postTranslate((childWidth/2), (childWidth/2));
  mCamera.restore();
  return true;
    }

}
 
===========================================================================================================================
package lowmans.test;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Gallery.LayoutParams;
 
public class ImageAdapter extends BaseAdapter {
    int mGalleryItemBackground;
    private Context mContext;
    private ImageView[] iv;
 
    private Integer[] mImageIds = {
         R.drawable.back_1 ,
         R.drawable.back_2 ,
         R.drawable.back_3 ,
         R.drawable.image ,
         R.drawable.back_1 ,
         R.drawable.back_2 ,
         R.drawable.back_3 ,
         R.drawable.image ,
        };
       
    private int cnt;
    public ImageAdapter(Context c) {
        mContext = c;
        cnt = mImageIds.length;
        iv = new ImageView[cnt];
      
      
        for(int i=0; i<cnt; i++){
          iv[i] = new ImageView(mContext);
          iv[i].setImageResource(mImageIds[i]);
          iv[i].setScaleType(ImageView.ScaleType.FIT_XY);
          iv[i].setLayoutParams(new Gallery.LayoutParams(200, 150));
        }
    }
    public int getCount() {
        return cnt;
    }
    public Object getItem(int position) {
        return position;
    }
    public long getItemId(int position) {
        return position;
    }
    public View getView(int position, View convertView, ViewGroup parent) {
         return iv[position];
    }
}