在类中绘制一条延伸“活动”的行

时间:2011-01-07 14:49:57

标签: android imageview draw lines

我有一个ImageView在我的Activity中加载一张图片,并希望在其上绘制一些线条。好像我无法将我绘制的线条覆盖到ImageView上。如果我创建一个扩展'View'的新类(myPainter),从活动中挂钩myPainter.onDraw()然后在onCreate()中设置setContentView(mp),我得到的就是绘制的线条。我的ImageView已从onCreate()中删除。如何让ImageView和线条以相同的布局显示?我也为ImageView覆盖了onTouch(),所以我可以从中获取xy点击位置。

    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);
     }

更新:

我想我已经将自己画成了一个角落。我现在如何在onTouchEvent()方法中向此代码添加frameAnimation?就像用户触摸屏幕的地方一样。如果我将代码添加到myPainter()类,则Android rocketAnimation示例会导致异常:

  @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);
  }

2 个答案:

答案 0 :(得分:1)

问题是您要拨打setContentView两次。你的onCreate应该更像:

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

答案 1 :(得分:0)

我试图实现上面的建议,但无法获得我的目标。也许我没有完全解决我的问题,或者只是一般性的误解。我最终在myPainter main方法中添加了一个setBackgroundResource()来获取屏幕上的图像。我想我已经按需要工作并发布下面的代码,以防它帮助其他人。感谢您提供帮助!

    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;
      }

    }

然后我的主应用程序类为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);

}