我已经开始学习为Android OS开发应用程序。我想在用户点击按钮后绘制一个圆圈。我的MainActivity看起来像:
public class MainActivity extends Activity {
EditText editText;
String message;
TextView display;
ImageView transOutput;
Paint paint;
Canvas c;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText1);
//display = (TextView)findViewById(R.id.display);
Button basic_button = (Button) findViewById(R.id.button1);
basic_button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
c = new Canvas();
c.drawColor(Color.CYAN);
// smooths
paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(4.5f);
// opacity
//p.setAlpha(0x80); //
c.drawCircle(50, 50, 30, paint);
}
});
}
我认为将绘图函数放在onClick侦听器中就可以了。最终我将添加更多形状。我错过了什么?
答案 0 :(得分:2)
如果你需要创建一个新的Canvas,那么你必须定义实际绘图的Bitmap。
Bitmap b = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
http://developer.android.com/guide/topics/graphics/2d-graphics.html