我创建了一个自定义视图类,它接受一个路径,然后将其填充为白色并将其边缘着色为红色:
StrokeFill.java:
package com.kf.pathshape;
...
public class StrokeFill extends View{
private Path shapePath;
public StrokeFill(Context context, Path path) {
super(context);
this.shapePath = path;
// TODO Auto-generated constructor stub
}
public StrokeFill(Context context, AttributeSet attrs, Path shapePath) {
super(context, attrs);
this.shapePath = shapePath;
}
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
//canvas.drawColor(Color.BLACK);
super.onDraw(canvas);
//Sets paints and draws the shapes
Paint fillPaint = new Paint();
fillPaint.setColor(android.graphics.Color.WHITE);
fillPaint.setStyle(Paint.Style.FILL);
fillPaint.setStrokeWidth(0);
Paint strokePaint = new Paint();
strokePaint.setColor(android.graphics.Color.RED);
strokePaint.setStyle(Paint.Style.STROKE);
strokePaint.setStrokeWidth(2);
canvas.drawPath(shapePath, fillPaint);
canvas.drawPath(shapePath, strokePaint);
}
}
我的布局XML(main.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.kf.pathshape.StrokeFill
android:id="@+id/pathshape"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
的活动:
setContentView(R.layout.main);
shapeView = (StrokeFill) findViewById(R.id.pathshape);
shapeView = new StrokeFill(this, testpath);
testpath是一个有效的路径。即使我注释掉定义“shapeView”的行,它也会给我错误。 logcat的:
07-11 01:50:17.150: E/AndroidRuntime(24321): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kf.pathshape/com.kf.pathshape.PathshapeActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class com.kf.pathshape.StrokeFill
07-11 01:50:17.150: E/AndroidRuntime(24321): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
07-11 01:50:17.150: E/AndroidRuntime(24321): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
07-11 01:50:17.150: E/AndroidRuntime(24321): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-11 01:50:17.150: E/AndroidRuntime(24321): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
07-11 01:50:17.150: E/AndroidRuntime(24321): at android.os.Handler.dispatchMessage(Handler.java:99)
07-11 01:50:17.150: E/AndroidRuntime(24321): at android.os.Looper.loop(Looper.java:130)
07-11 01:50:17.150: E/AndroidRuntime(24321): at android.app.ActivityThread.main(ActivityThread.java:3691)
07-11 01:50:17.150: E/AndroidRuntime(24321): at java.lang.reflect.Method.invokeNative(Native Method)
07-11 01:50:17.150: E/AndroidRuntime(24321): at java.lang.reflect.Method.invoke(Method.java:507)
07-11 01:50:17.150: E/AndroidRuntime(24321): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
07-11 01:50:17.150: E/AndroidRuntime(24321): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
07-11 01:50:17.150: E/AndroidRuntime(24321): at dalvik.system.NativeStart.main(Native Method)
07-11 01:50:17.150: E/AndroidRuntime(24321): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class com.kf.pathshape.StrokeFill
07-11 01:50:17.150: E/AndroidRuntime(24321): at android.view.LayoutInflater.createView(LayoutInflater.java:508)
07-11 01:50:17.150: E/AndroidRuntime(24321): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
07-11 01:50:17.150: E/AndroidRuntime(24321): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
07-11 01:50:17.150: E/AndroidRuntime(24321): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
07-11 01:50:17.150: E/AndroidRuntime(24321): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
07-11 01:50:17.150: E/AndroidRuntime(24321): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
07-11 01:50:17.150: E/AndroidRuntime(24321): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:234)
07-11 01:50:17.150: E/AndroidRuntime(24321): at android.app.Activity.setContentView(Activity.java:1679)
07-11 01:50:17.150: E/AndroidRuntime(24321): at com.kf.pathshape.PathshapeActivity.onCreate(PathshapeActivity.java:73)
07-11 01:50:17.150: E/AndroidRuntime(24321): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
07-11 01:50:17.150: E/AndroidRuntime(24321): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
07-11 01:50:17.150: E/AndroidRuntime(24321): ... 11 more
07-11 01:50:17.150: E/AndroidRuntime(24321): Caused by: java.lang.NoSuchMethodException: StrokeFill(Context,AttributeSet)
我在论坛中搜索但没找到我需要的东西。我已经定义了构造函数的上下文和attrs。我在这里缺少什么?
答案 0 :(得分:2)
在扩展视图时,需要实现3个构造函数:
public class StrokeFill extends View{
private Path mShapePath;
public StrokeFill(Context context) {
super(context);
init();
}
public StrokeFill(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public StrokeFill(Context context, AttributeSet attrs, int defstyle) {
super(context, attrs, defstyle);
init();
}
private void init(){
//some code... or not =)
}
public void setPath(Path path){
mShapePath = path;
}
/* rest of your code */
}
然后在你的活动中:
shapeView = (StrokeFill) findViewById(R.id.pathshape);
shapeView.setPath(yourPath);
希望这有助于你
答案 1 :(得分:0)
无需再次创建
shapeView = new StrokeFill(this, testpath);
像这样修改
setContentView(R.layout.main);
shapeView = (StrokeFill) findViewById(R.id.pathshape);
答案 2 :(得分:0)
当您从xml android调用StrokeFill(Context,AttributeSet)
构造函数创建自定义视图时。如果要从xml中扩展视图,则必须创建该构造函数。您还可以在代码中创建视图,然后从xml中删除它,在这种情况下,您不需要StrokeFill(Context,AttributeSet)
构造函数。
例如:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>
的活动:
setContentView(R.layout.main);
LinearLayout v = (LinearLayout)findViewById(R.id.myLayout);
shapeView = new StrokeFill(this, testpath);
v.addView(shapeView);