码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2014-05-18 15:54:33      阅读:318      评论:0      收藏:0      [点我收藏+]

标签:android   blog   class   code   c   java   

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小应用-----画画板,布布扣,bubuko.com

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

标签:android   blog   class   code   c   java   

原文地址:http://blog.csdn.net/andywuchuanlong/article/details/26083773

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!