Android绘图只是获得黑色视图

时间:2014-03-15 12:36:20

标签: android android-canvas

我正在关注本教程:http://danielnadeau.blogspot.ca/2012/01/android-canvas-beginners-tutorial.html

当我查看"图形布局" Eclipse中的选项卡,我看到了预期的结果(显示中心的红色方块),但是当我实际运行应用程序时,我只得到一个大的黑色矩形,我似乎无法找出原因。我搜索了一些帖子,但没有找到一个似乎适用于我的情况的答案。

MainActivity.java

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

activity_mail.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

        <com.hintonworks.cantstop.CustomView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:id="@+id/my_view" />

</RelativeLayout>

CustomView.java

package com.hintonworks.cantstop;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class CustomView extends SurfaceView implements SurfaceHolder.Callback {

    public CustomView (Context context) {
        super(context);
    }

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

    @Override
    public void onDraw (Canvas canvas) {
        int dCenter = 40;     //Distance from center in px, NOT in dp
                int centerX = (int)(getWidth()/2);
                int centerY = (int)(getHeight()/2);

                Paint paint = new Paint();
                paint.setColor(Color.RED);  //The square will draw in the color blue now

                Rect rect = new Rect(centerX-dCenter, centerY-dCenter, centerX+dCenter, centerY+dCenter);
                canvas.drawRect(rect, paint);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        setWillNotDraw(false);   //Allows us to use invalidate() to call onDraw()
        postInvalidate();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {}

}

感谢您的任何见解。这是我使用画布的尝试,直到现在我一直在使用可绘制的资源。

0 个答案:

没有答案