Android 修改Coverflow以使用xml文件
在我的应用程序中,我想修改这个coverflow,以便使用xml文件。对流情况如下。当我换了下面的封面Android 修改Coverflow以使用xml文件,android,Android,在我的应用程序中,我想修改这个coverflow,以便使用xml文件。对流情况如下。当我换了下面的封面 ConverFlow cf = new Coverflow(this); 到 我得到这个错误: 构造函数CoverFlow(int)未定义 我有3个修复这个问题 第一个是更改构造函数 public CoverFlow(Context context) { super(context); this.setStaticTransformationsEnabled(
ConverFlow cf = new Coverflow(this);
到
我得到这个错误:
构造函数CoverFlow(int)未定义
我有3个修复这个问题
第一个是更改构造函数
public CoverFlow(Context context) {
super(context);
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)));
}
}
}
构造函数溢出(int)未定义
是否有人讨论过同样的问题,我的意思是我想让这个活动与我的xml布局一起工作,但不是像现在这样以编程方式工作。请帮忙
**更新:在应用Benjamin答案后,我发现以下错误,这是我的日志
注:第32行:setContentView(coverFlow);
**
您不能执行
CoverFlow cf=new CoverFlow(R.id.coverflowReflect)
因为Coverflow
的构造函数接受类型为Context
的参数
您应该像这样调用构造函数
CoverFlow cf = new CoverFlow(this);
如果要将构造函数更改为接受整数参数,则不能通过向其传递int变量来调用超类方法,因为
库类构造函数将参数作为上下文对象而不是整数类型,因为要从xml中获取它,需要按id查找视图
试试这个:
XML:
他希望从xml中得到的不仅仅是一个新的CoverFlow,他的意思是:CoverFlow=(CoverFlow)findViewById(R.id.coverflowid);但我想修改此代码以执行CoverFlow cf=newcoverflow(R.id.coverflowReflect);有可能吗?嗨,本杰明·费尔,我用你的答案回答了一个错误。我用日志更新了我的问题,你能看一下吗?你有teamviewer吗?这样我可以和你一起看,帮你解决问题。我没有,但我可以把类和.xml文件发送给benjamin_faal@hotmail.comHello我查看了您的代码,修复非常简单,删除这行代码:setContentView(coverFlow);在第32行,您只能调用setContentView();一旦在活动中,并且您已经在oncreate下调用了它
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.
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
CoverFlow cf = new CoverFlow(this);
<?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);
}