android Canvas.drawBitmap 方法的理解

邵宜年
2023-12-01
在自定义view中需要绘制出画笔的图片,并且在当前按下的位置实时绘制,
 我的步骤是:(1)获取资源文件的下的画笔bitmap :
               Bitmap bitmapPaint = BitmapFactory.decodeResource(mContext.getResources(), 
             R.mipmap.paint);

             (2)在ondraw中  Canvas.drawBitmap(bitmapPaint,move_x,move_y,  new 
             Paint(Paint.Paint.ANTI_ALIAS_FLAG));实时绘制运行后结果发现:画笔不在当前按下的位置
			 
 经过查看源码发现:
              canvas.drawBitmap(Bitmap bitmap, float left, float top, Paint paint)中			 
					bitmap --- 需要绘制bitmap对象
					left ---- bitmap左边距离当前自定义view 左边的 距离
					top ---- bitmap上边距离当前自定义view 上边的 距离
					paint ---- 绘制的画笔
						
 我要将画笔显示在当前按下的位置点,所以将bitmap向上平移bitmap的高度:
 
   最终使用:
         canvas.drawBitmap(bitmapPaint,move_x,move_y-bitmapPaint.getHeight(),  new 
      Paint(Paint.ANTI_ALIAS_FLAG));

   move_x  --- 当前手指按下的屏幕x坐标
   move_y  --- 当前手指按下的屏幕y坐标

 

 类似资料: