Android的应用绘图:行不能在一个位图绘制从画廊加载 [英] android drawing app: line cannot be drawn on a bitmap loaded from gallery

查看:184
本文介绍了Android的应用绘图:行不能在一个位图绘制从画廊加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的绘图应用程序,但我不知道为什么,当图像从画廊加载,当它进一步得出,只是绘制的线条将出现在触摸,但是当手指离开屏幕就会消失,即绘制的线不能固定在位图。

会不会有任何人知道如何修改呢?非常感谢!

编码:

 公共类DrawView扩展视图//被画主屏幕
{
   私有静态最终浮动TOUCH_TOLERANCE = 10;   私人位图位图; //绘图区域显示或保存
   私人帆布bitmapCanvas; //用于绘制位图
   私人涂料paintScreen; //用来绘制位图到屏幕上
   私人涂料paintLine; //用来画线位图上
   私人的HashMap<整数,路径和GT;路径映射; //当前路径正在制定
   私人的HashMap<整数,点> previousPointMap; //当前点   公共DrawView(上下文的背景下,ATTRS的AttributeSet)
   {
      超(背景下,ATTRS);
      paintScreen =新的油漆();      //设置默认设置
      paintLine =新的油漆();
      paintLine.setColor(Color.BLACK);
      paintLine.setStyle(Paint.Style.STROKE);
      paintLine.setStrokeWidth(5);
      路径映射=新的HashMap<整数,路径>();
      previousPointMap =新的HashMap<整数,点>();
   }   //的方法OnSizeChanged创建应用程序后显示位图和帆布
   @覆盖
   公共无效onSizeChanged(INT W,INT小时,INT oldW,诠释oldH)
   {
      位图= Bitmap.createBitmap(的getWidth(),的getHeight(),Bitmap.Config.ARGB_8888);
      bitmapCanvas =新的Canvas(位图);
      bitmap.eraseColor(Color.WHITE); //删除白色位图
   }   公共无效load_pic(字符串picturePath)//将图库图片
   {
      pathMap.clear(); //删除所有路径
      previousPointMap.clear(); //删除所有previous点
      位= BitmapFactory.de codeFILE(picturePath);
      无效(); //刷新屏幕
   }   @覆盖
   保护无效的onDraw(帆布油画)
   {
      canvas.drawBitmap(位图,0,0,paintScreen);
      对于(整数键:pathMap.keySet())
         canvas.drawPath(pathMap.get(键),paintLine); //画线
   }   //处理触摸事件
   @覆盖
   公共布尔onTouchEvent(MotionEvent事件)
   {
      INT行动= event.getActionMasked();
      INT的actionIndex = event.getActionIndex(); //指针(即手指)      //确定哪种类型的行动给予MotionEvent重新presents,然后调用相应的处理方法
      如果(动作== || MotionEvent.ACTION_DOWN行动== MotionEvent.ACTION_POINTER_DOWN)
      {
         touchStarted(event.getX(的actionIndex),event.getY(的actionIndex),event.getPointerId(的actionIndex));
      } // 万一
      否则,如果(动作== || MotionEvent.ACTION_UP行动== MotionEvent.ACTION_POINTER_UP)
      {
         touchEnded(event.getPointerId(的actionIndex));
      } //结束否则如果
      其他
      {
         touchMoved(事件);
      } //其他结束      无效(); //重绘
      返回true; //消耗触摸事件
   } // end方法onTouchEvent   //当用户触摸屏幕叫
   私人无效touchStarted(浮法X,浮法Ÿ,诠释另一LineID)
   {
      路径路径; //用于存储路径为给定的触摸标识
      逐点; //用来存储在路径中的最后一个点      //如果已经有对另一LineID路径
      如果(pathMap.containsKey(另一LineID))
      {
         PATH = pathMap.get(另一LineID); //获取路径
         path.reset(); //重新设置路径,因为一个新的触摸开始
         点= previousPointMap.get(另一LineID); //获取路径的最后一点
      } // 万一
      其他
      {
         路径=新路径(); //创建一个新路径
         pathMap.put(另一LineID,路径); //添加路径地图
         点=新点(); //创建一个新点
         previousPointMap.put(另一LineID,点); //点​​添加到地图
      } //其他结束      //移到触摸的坐标
      path.moveTo(X,Y);
      point.x =(INT)X;
      point.y =(INT)Y;
   }......其他的触摸事件相似


解决方案

我用下面的code解决了这个问题。
所需的位图是复制用于编辑的目的。

  =位图(BitmapFactory.de codeFILE(picturePath));
位= bitmap.copy(Bitmap.Config.ARGB_8888,真);
bitmapCanvas =新的Canvas(位图);
无效();

I am working on a drawing app, but i do not know why when the picture is loaded from gallery, when further draw on it, the line just drawn will appear on Touch but will disappear when the finger is off the screen, i.e. the line drawn cannot be fixed onto the Bitmap.

Would there be anyone that know how to modify it? Many thanks!!!

coding:

public class DrawView extends View  // the main screen that is painted
{
   private static final float TOUCH_TOLERANCE = 10;

   private Bitmap bitmap; // drawing area for display or saving
   private Canvas bitmapCanvas; // used to draw on bitmap
   private Paint paintScreen; // use to draw bitmap onto screen
   private Paint paintLine; // used to draw lines onto bitmap
   private HashMap<Integer, Path> pathMap; // current Paths being drawn
   private HashMap<Integer, Point> previousPointMap; // current Points   

   public DrawView(Context context, AttributeSet attrs) 
   {
      super(context, attrs);     
      paintScreen = new Paint(); 

      // set the default settings
      paintLine = new Paint();
      paintLine.setColor(Color.BLACK); 
      paintLine.setStyle(Paint.Style.STROKE);
      paintLine.setStrokeWidth(5);
      pathMap = new HashMap<Integer, Path>();
      previousPointMap = new HashMap<Integer, Point>();
   }

   // Method onSizeChanged creates BitMap and Canvas after app displays
   @Override
   public void onSizeChanged(int w, int h, int oldW, int oldH)
   {
      bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
      bitmapCanvas = new Canvas(bitmap);
      bitmap.eraseColor(Color.WHITE); // erase the BitMap with white
   }       

   public void load_pic(String picturePath) // load a picture from gallery
   {
      pathMap.clear(); // remove all paths
      previousPointMap.clear(); // remove all previous points
      bitmap = BitmapFactory.decodeFile(picturePath);
      invalidate(); // refresh the screen
   }   

   @Override
   protected void onDraw(Canvas canvas) 
   {
      canvas.drawBitmap(bitmap, 0, 0, paintScreen);
      for (Integer key : pathMap.keySet()) 
         canvas.drawPath(pathMap.get(key), paintLine); // draw line
   }

   // handle touch event
   @Override
   public boolean onTouchEvent(MotionEvent event) 
   {
      int action = event.getActionMasked();
      int actionIndex = event.getActionIndex(); // pointer (i.e., finger)

      // determine which type of action the given MotionEvent represents, then call the corresponding handling method
      if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_POINTER_DOWN) 
      {
         touchStarted(event.getX(actionIndex), event.getY(actionIndex), event.getPointerId(actionIndex));
      } // end if
      else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_POINTER_UP) 
      {
         touchEnded(event.getPointerId(actionIndex));
      } // end else if
      else 
      {
         touchMoved(event); 
      } // end else

      invalidate(); // redraw
      return true; // consume the touch event
   } // end method onTouchEvent

   // called when the user touches the screen
   private void touchStarted(float x, float y, int lineID) 
   {
      Path path; // used to store the path for the given touch id
      Point point; // used to store the last point in path

      // if there is already a path for lineID
      if (pathMap.containsKey(lineID)) 
      {
         path = pathMap.get(lineID); // get the Path
         path.reset(); // reset the Path because a new touch has started
         point = previousPointMap.get(lineID); // get Path's last point
      } // end if
      else 
      {
         path = new Path(); // create a new Path
         pathMap.put(lineID, path); // add the Path to Map
         point = new Point(); // create a new Point
         previousPointMap.put(lineID, point); // add the Point to the Map
      } // end else

      // move to the coordinates of the touch
      path.moveTo(x, y);
      point.x = (int) x;
      point.y = (int) y;
   }

...similar for other on Touch event

解决方案

I have solved the problem using the following code. The bitmap needed to be copy for editing purposes.

bitmap = (BitmapFactory.decodeFile(picturePath));
bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);    
bitmapCanvas = new Canvas(bitmap);
invalidate();

这篇关于Android的应用绘图:行不能在一个位图绘制从画廊加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆