Android 有数据,但就是没有 Draw 出来,怎么办

2016-08-25 21:07:02 +08:00
 GentleSadness

如题, debug 下是有数据的, onDraw 有运行,但就是没画出来

public class Arkanoid extends View {

private  int defaultSize;
private int color;
private Paint mPaint;
static private List<Brick> mBrick = new ArrayList<Brick>();

public Arkanoid(Context context, AttributeSet attrs)
{
    this(context, attrs, 0);
}

public Arkanoid(Context context)
{
    this(context, null);
}

/**
 * 获得我自定义的样式属性
 *
 * @param context
 * @param attrs
 * @param defStyle
 */
public Arkanoid(Context context, AttributeSet attrs, int defStyle)
{
    super(context, attrs, defStyle);
    setWillNotDraw(false);
    TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.Arkanoid, defStyle, 0);
    int n = a.getIndexCount();
    for (int i = 0; i < n; i++)
    {
        int attr = a.getIndex(i);
        switch (attr)
        {
            case R.styleable.Arkanoid_aacolor:
                // 默认颜色设置为黑色
                color = a.getColor(attr, Color.BLACK);
                break;
        }

    }
    a.recycle();


    mPaint = new Paint();
    mPaint.setColor(color);
	int value_1 = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics());
    int value_2 = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, getResources().getDisplayMetrics());
    int value_3 = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 30, getResources().getDisplayMetrics());
    int value_4 = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 3, getResources().getDisplayMetrics());
    int value_5 = 0;
    int value_6 = value_2 + value_4;


  /*    int value_1 =50;
    int value_2 =10;
    int value_3 =30;
    int value_4 =3;
    int value_5 =0;
    int value_6 =13;*/
    for(int i = 0; i < 3 ; ++i){
        for(int j = 0; j < 4; ++j ){
            Brick x = new Brick(value_1, value_5, value_6, value_3, Color.BLACK);
            mBrick.add(x);
            value_5 = value_5 + value_4;
        }
        value_1 = value_1 + value_2;
    }
    this.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View v)
        {
            Toast.makeText(MainActivity.mainActivity,"d" , Toast.LENGTH_SHORT).show();
        }

    });
}


@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
    // super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    int width = 0;
    int height = 0;

    /**
     * 设置宽度
     */
    int specMode = MeasureSpec.getMode(widthMeasureSpec);
    int specSize = MeasureSpec.getSize(widthMeasureSpec);
    switch (specMode)
    {
        case MeasureSpec.EXACTLY:// 明确指定了
            width = getPaddingLeft() + getPaddingRight() + specSize;
            break;
        case MeasureSpec.AT_MOST:// 一般为 WARP_CONTENT
            width = getPaddingLeft() + getPaddingRight() + (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1000, getResources().getDisplayMetrics());
            break;
    }

    /**
     * 设置高度
     */
    specMode = MeasureSpec.getMode(heightMeasureSpec);
    specSize = MeasureSpec.getSize(heightMeasureSpec);
    switch (specMode)
    {
        case MeasureSpec.EXACTLY:// 明确指定了
            height = getPaddingTop() + getPaddingBottom() + specSize;
            break;
        case MeasureSpec.AT_MOST:// 一般为 WARP_CONTENT
            height = getPaddingTop() + getPaddingBottom() + (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 140, getResources().getDisplayMetrics());
            break;
    }

    setMeasuredDimension(width, height);

}

@Override
protected void onDraw(Canvas canvas)
{

    for(Iterator iter = mBrick.iterator(); iter.hasNext();){
        Brick q = (Brick) iter.next();
		canvas.drawRect(q.x, q.y, q.width, q.height, mPaint);
    }
    //canvas.drawRect( 0, 0 ,500,500, mPaint);
}

class Brick{
	public int x;
	public int y;
	public int width;
	public int height;
	public int color;

	public Brick(int x,int y, int width, int height, int color){
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
		this.color = color;
	}
}

}

8108 次点击
所在节点    Android
6 条回复
kifile
2016-08-25 21:41:39 +08:00
首先确定构造器里的 color 是不是为 0 ,透明了吧
GentleSadness
2016-08-25 21:47:15 +08:00
@kifile 不是,//canvas.drawRect( 0, 0 ,500,500, mPaint);这个能画出来。。
nicevar
2016-08-25 21:55:35 +08:00
楼主仔细看 api 啊,你连 drawRect 都没理解好
nicevar
2016-08-25 21:56:23 +08:00
后面两个参数不是 width 和 height ,是 right 和 bottom
youxiachai
2016-08-25 22:03:39 +08:00
我很好奇 ondraw 你的宽高有值?
GentleSadness
2016-08-25 22:13:10 +08:00
@nicevar 谢谢啊

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/301784

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX