跳到主要內容

發表文章

目前顯示的是 3月, 2013的文章

[Android]在圖上畫圖與dp<=>px轉換

有兩種做法: 1. 以Canvas畫在指定位址與縮放,再將Canvas所畫的Bitmap拿來用 2. 以Layout檔Inflate,再將View轉為Bitmap拿來用 結論: 第二種方法較快且容易擴充與調整 方法一: Bitmap org = BitmapFactory.decodeResource(getResources(), R.drawable.empty_pinpoint); Bitmap pinPoint = Bitmap.createScaledBitmap(org, dp2px(52), dp2px(68), true); Canvas pinPointCanvas = new Canvas(pinPoint); Bitmap head = BitmapFactory.decodeResource(getResources(), R.drawable.test_head); pinPointCanvas.drawBitmap(head, null, new Rect(dp2px(6), dp2px(3), dp2px(46), dp2px(43)), null); ImageView view = (ImageView) findViewById(R.id.imgResult); view.setImageBitmap(pinPoint); 方法二:         //1. Make a bitmap to draw to.         Bitmap bmp = Bitmap.createBitmap(480, 800, Bitmap.Config.ARGB_8888);         //2. We need a Canvas to handle drawing to the bitmap. Bitmap has no draw methods of its own.         Canvas canvas = new Canvas(bmp);         //3. Get a ref...