在布局中显示自定义视图

时间:2012-03-22 14:52:01

标签: java android

我目前有一个名为clientActivity的类,它包含绘制到画布上的手指paintig活动,该类具有View Called MyView的扩展名。我想这样做,以便画布在我的xml聊天布局上以相对布局显示。

这是ClientActivity类

public class ClientActivity extends GraphicsActivity
implements ColorPickerDialog.OnColorChangedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(new MyView(this));
    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setDither(true);
    mPaint.setColor(Color.BLACK);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(20);

    mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 },
            0.4f, 6, 3.5f);

    mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);

}

private Paint       mPaint;
private MaskFilter  mEmboss;
private MaskFilter  mBlur;

public void colorChanged(int color) {
    mPaint.setColor(color);
}


public class MyView extends View {

    ClientNetwork net = new ClientNetwork();

    private static final float MINP = 0.25f;
    private static final float MAXP = 0.75f;

    private Bitmap  mBitmap;
    private Canvas  mCanvas;
    private Path    mPath;
    private Paint   mBitmapPaint;

    public MyView(Context c) {
        super(c);

        mPath = new Path();
        mBitmapPaint = new Paint(Paint.DITHER_FLAG);
        Thread fred = new Thread(net);
        fred.start();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);



    }

我的chat.xml看起来像这样。

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

<RelativeLayout android:id="@+id/relativeLayout3"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_weight="0.83">

<com.DrawTastic.ClientActivity android:id="@+id/MyView1"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center"/>


</RelativeLayout>

<ListView 
android:id="@+id/listView"
    android:layout_width="match_parent" 
    android:layout_height="152dp" android:layout_weight="0.17">
    </ListView>



    <RelativeLayout 
    android:id="@+id/relativeLayout2"
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true" 
        android:layout_alignParentLeft="true">


        <EditText 
        android:layout_width="232dp"
            android:layout_alignParentTop="true" 
            android:layout_alignParentLeft="true"
            android:layout_height="wrap_content" 
            android:id="@+id/input">
            </EditText>


        <Button 
        android:layout_width="wrap_content"
            android:layout_alignParentTop="true" 
            android:layout_marginRight="16dp"
            android:layout_alignParentRight="true" 
            android:text="Send"
            android:onClick="sent" android:id="@+id/send" 
            android:layout_height="wrap_content">
            </Button>

    </RelativeLayout>

我只需要在其中一个布局中的chat.xml中显示画布,这样我就可以在它下面放置一个列表视图。

非常感谢任何帮助!

由于

编辑:堆栈跟踪。

03-22 15:32:21.310: ERROR/AndroidRuntime(1301): FATAL EXCEPTION: main
03-22 15:32:21.310: ERROR/AndroidRuntime(1301): java.lang.RuntimeException: Unable to     start activity ComponentInfo{com.DrawTastic/com.DrawTastic.GameActivity}:     android.view.InflateException: Binary XML file line #8: Error inflating class     com.DrawTastic.ClientActivity.MyView
03-22 15:32:21.310: ERROR/AndroidRuntime(1301):     at         android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301):     at     android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301):     at     android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
       03-22 15:32:21.310: ERROR/AndroidRuntime(1301):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301):     at android.os.Looper.loop(Looper.java:123)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301):     at android.app.ActivityThread.main(ActivityThread.java:3683)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301):     at java.lang.reflect.Method.invokeNative(Native Method)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301):     at java.lang.reflect.Method.invoke(Method.java:507)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301):     at dalvik.system.NativeStart.main(Native Method)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class com.DrawTastic.ClientActivity.MyView
03-22 15:32:21.310: ERROR/AndroidRuntime(1301):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301):     at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301):     at android.app.Activity.setContentView(Activity.java:1657)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301):     at com.DrawTastic.GameActivity.onCreate(GameActivity.java:13)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301):     ... 11 more
03-22 15:32:21.310: ERROR/AndroidRuntime(1301): Caused by: java.lang.ClassNotFoundException: com.DrawTastic.ClientActivity.MyView in loader     dalvik.system.PathClassLoader[/data/app/com.DrawTastic-2.apk]
03-22 15:32:21.310: ERROR/AndroidRuntime(1301):     at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301):     at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301):     at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301):     at android.view.LayoutInflater.createView(LayoutInflater.java:471)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
03-22 15:32:21.310: ERROR/AndroidRuntime(1301):     ... 21 more

2 个答案:

答案 0 :(得分:1)

您需要在布局chat.xml中声明这样的自定义视图:

//Notice that i've added .MyView

<com.DrawTastic.ClientActivity.MyView  android:id="@+id/MyView1"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center"/>

另外,您应该覆盖 View类中MyView的其他构造函数,如下所示:

public MyView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}


public MyView(Context context, AttributeSet attrs ) {
    super(context, attrs);
}

编辑:

关于你遇到的问题,你应该在你的Manifest文件中声明你的Activity:

<activity android:name=".YourActivity" />

答案 1 :(得分:0)

您需要<com.DrawTastic.ClientActivity.MyView...,因为它指向您要添加的实际View