【发布时间】:2013-09-30 08:52:21
【问题描述】:
我正在做一种靶心的类型,但我使用的是正方形而不是圆形。
但问题是:
一切都完成了,生成其他方块颜色的算法也完成了。
但是我实现了一个按钮,我让它刷新了靶心。
问题是我做不到,需要帮助。
这是 MainActivity,我会从这里检测按钮点击。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FrameLayout frame = (FrameLayout) findViewById(R.id.frame);
Draw draw = new Draw(this);
frame.addView(draw);
Button refresh = (Button) findViewById(R.id.refresh);
refresh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
draw.onDraw(canvas);
frame.addView(draw);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
这是 Draw 活动,这是我用来渲染图像的活动。
public class Draw extends View {
public Draw(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
Paint prop = new Paint();
Random color = new Random();
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
int width = getWidth();
int height = getHeight();
int oriwidth = 0;
int oriheight = 0;
for (int x = 0; x < 20; x++) {
int red = color.nextInt();
int green = color.nextInt();
int blue = color.nextInt();
prop.setARGB(0, red, green, blue);
canvas.drawRect(oriwidth += 10, oriheight += 10, width -= 10,
height -= 10, prop);
}
}
}
有人可以帮助我吗?对不起英语。
【问题讨论】:
-
究竟是什么问题。你提到“问题是我做不到,需要帮助。”是编译问题还是运行时问题?
-
我无法调用onDraw方法再次运行。我想让它看到一个新颜色的新方块。
标签: android image methods call