无法在surfaceview上绘画

时间:2015-02-27 08:19:44

标签: android

我正在尝试使用SurfaceView并想在其上绘画,使用代码:

public class TestPaint extends SurfaceView {
    public TestPaint(Context context) {
        super(context);
        setWillNotDraw(false);
        setBackgroundColor(color.white);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Paint p = new Paint();
        p.setColor(color.black);
        canvas.drawText("test", 10, 10, p);
    }
}
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TestPaint p=new TestPaint(this);
        p.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        LinearLayout rl=(LinearLayout) findViewById(R.id.rltMain);
        rl.addView(p);

    }
}

TestPaint仅显示黑色背景。我试试为什么能得到它?

1 个答案:

答案 0 :(得分:0)

使用paint类更好,然后使用框架布局并设置文本视图或任何你想要的。

检查一下,

<FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <SurfaceView
                android:id="@+id/surfaceView1"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </SurfaceView>
        </LinearLayout>

        <TextView
            android:id="@+id/test"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="top|right"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp"
            android:text="test"
            android:background="@color/abc_search_url_text_normal"
            android:textColor="@android:color/black"
            android:textSize="20sp" />
    </FrameLayout>