0

在我的应用程序中,我想修改这个封面流,以便我可以使用 xml 文件。汇流如下。当我改变下面的coverflow

ConverFlow cf = new Coverflow(this);

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

我收到此错误: 在此处输入图像描述 构造函数 CoverFlow(int) 未定义

我为这个问题做了 3 个修复。

第一个是改变构造函数

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

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

但我在我的coverflow类中出现错误说

构造函数 Gallery(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.

构造函数 overflow(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
4

2 回答 2

0

你不能这样做,CoverFlow cf = new CoverFlow(R.id.coverflowReflect);因为你的构造函数Coverflow需要一个类型的参数Context

你应该像这样调用构造函数

CoverFlow cf = new CoverFlow(this);

如果您将构造函数更改为采用整数参数,那么您不能通过将 int 变量传递给它来调用超类方法,因为Gallery类构造函数将参数作为Context对象而不是integer类型

于 2013-04-18T09:09:35.640 回答
0

既然你想从 xml 中得到它,你需要通过 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);

}
于 2013-04-18T09:15:50.983 回答