如题, 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;
}
}
}
1
kifile 2016-08-25 21:41:39 +08:00
首先确定构造器里的 color 是不是为 0 ,透明了吧
|
2
GentleSadness OP @kifile 不是,//canvas.drawRect( 0, 0 ,500,500, mPaint);这个能画出来。。
|
3
nicevar 2016-08-25 21:55:35 +08:00
楼主仔细看 api 啊,你连 drawRect 都没理解好
|
4
nicevar 2016-08-25 21:56:23 +08:00 1
后面两个参数不是 width 和 height ,是 right 和 bottom
|
5
youxiachai 2016-08-25 22:03:39 +08:00 via iPad
我很好奇 ondraw 你的宽高有值?
|
6
GentleSadness OP @nicevar 谢谢啊
|