Drawing a line in a class which extends "Activity'

拜拜、爱过 提交于 2019-12-11 08:09:00

问题


I have an ImageView loading a picture in my Activity and want to draw some lines on it. It seems like I'm not able to overlay the lines I draw onto the ImageView. If I create a new class (myPainter) which extends 'View', hook myPainter.onDraw() and then setContentView(mp) in onCreate() from the activity, all I get are the drawn lines. My ImageView is gone from onCreate(). How do I get the ImageView and lines to show up in the same layout? I'm also Override'ing onTouch() for the ImageView so I can get the xy click locations from it.

    public class myPainter extends View
    {
    public myPainter (Context context)
       {
        super (context);
       }

     @Override
       protected void onDraw(Canvas canvas) 
     {
      super.onDraw (canvas);
      Paint p = new Paint();
          p.setColor(Color.RED);

         canvas.drawLine (E_XMIN, E_YMIN, E_XMAX, E_YMAX, p);
     }
    }


     @Override
     public void onCreate (Bundle savedInstanceState)
     {
      super.onCreate (savedInstanceState);
      setContentView (R.layout.main);
      ImageView iv = (ImageView) findViewById(R.id.ImageView01);
      iv.setOnTouchListener (this);

// this draws my lines, but I loose the pic above ^^^
      myPainter mp = new myPainter(this);        
      setContentView(mp);
     }

UPDATE:

I think I've literally painted myself into a corner. How would I add a frameAnimation to this code now in the onTouchEvent() method? Like where the user touches the screen. The Android rocketAnimation example causes an exception if I add code to the myPainter() class:

  @Override
  public boolean onTouchEvent (MotionEvent event)
  {

        //Log.d (TAG, "touchevent in myPainter");
        switch (event.getAction())
        {
            case MotionEvent.ACTION_DOWN:
                    Log.d (TAG, "down x:" + event.getX() + " y:" + event.getY());

                    int wClick = getClick (event.getX(), event.getY());


                    AnimationDrawable touchAni;
                    ImageView iv = (ImageView) findViewById (R.drawable.triangle);
                    iv.setBackgroundResource (R.drawable.nova);
                    touchAni = (AnimationDrawable) iv.getBackground ();
                   touchAni.start ();

                   return true;
        }

       return super.onTouchEvent (event);
  }

回答1:


The problem is you are calling setContentView twice. Your onCreate should be more like:

     @Override
     public void onCreate (Bundle savedInstanceState)
     {
      super.onCreate (savedInstanceState);
      setContentView (R.layout.main);
      ImageView iv = (ImageView) findViewById(R.id.ImageView01);
      iv.setOnTouchListener (this);
     }



回答2:


I tried to implement the suggestions above, but could not get the functionality I was aiming for. Perhaps I've not framed my question completely, or just a general misunderstanding. I ended up adding a setBackgroundResource() in the myPainter main method to get the image on the screen. I think I've got it working as needed and posting the code below in case it helps someone else. Thanks for offering your help!

    class myPainter extends View implements myConst
    {
        private final static String TAG = "** myPainter **";

        public myPainter (Context context)
       {
           super (context);       
           Log.d (TAG, "new myPainter");
           setBackgroundResource (R.drawable.triangle);
       }

        @Override
       protected void onDraw(Canvas canvas) 
        {
            super.onDraw (canvas);     
            Log.d (TAG, "new myPainter");

            Paint p = new Paint ();
           p.setColor (Color.RED);
          canvas.drawLine (E_XMIN, E_YMIN, E_XMAX, E_YMIN, p);
        }


       /** 
         * called to determine what part of the triangle was clicked
         * @param x
         * @param y
         * @return click id from myConst
         */
        private int getClick (float x,  float y)
        {
            int id = 0;
                // insert logic to test x,y locations and return
                // region ID of what was clicked
                ...

            return (id);
        }

      @Override
      public boolean onTouchEvent (MotionEvent event)
      {
            Log.d (TAG, "touchevent in myPainter");
switch (event.getAction())
            {
                case MotionEvent.ACTION_DOWN:
                        Log.d (TAG, "down x:" + event.getX() + " y:" + event.getY());

                        int wClick = getClick (event.getX(), event.getY());
                break;
            }

           return false;
      }

    }

And then my main application class has this for onCreate():

/** Called when the activity is first created. */
@Override
public void onCreate (Bundle savedInstanceState)
{
    super.onCreate (savedInstanceState);
    setContentView (R.layout.main);

    myPainter mp = new myPainter(getApplicationContext());
    mp.invalidate ();
    setContentView(mp);

    //ImageView iv = (ImageView) findViewById(R.id.ImageView01);
    //iv.setOnTouchListener (this);

}


来源:https://stackoverflow.com/questions/4626692/drawing-a-line-in-a-class-which-extends-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!