V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
GentleSadness
V2EX  ›  Android

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

  •  
  •   GentleSadness · 2016-08-25 21:07:02 +08:00 · 8479 次点击
    这是一个创建于 3031 天前的主题,其中的信息可能已经有所发展或是发生改变。

    如题, 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;
    	}
    }
    

    }

    6 条回复    2016-08-25 22:13:10 +08:00
    kifile
        1
    kifile  
       2016-08-25 21:41:39 +08:00
    首先确定构造器里的 color 是不是为 0 ,透明了吧
    GentleSadness
        2
    GentleSadness  
    OP
       2016-08-25 21:47:15 +08:00
    @kifile 不是,//canvas.drawRect( 0, 0 ,500,500, mPaint);这个能画出来。。
    nicevar
        3
    nicevar  
       2016-08-25 21:55:35 +08:00
    楼主仔细看 api 啊,你连 drawRect 都没理解好
    nicevar
        4
    nicevar  
       2016-08-25 21:56:23 +08:00   ❤️ 1
    后面两个参数不是 width 和 height ,是 right 和 bottom
    youxiachai
        5
    youxiachai  
       2016-08-25 22:03:39 +08:00 via iPad
    我很好奇 ondraw 你的宽高有值?
    GentleSadness
        6
    GentleSadness  
    OP
       2016-08-25 22:13:10 +08:00 via Android
    @nicevar 谢谢啊
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   944 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 21ms · UTC 19:42 · PVG 03:42 · LAX 11:42 · JFK 14:42
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.