2012-05-15 55 views
2

我已經實現了Android Coverflow示例。點擊圖像後,我可以檢索它的位置並在ImageView中顯示圖像。 我的其他要求是當我通過它的Id時將圖像集中在coverFlow中。我應該把重點放在中心。 這是用於計算轉換期間圖像之間角度的代碼。我如何改變它以達到要求的輸出?Android Coverflow - 將位置已知的圖像聚焦

protected boolean getChildStaticTransformation(final View child, final 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; 
} 


private void transformImageBitmap(final ImageView child, final Transformation t, final int rotationAngle) { 
    mCamera.save(); 
    final Matrix imageMatrix = t.getMatrix(); 

    final int height = child.getLayoutParams().height; 

    final int width = 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) { 
     final float zoomAmount = (float) (mMaxZoom + rotation * 1.5); 
     mCamera.translate(0.0f, 0.0f, zoomAmount); 
    } 

    mCamera.rotateY(rotationAngle); 
    mCamera.getMatrix(imageMatrix); 
    imageMatrix.preTranslate(-(width/2.0f), -(height/2.0f)); 
    imageMatrix.postTranslate((width/2.0f), (height/2.0f)); 
    mCamera.restore(); 
} 

回答

0

可以使用的CoverFlow的setOnItemSelectedListener實現這一目標。請參閱下面的代碼,您將獲得中心圖像的位置。

int sel_pos; 

coverFlow.setOnItemSelectedListener(new SelectListener(this)); 

    private class SelectListener implements AdapterView.OnItemSelectedListener { 

      public SelectListener(Context c) 
      { 

      } 

      public void onItemSelected(AdapterView<?> parent, View v, int position,long id) 
      { 

       Log.e("Changed----->", "" + position); 

       // Zoom the new selected view 
       try 
       {        
        sel_pos = position;    
        coverImageAdapter.notifyDataSetChanged(); 

       } catch (Exception animate) 
       { 

       }  

      } 

      public void onNothingSelected(AdapterView<?> parent) { 
      } 

     } 

現在,您可以在適配器的getview方法的coverFlow中更改焦點圖像。

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.refreshDrawableState(); 
     Log.e("position==", ""+position); 

     if(sel_pos==position) 
     { 
      i.setImageResource(selectedImage[position]); 
     } 
     else 
     { 
      i.setImageResource(UnselectedImage[position]); 

     }  


     i.setLayoutParams(new CoverFlow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
     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; 

    } 

跳這將幫助你。