public class MainActivity extends Activity { private ImageView iv; float startX = 0; float startY = 0; //获得一个可以被bitmap Bitmap bitmap = Bitmap.createBitmap(400, 400,Bitmap.Config.ARGB_8888); //创建画布 Canvas canvas = new Canvas(bitmap); //创建画笔 Paint paint = new Paint(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv = (ImageView) this.findViewById(R.id.iv); paint.setColor(Color.BLACK); paint.setStrokeWidth(1);//设置像素 iv.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: startX = event.getX(); startY = event.getY(); break; case MotionEvent.ACTION_MOVE: float newX = event.getX(); float newY = event.getY(); canvas.drawLine(startX, startY, newX, newY, paint); iv.setImageBitmap(bitmap); break; case MotionEvent.ACTION_UP: break; default: break; } return true; } }); } @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; } }
Android小应用-----画画板
时间: 2024-11-08 07:34:39