Android 如何调整coverflow/gallery以适应不同的屏幕尺寸

Android 如何调整coverflow/gallery以适应不同的屏幕尺寸,android,gallery,coverflow,screen-size,Android,Gallery,Coverflow,Screen Size,我正在使用此CoverFlow: 我想让coverflow适应不同的屏幕尺寸 我稍微修改了coverflow,以便使用XML布局 以下是我的手机(320x480)的布局和外观 以下是NexusOne(emulator中的480x720)上的布局 COVERFLOW类别: public class CoverFlow extends Gallery { /** * Graphics Camera used for transforming the matrix of Ima

我正在使用此CoverFlow:

我想让coverflow适应不同的屏幕尺寸

我稍微修改了coverflow,以便使用XML布局

以下是我的手机(320x480)的布局和外观

以下是NexusOne(emulator中的480x720)上的布局

COVERFLOW类别:

public class CoverFlow extends Gallery {

    /**
     * Graphics Camera used for transforming the matrix of ImageViews
     */
    private final Camera mCamera = new Camera();

    /**
     * The maximum angle the Child ImageView will be rotated by
     */    
    private int mMaxRotationAngle = 80;

    /**
     * The maximum zoom on the centre Child
     */
    // TODO RENDRE LA VALEUR DYNAMIQUE SUR LA TAILLE DE L'ECRAN

//    private int mMaxZoom = -430;
    private int mMaxZoom = -370;

    /**
     * The Centre of the Coverflow 
     */   
    private int mCoveflowCenter;

 public CoverFlow(Context context) {
  super(context);
  this.setStaticTransformationsEnabled(true);
 }

 public CoverFlow(Context context, AttributeSet attrs) {
  super(context, attrs);
        this.setStaticTransformationsEnabled(true);
 }

  public CoverFlow(Context context, AttributeSet attrs, int defStyle) {
   super(context, attrs, defStyle);
   this.setStaticTransformationsEnabled(true);   
  }

    /**
     * Get the max rotational angle of the image
  * @return the mMaxRotationAngle
  */
 public int getMaxRotationAngle() {
  return mMaxRotationAngle;
 }

 /**
  * Set the max rotational angle of each image
  * @param maxRotationAngle the mMaxRotationAngle to set
  */
 public void setMaxRotationAngle(int maxRotationAngle) {
  mMaxRotationAngle = maxRotationAngle;
 }

 /**
  * Get the Max zoom of the centre image
  * @return the mMaxZoom
  */
 public int getMaxZoom() {
  return mMaxZoom;
 }

 /**
  * Set the max zoom of the centre image
  * @param maxZoom the mMaxZoom to set
  */
 public void setMaxZoom(int maxZoom) {
  mMaxZoom = maxZoom;
 }

 /**
     * Get the Centre of the Coverflow
     * @return The centre of this Coverflow.
     */
    private int getCenterOfCoverflow() {
        return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2 + getPaddingLeft();
    }

    /**
     * Get the Centre of the View
     * @return The centre of the given view.
     */
    private static int getCenterOfView(View view) {
        return view.getLeft() + view.getWidth() / 2;
    }  
    /**
  * {@inheritDoc}
  *
  * @see #setStaticTransformationsEnabled(boolean) 
  */ 
    @Override
    protected boolean getChildStaticTransformation(View child, Transformation t) {

  final int childCenter = getCenterOfView(child);
  final int childWidth = child.getWidth() ;
  int rotationAngle = 0;

  t.clear();
  t.setTransformationType(Transformation.TYPE_MATRIX);

        if (childCenter == mCoveflowCenter) {
            transformImageBitmap((ImageView) child, t, 0);
        } else {      
            rotationAngle = (int) (((float) (mCoveflowCenter - childCenter)/ childWidth) *  mMaxRotationAngle);
            if (Math.abs(rotationAngle) > mMaxRotationAngle) {
             rotationAngle = (rotationAngle < 0) ? -mMaxRotationAngle : mMaxRotationAngle;   
            }
            transformImageBitmap((ImageView) child, t, rotationAngle);         
        }    

  return true;
 }

 /**
  * This is called during layout when the size of this view has changed. If
  * you were just added to the view hierarchy, you're called with the old
  * values of 0.
  *
  * @param w Current width of this view.
  * @param h Current height of this view.
  * @param oldw Old width of this view.
  * @param oldh Old height of this view.
     */
     @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
      mCoveflowCenter = getCenterOfCoverflow();
      super.onSizeChanged(w, h, oldw, oldh);
     }

     /**
      * Transform the Image Bitmap by the Angle passed 
      * 
      * @param imageView ImageView the ImageView whose bitmap we want to rotate
      * @param t transformation 
      * @param rotationAngle the Angle by which to rotate the Bitmap
      */
     private void transformImageBitmap(ImageView child, Transformation t, int rotationAngle) {            
      mCamera.save();
      final Matrix imageMatrix = t.getMatrix();;
      final int imageHeight = child.getLayoutParams().height;;
      final int imageWidth = child.getLayoutParams().width;
      final int rotation = Math.abs(rotationAngle);

      mCamera.translate(0.0f, 0.0f, 100.0f);

      //As the angle of the view gets less, zoom in     
      if ( rotation < mMaxRotationAngle ) {
       float zoomAmount = (mMaxZoom + rotation);
       mCamera.translate(0.0f, 0.0f, zoomAmount);          
      } 

      mCamera.rotateY(rotationAngle);
      mCamera.getMatrix(imageMatrix);               
      imageMatrix.preTranslate(-(imageWidth/2), -(imageHeight/2)); 
      imageMatrix.postTranslate((imageWidth/2), (imageHeight/2));
      mCamera.restore();
 }
}
公共类CoverFlow扩展库{
/**
*用于变换图像视图矩阵的图形摄像机
*/
专用最终摄像头mCamera=新摄像头();
/**
*子ImageView将旋转的最大角度
*/    
私有int mMaxRotationAngle=80;
/**
*中心子对象上的最大缩放
*/
//托多·兰德尔·拉瓦勒·德莱坎河畔的活力之旅
//私有int-mMaxZoom=-430;
私有int-mMaxZoom=-370;
/**
*覆盖流的中心
*/   
私人国际流动中心;
公共CoverFlow(上下文){
超级(上下文);
此.setStaticTransformationsEnabled(true);
}
公共CoverFlow(上下文、属性集属性){
超级(上下文,attrs);
此.setStaticTransformationsEnabled(true);
}
公共CoverFlow(上下文上下文、属性集属性、int-defStyle){
超级(上下文、属性、定义样式);
此.setStaticTransformationsEnabled(true);
}
/**
*获取图像的最大旋转角度
*@返回mMaxRotationAngle
*/
public int getMaxRotationAngle(){
返回Mmax旋转角度;
}
/**
*设置每个图像的最大旋转角度
*@param maxRotationAngle要设置的mMaxRotationAngle
*/
公共无效设置maxRotationAngle(int maxRotationAngle){
mMaxRotationAngle=最大旋转角度;
}
/**
*获取中心图像的最大变焦
*@返回mMaxZoom
*/
public int getMaxZoom(){
返回mMaxZoom;
}
/**
*设置中心图像的最大缩放
*@param maxZoom要设置的mMaxZoom
*/
公共void setMaxZoom(int-maxZoom){
mMaxZoom=maxZoom;
}
/**
*获取覆盖流的中心
*@返回此覆盖流的中心。
*/
私有int GetCenterOfOverflow(){
返回(getWidth()-getPaddingLeft()-getPaddingRight())/2+getPaddingLeft();
}
/**
*占据视野的中心
*@返回给定视图的中心。
*/
私有静态int getCenterOfView(视图){
return view.getLeft()+view.getWidth()/2;
}  
/**
*{@inheritardoc}
*
*@see#setStaticTransformationsEnabled(布尔值)
*/ 
@凌驾
受保护的布尔getChildStaticTransformation(查看子对象,转换t){
final int childCenter=getCenterOfView(子对象);
final int childWidth=child.getWidth();
int rotationAngle=0;
t、 清除();
t、 setTransformationType(Transformation.TYPE_矩阵);
if(childCenter==mCoveflowCenter){
transformImageBitmap((ImageView)子级,t,0);
}否则{
rotationAngle=(int)((float)(mCoveflowCenter-childCenter)/childWidth)*mmax rotationAngle);
if(数学绝对值(旋转角度)>MMAX旋转角度){
rotationAngle=(rotationAngle<0)?-mmax rotationAngle:mmax rotationAngle;
}
transformImageBitmap((ImageView)子对象,t,旋转角度);
}    
返回true;
}
/**
*当此视图的大小更改时,在布局期间调用此函数。如果
*您刚刚被添加到视图层次结构中,将使用旧的
*值为0。
*
*@param w此视图的当前宽度。
*@param h此视图的当前高度。
*@param oldw此视图的旧宽度。
*@param Old此视图的旧高度。
*/
@凌驾
已更改尺寸的受保护空心(整数w、整数h、整数oldw、整数oldh){
mCoveflowCenter=getCenterOfCoverflow();
super.onSizeChanged(w,h,oldw,oldh);
}
/**
*按传递的角度变换图像位图
* 
*@param imageView要旋转其位图的imageView
*@param t变换
*@param rotation旋转位图的角度
*/
私有void transformImageBitmap(ImageView子级,转换t,int rotationAngle){
mCamera.save();
最终矩阵imageMatrix=t.getMatrix();;
final int-imageHeight=child.getLayoutParams().height;;
final int imageWidth=child.getLayoutParams().width;
最终整数旋转=数学abs(旋转角度);
翻译(0.0f,0.0f,100.0f);
//随着视图角度变小,放大视图
if(旋转角度
家庭活动:

public class HomeActivity extends Activity {

    private  final static String TAG = "HomeActivity";
    private TextView pageNameTextView;
    private CoverFlow coverFlow;
    private ImageAdapter coverImageAdapter;
    private int itemSelected = 0;
    private Context context;
    private SparseArray<String> listeNomIcons;
    private int currentImagePosition = 0;

    //Info button
    private ImageView infoAccueilImageView; 

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_layout);
        //animate Transition
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);

        context = this;

        listeNomIcons = new SparseArray<String>();
        listeNomIcons.put(0, "DELAIS D'ATTENTE, RETARD");
        listeNomIcons.put(1, "COURRIER SUBSTITUTION");
        listeNomIcons.put(2, "IR LC");
        listeNomIcons.put(3, "CONTACTS UTILES");
        listeNomIcons.put(4, "TEMPS DE PAUSE");
        listeNomIcons.put(5, "DISPERTION");
        listeNomIcons.put(6, "PRORATA REPOS");
        listeNomIcons.put(7, "ALERTE DOMICILE");
        listeNomIcons.put(8, "RESERVE DOMICILE");
        listeNomIcons.put(9, "RADD");
        listeNomIcons.put(10, "JOKER");

        coverFlow = (CoverFlow)findViewById(R.id.coverflow);  
        coverImageAdapter =  new ImageAdapter(this);  
        coverFlow.setAdapter(coverImageAdapter);
        coverFlow.setSelection(0, true);
        coverFlow.setAnimationDuration(1000);

        //cover

        pageNameTextView = (TextView)findViewById(R.id.page_nameTextView);

        //Info Accueil Image View
        infoAccueilImageView = (ImageView)findViewById(R.id.infoImageView);
        infoAccueilImageView.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(context, InfoAccueilActivity.class));
            }
        } ) ;

        coverFlow.setOnItemSelectedListener(new OnItemSelectedListener(){
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                currentImagePosition = position; //this will update your current marker
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });

        Button goLeft = (Button) findViewById(R.id.select_leftButton);
        goLeft.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // go to previous image

                coverFlow.onKeyDown(KeyEvent.KEYCODE_DPAD_LEFT, new KeyEvent(0, 0));
            }
        });

        Button goRight = (Button) findViewById(R.id.select_rightButton);
        goRight.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // go to next item
                coverFlow.onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, new KeyEvent(0, 0));
            }
        });

        coverFlow.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {

                if(itemSelected == arg2)
                {
                    Log.d(TAG, "arg2 : "+arg2);
                    switch (arg2) {
                    case 0:
                        startActivity(new Intent(context, DelaisAttenteActivity.class));
                        break;
                    case 1:
                        startActivity(new Intent(context, CourrierSubstitutionActivity.class));
                        break;
                    case 2:
                        startActivity(new Intent(context, IRLCActivity.class));
                        break;
                    case 3:
                        startActivity(new Intent(context, ContactsActivity.class));
                        break;
                    case 4:
                        startActivity(new Intent(context, TempsPauseActivity.class));
                        break;
                    case 5:
                        startActivity(new Intent(context, DispertionActivity.class));
                        break;
                    case 6:
                        startActivity(new Intent(context, ProrataReposActivity.class));
                        break;
                    case 7:
                        startActivity(new Intent(context, AlerteDomicileActivity.class));
                        break;
                    case 8:
                        startActivity(new Intent(context, ReserveDomicileActivity.class));
                        break;                      
                    case 9:
                        startActivity(new Intent(context, ReposAdditionnelActivity.class));
                        break;
                    case 10:
                        startActivity(new Intent(context, JokerActivity.class));
                        break;

                    default:
                        break;
                    }
                }
            }
        });
        coverFlow.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {

                pageNameTextView.setText(listeNomIcons.get(arg2));
                itemSelected = arg2;
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });
    }

    public class ImageAdapter extends BaseAdapter {
        int mGalleryItemBackground;
        private final Context mContext;


        private final Integer[] mImageIds = {
                R.drawable.retard_controller,
                R.drawable.courrier_substitution_controller,
                R.drawable.irmf_controller,
                R.drawable.contacts_controller,
                R.drawable.pause_controller,
                R.drawable.dispersion_controller,
                R.drawable.repos_controller,
                R.drawable.alerte_controller,
                R.drawable.reserve_domicile_controller,
                R.drawable.repos_additionnel_controller,
                R.drawable.joker_controller
        };

        private final ImageView[] mImages;

        public ImageAdapter(Context c) {
            mContext = c;
            mImages = new ImageView[mImageIds.length];
        }

        @Override
        public int getCount() {
            return mImageIds.length;
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            //Use this code if you want to load from resources
            ImageView i = new ImageView(mContext);
            i.setImageResource(mImageIds[position]);
            i.setLayoutParams(new CoverFlow.LayoutParams(130, 130));
            i.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 
            //Make sure we set anti-aliasing otherwise we get jaggies
            BitmapDrawable drawable = (BitmapDrawable) i.getDrawable();
            drawable.setAntiAlias(true);
            return i;

            //return mImages[position];
        }
        /** Returns the size (0.0f to 1.0f) of the views 
         * depending on the 'offset' to the center. */ 
        public float getScale(boolean focused, int offset) { 
            /* Formula: 1 / (2 ^ offset) */ 
            return Math.max(0, 1.0f / (float)Math.pow(2, Math.abs(offset))); 
        } 

    }
}
公共类HomeActivity扩展活动{
私有最终静态字符串TAG=“HomeActivity”;
私有文本视图页面名称文本视图;
私人CoverFlow CoverFlow;
专用图像适配器coverImageAdapter;
private int itemSelected=0;
私人语境;
私人麻雀;
private int currentImagePosition=0;
//信息按钮
私有图像视图InfoAccueLimageView;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(右布局、主布局);
//设置过渡动画
重写转换(android.R.anim.fade\u-in,android.R.anim.fade\u-out);
上下文=这个;
listeNomIcons=新SparseArray();
listeNomIcons.put(0,“DELAIS D'ATTENTE,delay”);
listeNomIcons.put(1,“信使替代”);
@Override
public View getView(int position, View convertView, ViewGroup parent)
i.setLayoutParams(new CoverFlow.LayoutParams(130, 130));
int imageSize = calculateSize();
i.setLayoutParams(new CoverFlow.LayoutParams(imageSize, imageSize));
public int calculateSize()
{
    // GET SCREEN SIZE
    Display display = getWindowManager().getDefaultDisplay();

    // HEIGHT
    int height = display.getHeight();
    long roundedHeightSize = Math.round((0.2132*height)+27.177);

    //WIDTH
    int width = display.getWidth();
    long roundedWidthSize = Math.round((0.4264*width)-6.9355);


    return (int)((roundedHeightSize+roundedWidthSize)/2);
}
f(1280)=300
f(480)=135
f(320)=95

f(x) = 0.2132X + 27.177
if (convertView == null) {
            convertView = lInflater.inflate(R.layout.item_gallery, null);
            ((RelativeLayout) convertView.findViewById(R.id.image_issue_layout)).setLayoutParams(new CoverFlowView.LayoutParams(
                    app.getResources().getDimensionPixelSize(R.dimen.width_coverflow), 
                    app.getResources().getDimensionPixelSize(R.dimen.height_coverflow)));