Android小应用-----画画板

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小应用-----画画板,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。