Note that there are some explanatory texts on larger screens.

plurals
  1. PODrawing a line in a class which extends "Activity'
    text
    copied!<p>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.</p> <pre><code> 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); } </code></pre> <hr> <p><strong>UPDATE:</strong> </p> <p>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: </p> <pre><code> @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); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload