自定义绘制视图未显示

时间:2017-07-17 11:57:39

标签: java android

我创建了一个自定义视图来绘制一些形状,但我的问题是视图上没有显示任何内容。我没有收到任何错误消息或崩溃。我的自定义类看起来像这样:

public class DrawableView extends View {
  private Paint mPaint;
  private MyDrawable mDrawableObj; // object to draw
  private RectF mRectBounds;

public DrawableView(Context context) {
    super(context);
    init();
}

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

public DrawableView(Context context, AttributeSet attrs, int defStyleAttr){
    super(context, attrs, defStyleAttr);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int w = MeasureSpec.getSize(widthMeasureSpec);
    int h = MeasureSpec.getSize(heightMeasureSpec);

    setMeasuredDimension(w, h);
}

public void setDrawableObj(MyDrawable obj, boolean forcePrepare) {
    final boolean needPrepare = forcePrepare || (mDrawableObj != null) ;
    mDrawableObj = obj;
    mDrawableObj.setView(this);
    if (needPrepare) {
        prepareObject();
        invalidate();
    }
}

private void init() {
    mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mPaint.setColor(Color.BLUE);
    mPaint.setStyle(Paint.Style.STROKE);
}

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

    final int padLeft = getPaddingLeft();
    final int padTop = getPaddingTop();
    final float xpad = (float) (padLeft + getPaddingRight());
    final float ypad = (float) (padTop + getPaddingBottom());

    final float ww = (float) w - xpad - 1;
    final float hh = (float) h - ypad - 1;

    mRectBounds = new RectF(0, 0, ww, hh);
    mRectBounds.offsetTo(padLeft, padTop);

    prepareObject();
}

public void prepareObject() {
    if (mDrawableObj != null)
        mDrawableObj.prepare(mRectBounds);
}

@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (mDrawableObj != null)
        mDrawableObj.draw(canvas, mRectBounds);
    else {
        canvas.drawRect(mRectBounds, mPaint);
    }
}

我的布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/mainLayout"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="com.hello.myApp.Draw">

<DrawableView.DrawableView
    android:id="@+id/drawtest_drawableView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/toolbar"
    app:layout_constraintLeft_toRightOf="@+id/leftMenu"
    android:padding="4dp" />

</android.support.constraint.ConstraintLayout>

你知道我为什么只有白屏吗?

2 个答案:

答案 0 :(得分:0)

使用此

    public void init() {
         if (isInEditMode())
                return;
         //code
         //...
    }

答案 1 :(得分:0)

我将您的代码复制/粘贴到我的IDE中并运行它(尽管我必须创建一个假的MyDrawable接口来满足编译器)。我只是将DrawableView的一个实例放入LinearLayout,我认为它的绘制正常。

我的布局xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/myLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.example.stackoverflow.DrawableView
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_margin="12dp"/>

</LinearLayout>

我所看到的:

enter image description here

我只看到蓝色矩形是有道理的,因为这是你的onDraw()实现:

@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (mDrawableObj != null)
        mDrawableObj.draw(canvas, mRectBounds);
    else {
        canvas.drawRect(mRectBounds, mPaint);
    }
}

鉴于您发布的代码中从不调用setDrawableObj(MyDrawable obj, boolean forcePrepare)else始终是执行/绘制的内容。