简体   繁体   中英

draw after clicking button Android

I have started learning to develop apps for Android OS. I am trying to draw a circle after the user clicks a button. My MainActivity looks like:

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. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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