Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法实现Android绘图应用程序OnTouchListener_Android_View_Import_Ontouchlistener - Fatal编程技术网

无法实现Android绘图应用程序OnTouchListener

无法实现Android绘图应用程序OnTouchListener,android,view,import,ontouchlistener,Android,View,Import,Ontouchlistener,我正在开发一个绘图应用程序并扩展视图,希望实现OnTouchListener。代码如下: public class DoodleView extends View implements OnTouchListener //ERROR1 { private Bitmap bitmap; // drawing area for display or saving private Canvas bitmapCanvas; // used to draw on bitmap priv

我正在开发一个绘图应用程序并扩展
视图
,希望实现OnTouchListener。代码如下:

public class DoodleView extends View implements OnTouchListener  //ERROR1
{
   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 Path    mPath;
   private Paint   mPaint, circlePaint, outercirclePaint;  
   private ArrayList<Path> paths = new ArrayList<Path>();
   private ArrayList<Path> undonePaths = new ArrayList<Path>();
   private float xleft,xright,xtop,xbottom;
   private float mX, mY;

   // DoodleView constructor initializes the DoodleView
   public DoodleView(Context context) 
   {
      super(context); // pass context to View's constructor
      this.context_new=context;
      this.setOnTouchListener(this); //ERROR2

      paintScreen = new Paint(); // used to display bitmap onto screen

      // set the initial display settings for the painted line
      paintLine = new Paint();
      paintLine.setAntiAlias(true); // smooth edges of drawn line
      paintLine.setColor(Color.BLACK); // default color is black
      paintLine.setStyle(Paint.Style.STROKE); // solid line
      paintLine.setStrokeWidth(5); // set the default line width
      paintLine.setStrokeCap(Paint.Cap.ROUND); // rounded line ends

      mPath = new Path();
      paths.add(mPath);

   } // end DoodleView constructor
public class DoodleView extends View implements View.OnTouchListener {
....
onDraw:

   @Override
   public void onSizeChanged(int w, int h, int oldW, int oldH)
   {
      super.onSizeChanged(w, h, oldW, oldH);
      DoodlzViewWidth = w;     
      DoodlzViewHeight = h;

      bitmap = Bitmap.createBitmap(getWidth(), DoodlzViewHeight, Bitmap.Config.ARGB_8888);

      bitmapCanvas = new Canvas(bitmap);
      bitmap.eraseColor(Color.WHITE); // erase the BitMap with white 
   } 
   @Override
   protected void onDraw(Canvas canvas)
   {
       canvas.drawBitmap(bitmap, 0, 0, paintScreen); // draw the background screen
       // for each path currently being drawn
       for (Path p : paths){canvas.drawPath(p, paintLine);}
       Toast.makeText(getContext(), "ondraw", Toast.LENGTH_SHORT).show();              
   } 
   @Override
   public boolean onTouchEvent(View arg0, MotionEvent event) //ERROR3
   {

          float x = event.getX();
          float y = event.getY();

          switch (event.getAction())
          {
              case MotionEvent.ACTION_DOWN:
                  touchStarted(x, y);
                  invalidate();
                  break;
              case MotionEvent.ACTION_MOVE:
                  touchMoved(x, y);
                  invalidate();
                  break;
              case MotionEvent.ACTION_UP:
                  touchEnded();
                  invalidate();
                  break;
          }
          return true;
    }
onTouchEvent:

   @Override
   public void onSizeChanged(int w, int h, int oldW, int oldH)
   {
      super.onSizeChanged(w, h, oldW, oldH);
      DoodlzViewWidth = w;     
      DoodlzViewHeight = h;

      bitmap = Bitmap.createBitmap(getWidth(), DoodlzViewHeight, Bitmap.Config.ARGB_8888);

      bitmapCanvas = new Canvas(bitmap);
      bitmap.eraseColor(Color.WHITE); // erase the BitMap with white 
   } 
   @Override
   protected void onDraw(Canvas canvas)
   {
       canvas.drawBitmap(bitmap, 0, 0, paintScreen); // draw the background screen
       // for each path currently being drawn
       for (Path p : paths){canvas.drawPath(p, paintLine);}
       Toast.makeText(getContext(), "ondraw", Toast.LENGTH_SHORT).show();              
   } 
   @Override
   public boolean onTouchEvent(View arg0, MotionEvent event) //ERROR3
   {

          float x = event.getX();
          float y = event.getY();

          switch (event.getAction())
          {
              case MotionEvent.ACTION_DOWN:
                  touchStarted(x, y);
                  invalidate();
                  break;
              case MotionEvent.ACTION_MOVE:
                  touchMoved(x, y);
                  invalidate();
                  break;
              case MotionEvent.ACTION_UP:
                  touchEnded();
                  invalidate();
                  break;
          }
          return true;
    }
然而,我真的抓挠我的头,不知道为什么

  • 错误1:实现OnTouchListener报告红色下划线 无法将OnTouchListener解析为类型,建议导入android.view.view。但是我试着点击导入,它仍然不愿意被导入,我看到导入列表,它已经在那里了

  • 错误2:
    类型视图中的方法setOnTouchListener(View.OnTouchListener)不适用于参数(DoodleView)
    ,原因应该是错误1

  • 错误3:
    类型为DoodleView的onTouchEvent(View,MotionEvent)方法必须重写或实现一个超类型方法,也应该是因为错误1


  • 有人知道发生了什么吗?我被困在这里一动不动!非常感谢

    这似乎是一个简单的进口问题。尝试添加

    import android.view.View
    
    如果尚未出现在类中,请按如下方式更改类声明:

    public class DoodleView extends View implements OnTouchListener  //ERROR1
    {
       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 Path    mPath;
       private Paint   mPaint, circlePaint, outercirclePaint;  
       private ArrayList<Path> paths = new ArrayList<Path>();
       private ArrayList<Path> undonePaths = new ArrayList<Path>();
       private float xleft,xright,xtop,xbottom;
       private float mX, mY;
    
       // DoodleView constructor initializes the DoodleView
       public DoodleView(Context context) 
       {
          super(context); // pass context to View's constructor
          this.context_new=context;
          this.setOnTouchListener(this); //ERROR2
    
          paintScreen = new Paint(); // used to display bitmap onto screen
    
          // set the initial display settings for the painted line
          paintLine = new Paint();
          paintLine.setAntiAlias(true); // smooth edges of drawn line
          paintLine.setColor(Color.BLACK); // default color is black
          paintLine.setStyle(Paint.Style.STROKE); // solid line
          paintLine.setStrokeWidth(5); // set the default line width
          paintLine.setStrokeCap(Paint.Cap.ROUND); // rounded line ends
    
          mPath = new Path();
          paths.add(mPath);
    
       } // end DoodleView constructor
    
    public class DoodleView extends View implements View.OnTouchListener {
    ....
    

    这似乎是一个简单的进口问题。尝试添加

    import android.view.View
    
    如果尚未出现在类中,请按如下方式更改类声明:

    public class DoodleView extends View implements OnTouchListener  //ERROR1
    {
       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 Path    mPath;
       private Paint   mPaint, circlePaint, outercirclePaint;  
       private ArrayList<Path> paths = new ArrayList<Path>();
       private ArrayList<Path> undonePaths = new ArrayList<Path>();
       private float xleft,xright,xtop,xbottom;
       private float mX, mY;
    
       // DoodleView constructor initializes the DoodleView
       public DoodleView(Context context) 
       {
          super(context); // pass context to View's constructor
          this.context_new=context;
          this.setOnTouchListener(this); //ERROR2
    
          paintScreen = new Paint(); // used to display bitmap onto screen
    
          // set the initial display settings for the painted line
          paintLine = new Paint();
          paintLine.setAntiAlias(true); // smooth edges of drawn line
          paintLine.setColor(Color.BLACK); // default color is black
          paintLine.setStyle(Paint.Style.STROKE); // solid line
          paintLine.setStrokeWidth(5); // set the default line width
          paintLine.setStrokeCap(Paint.Cap.ROUND); // rounded line ends
    
          mPath = new Path();
          paths.add(mPath);
    
       } // end DoodleView constructor
    
    public class DoodleView extends View implements View.OnTouchListener {
    ....