출처 : http://blog.naver.com/lowmans?Redirect=Log&logNo=100115258620

참고 : 
http://www.inter-fuser.com/2010/01/android-coverflow-widget.html
 
Android Gallery를 이뿌게~? 작업하기 위해서 구글 할부지께 물어본 결과 위에 링크한 CoverFlow를 알게 되었다
Gallery를 커스터 마이징한 소스인데 여간 잔망스러운게 아니여서.. 나름 깔끔하게 다듬어 본 소스를 공개한다.
 
불필요한 작업을 피하기 위해 바로 Gallery를 상속 받고
protected boolean getChildStaticTransformation(View child, Transformation t)
를 override 하여 작업하면 된다
 
getChildStaticTransformation 는 List에 연결된(gallery view 안에서 스크롤 할때 ) child가 어디에 위치 했는지 알고 싶을때 사용하는 method이다
 
metrix 는 child 의 bitmap 정보를  camera는  원근 효과를 주는 클래스들인데 이를  사용하여 gallery가 스크롤시 원근 효과를 주어 마치 스페이스 공간에서 움직이는 듯한 effect를 주었다 ..
 
view 를 click 할경우 animation 효과를 주어 나름 신경좀 써 봤지만.. 오히려 더 지저분한 느낌도 든다(순수한 나의 생각이지만  --;)
 
 
===========================================================================================================================
 
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];
    }
}
=========================================================================================================================== 

+ Recent posts