Android应用程序运行但画布上没有任何内容

时间:2013-04-01 18:54:32

标签: android eclipse android-canvas

我编写了一个Compass示例,该示例来自Android应用程序教程书并编写了自定义视图,方法等,但在应用程序运行时屏幕上没有任何内容。

这是班级:

package com.example.compass;

import android.view.View;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.accessibility.AccessibilityEvent;

public class CompassView extends View {

private float bearing;
private Paint markerPaint;
private Paint textPaint;
private Paint circlePaint;
private String northString;
private String eastString;
private String southString;
private String westString;
private int textHeight;

public CompassView(Context context) {
    super(context);
    initCompassView();
}

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

public CompassView(Context context, AttributeSet ats, int defaultStyle) {
    super(context, ats, defaultStyle);
    initCompassView();
}

protected void initCompassView() {
    setFocusable(true);

    Resources r = this.getResources();

    circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    circlePaint.setColor(r.getColor(R.color.background_color));
    circlePaint.setStrokeWidth(1);
    circlePaint.setStyle(Paint.Style.FILL_AND_STROKE);

    northString = r.getString(R.string.cardinal_north);
    eastString = r.getString(R.string.cardinal_east);
    southString = r.getString(R.string.cardinal_south);
    westString = r.getString(R.string.cardinal_west);

    textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    textPaint.setColor(r.getColor(R.color.text_color));
    textHeight = (int)textPaint.measureText("yY");

    markerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    markerPaint.setColor(r.getColor(R.color.marker_color));
}

@Override
protected void onDraw(Canvas canvas) {
    int mMeasuredWidth = getMeasuredWidth();
    int mMeasuredHeight = getMeasuredHeight();

    int px = mMeasuredWidth/2;
    int py = mMeasuredHeight/2;

    int radius = Math.min(px, py);

    canvas.drawCircle(px, py, radius, circlePaint);

    canvas.save();
    canvas.rotate(-bearing, px, py);

    int textWidth = (int) textPaint.measureText("W");
    int cardinalX = px-textWidth/2;
    int cardinalY = py-radius+textHeight;

    for (int i = 0; i < 24; i++) {
        canvas.drawLine(px, py-radius, px, py-radius+10, markerPaint);
        canvas.save();
        canvas.translate(0, textWidth);

        if (i%6 == 0) {
            String dirString = "";
            switch (i) {
            case(0):{
                dirString = northString;
                int arrowY = 2*textHeight;
                canvas.drawLine(px, arrowY, px-5, 3*textHeight, markerPaint);
                canvas.drawLine(px, arrowY, px+5, 3*textHeight, markerPaint);
                break;
                }
                case(6): dirString = eastString; break;
                case(12): dirString = southString; break;
                case(18): dirString = westString; break;
            }
            canvas.drawText(dirString, cardinalX, cardinalY, textPaint);
        }

        else if (i%3==0) {
            String angle = String.valueOf(i*15);
            float angleTextWidth = textPaint.measureText(angle);

            int angleTextX = (int)(px-angleTextWidth/2);
            int angleTextY = py-radius+textHeight;
            canvas.drawText(angle, angleTextX, angleTextY, textPaint);
        }
        canvas.restore();
        canvas.rotate(15, px, py);
    }
    canvas.restore();
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int measuredWidth = measure(widthMeasureSpec);
    int measuredHeight = measure(heightMeasureSpec);

    int d = Math.min(measuredWidth, measuredHeight);

    setMeasuredDimension(d, d);
}

private int measure(int measureSpec) {
    int result = 0;

    int specMode = MeasureSpec.getMode(measureSpec);
    int specSize = MeasureSpec.getMode(measureSpec);

    if (specMode == MeasureSpec.UNSPECIFIED) {
        result = 200;
    } 
    else {
        result = specSize;
    }

    return result;
}

public void setBearing(float _bearing) {
    bearing = _bearing;
    sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED);
}

public float getBearing() {
    return bearing;
}

@SuppressWarnings("deprecation")
@Override
public boolean dispatchPopulateAccessibilityEvent(final AccessibilityEvent event) {
    super.dispatchPopulateAccessibilityEvent(event);
    if (isShown()) {
        String bearingStr = String.valueOf(bearing);
        if (bearingStr.length() > AccessibilityEvent.MAX_TEXT_LENGTH)
            bearingStr = bearingStr.substring(0, AccessibilityEvent.MAX_TEXT_LENGTH);

        event.getText().add(bearingStr);
        return true;
    }
    else
        return false;
}
}

我没有收到任何logcat错误;然而它显示了这个(可能是相关的):

04-01 18:41:57.163: D/gralloc_goldfish(583): Emulator without GPU emulation detected.

然而,在看到这个之后,我将GPU仿真硬件添加到我的模拟器并选择了“是”选项,但此消息一直显示。

什么可能是屏幕上没有任何显示的原因?如果需要,我也可以发布xml文件。

1 个答案:

答案 0 :(得分:0)

找出原因:第二次调用getMode而不是getSize ......我现在感到愚蠢。