2013-04-18 134 views
0

在我的應用程序中,我想修改這個coverflow,以便我可以使用它的xml文件。匯聚流程如下。當我改變了下面修改Coverflow以使用xml文件

ConverFlow cf = new Coverflow(this); 

的CoverFlow的到

CoverFlow cf = new CoverFlow(R.id.coverflowReflect); 

我得到這個錯誤: enter image description here 構造的CoverFlow(INT)是未定義

我得到了3個修復了這個問題。

第一個是給構造

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

改變

public CoverFlow(int coverflowreflect) { 
     super(coverflowreflect); 
     this.setStaticTransformationsEnabled(true); 
    } 

,但我得到了我的CoverFlow班說

構造函數庫(INT)是未定義的錯誤。

這裏是我的CoverFlow類:

public class CoverFlow extends Gallery { 

     private Camera mCamera = new Camera(); 
     private int mMaxRotationAngle = 0; 
     private int mMaxZoom = -380; 
     private int mCoveflowCenter; 
     private boolean mAlphaMode = true; 
     private boolean mCircleMode = false; 
     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); 
     } 
     public int getMaxRotationAngle() { 
      return mMaxRotationAngle; 
     } 
     public void setMaxRotationAngle(int maxRotationAngle) { 
      mMaxRotationAngle = maxRotationAngle; 
     } 
     public boolean getCircleMode() { 
      return mCircleMode; 
     } 
     public void setCircleMode(boolean isCircle) { 
      mCircleMode = isCircle; 
     } 
     public boolean getAlphaMode() { 
      return mAlphaMode; 
     } 
     public void setAlphaMode(boolean isAlpha) { 
      mAlphaMode = isAlpha; 
     } 
     public int getMaxZoom() { 
      return mMaxZoom; 
     } 
     public void setMaxZoom(int maxZoom) { 
      mMaxZoom = maxZoom; 
     } 
     private int getCenterOfCoverflow() { 
      return (getWidth() - getPaddingLeft() - getPaddingRight())/2 
        + getPaddingLeft(); 
     } 
     private static int getCenterOfView(View view) { 
      return view.getLeft() + view.getWidth()/2; 
     } 
     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. 
     */ 
     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 = (float) (mMaxZoom + (rotation * 1.5)); 
       mCamera.translate(0.0f, 0.0f, zoomAmount); 
       if (mCircleMode) { 
        if (rotation < 40) 
         mCamera.translate(0.0f, 155, 0.0f); 
        else 
         mCamera.translate(0.0f, (255 - rotation * 2.5f), 0.0f); 
       } 
       if (mAlphaMode) { 
        ((ImageView) (child)).setAlpha((int) (255 - rotation * 2.5)); 
       } 
      } 
      mCamera.rotateY(rotationAngle); 
      mCamera.getMatrix(imageMatrix); 

      imageMatrix.preTranslate(-(imageWidth), -(imageHeight/2)); 
      imageMatrix.postTranslate((imageWidth), (imageHeight/2)); 

      mCamera.restore(); 
     } 
    } 



And this is where i made the call the activity 


public class CoverFlowActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     int pixels = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 60, getResources().getDisplayMetrics()); 
     CoverFlow cf = new CoverFlow(this); 
     ImageAdapter ia = new ImageAdapter(this); 
     cf.setAdapter(ia); 
     cf.setAnimationDuration(1000); 
     cf.setSpacing(pixels); 
     setContentView(cf); 

    } 

    public class ImageAdapter extends BaseAdapter { 

     private int[] mImgs = { 
       R.drawable.img1, 
       R.drawable.img2, 
       R.drawable.img3, 
       R.drawable.img4, 
       R.drawable.img5, 
       R.drawable.img6, 
       R.drawable.img7, 
       R.drawable.img8 
     }; 
     Context mContext; 

     public ImageAdapter(Context context) { 
      this.mContext = context; 
     } 
     @Override 
     public int getCount() { 
      return mImgs.length; 
     } 

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

     @Override 
     public long getItemId(int position) { 
      return mImgs[position]; 

     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      ReflectionImage i = new ReflectionImage(mContext); 

      i.setImageResource(mImgs[position]); 
      i.setLayoutParams(new CoverFlow.LayoutParams(160, 160)); 
      i.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 
      BitmapDrawable drawable = (BitmapDrawable) i.getDrawable(); 
      drawable.setAntiAlias(true); 

      return i; 
     } 

     public float getScale(boolean focused, int offset) { 
      return Math.max(0, 1f/(float)Math.pow(2, Math.abs(offset))); 
     } 

    } 
} 

的第二個選擇是構造流程更改爲

public CoverFlow(int defStyle) { 
super(context, attrs, defStyle); 
... again Coverflow give an error. 

構造溢出(INT)是不確定的。

是否有人去貫穿同一問題,我的意思是我想要得到這個活動工作與我的XML佈局,但不是編程方式,因爲它是現在。請幫助

**更新:應用本傑明回答後,我得到以下錯誤這裏是我的日誌。

注意:第32行:setContentView(coverFlow); **

04-18 13:22:42.812: E/AndroidRuntime(4096): FATAL EXCEPTION: main 
04-18 13:22:42.812: E/AndroidRuntime(4096): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ufida.coverflow/com.ufida.coverflow.CoverFlowActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 
04-18 13:22:42.812: E/AndroidRuntime(4096):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651) 
04-18 13:22:42.812: E/AndroidRuntime(4096):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) 
04-18 13:22:42.812: E/AndroidRuntime(4096):  at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
04-18 13:22:42.812: E/AndroidRuntime(4096):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935) 
04-18 13:22:42.812: E/AndroidRuntime(4096):  at android.os.Handler.dispatchMessage(Handler.java:99) 
04-18 13:22:42.812: E/AndroidRuntime(4096):  at android.os.Looper.loop(Looper.java:130) 
04-18 13:22:42.812: E/AndroidRuntime(4096):  at android.app.ActivityThread.main(ActivityThread.java:3687) 
04-18 13:22:42.812: E/AndroidRuntime(4096):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-18 13:22:42.812: E/AndroidRuntime(4096):  at java.lang.reflect.Method.invoke(Method.java:507) 
04-18 13:22:42.812: E/AndroidRuntime(4096):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842) 
04-18 13:22:42.812: E/AndroidRuntime(4096):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 
04-18 13:22:42.812: E/AndroidRuntime(4096):  at dalvik.system.NativeStart.main(Native Method) 
04-18 13:22:42.812: E/AndroidRuntime(4096): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 
04-18 13:22:42.812: E/AndroidRuntime(4096):  at android.view.ViewGroup.addViewInner(ViewGroup.java:1976) 
04-18 13:22:42.812: E/AndroidRuntime(4096):  at android.view.ViewGroup.addView(ViewGroup.java:1871) 
04-18 13:22:42.812: E/AndroidRuntime(4096):  at android.view.ViewGroup.addView(ViewGroup.java:1851) 
04-18 13:22:42.812: E/AndroidRuntime(4096):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:228) 
04-18 13:22:42.812: E/AndroidRuntime(4096):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:218) 
04-18 13:22:42.812: E/AndroidRuntime(4096):  at android.app.Activity.setContentView(Activity.java:1668) 
04-18 13:22:42.812: E/AndroidRuntime(4096):  at com.ufida.coverflow.CoverFlowActivity.onCreate(CoverFlowActivity.java:32) 
04-18 13:22:42.812: E/AndroidRuntime(4096):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
04-18 13:22:42.812: E/AndroidRuntime(4096):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615) 
04-18 13:22:42.812: E/AndroidRuntime(4096):  ... 11 more 

回答

0

你需要找到ID 視圖試試這個:

XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <com.yourpackagename.CoverFlow 
     android:id="@+id/coverflowid" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</LinearLayout> 

活動:

import com.yourpackagename.CoverFlow; // the coverflow class 

CoverFlow coverFlow; 

public class CoverFlowActivity extends Activity{ 

//in your oncreate do this: 

coverFlow = (CoverFlow) findViewById(R.id.coverflowid); 

} 
+0

嗨本傑明佛爾,我用你的答案我有一個錯誤。我更新我的問題與日誌可以請你看看 – yakusha

+0

你有團隊查看器,所以我可以看看你並幫助你。 –

+0

我不,但我可以郵寄給你的課程和.xml文件 – yakusha

0

你不能這樣做,因爲CoverFlow cf = new CoverFlow(R.id.coverflowReflect);您的Coverflow構造函數採用Context類型的參數。

你應該調用構造這樣

CoverFlow cf = new CoverFlow(this); 

如果你將改變構造帶整型參數則無法通過傳遞int變量調用它的超類的方法,因爲Gallery類的構造函數將採取因爲你從XML想要參數作爲Context對象不integer

+0

他從XML希望不只是一個新的CoverFlow他的意思是這樣的:的CoverFlow =(的CoverFlow)findViewById(R.id.coverflowid); –

+0

但我想修改這個代碼來做CoverFlow cf = new CoverFlow(R.id.coverflowReflect);可能嗎 ? – yakusha