Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android小應用-----畫畫板

Android小應用-----畫畫板

編輯:關於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;
	}

}

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved