简体   繁体   English

单击按钮后绘制Android

[英]draw after clicking button Android

I have started learning to develop apps for Android OS. 我已经开始学习为Android OS开发应用程序。 I am trying to draw a circle after the user clicks a button. 我试图在用户单击按钮后画一个圆。 My MainActivity looks like: 我的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);

        }
    }); 
}

I thought that placing the drawing functions in the onClick listener would do the trick. 我认为将绘图功能放在onClick侦听器中可以解决问题。 Eventually I will be adding more shapes. 最终,我将添加更多形状。 What am I missing? 我想念什么?

if you need to create a new Canvas, then you must define the Bitmap upon which drawing will actually be performed. 如果需要创建新的Canvas,则必须定义将在其上实际执行绘制的位图。

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 http://developer.android.com/guide/topics/graphics/2d-graphics.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM