0

I am trying to make an ImageView be a surface that I can draw on with my finger.

I have followed Android 2D Graphics Example, which resulted in making a whole screen drawable (with some issues, discussed in question 8287949, but somewhat disconnected from what I am trying to do.)

I am working with:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceStatA);
    // getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    // requestWindowFeature(Window.FEATURE_NO_TITLE);
    drawView = new DrawView(this);
    drawView.requestFocus();
    setContentView(R.layout.activity_main);
}

I'm thinking I need to change drawView = new DrawView(this) to something that would reference the Context of the resource I define in activity_main.xml:

ImageView
    android:id="@+id/canvasview"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/btnRed"
    android:background="@drawable/kitty"

However, I either don't know what I'm looking for in the API reference, or I'm not finding it.

The class for DrawView is documented at Android 2D Graphics Example, so I won't repeat it here.

I have been working through AppInventor, and trying to duplicate what I learn there in Eclipse using the real Android SDK, but I'm feeling stumped on Tutorial #1, so sorry for the newbie question!

Community
  • 1
  • 1
Tim AtLee
  • 917
  • 1
  • 10
  • 17

1 Answers1

5

The Context used to create the View is actually the Activity so it's already assigned to the this reference. It's set when you call setContentView().

Also, View#getContext().

So to retrieve the Context you would to get a reference to the View then call getContext(). In this specific case, it would be

ImageView img = (ImageView) findViewById(R.id.canvasview);
Context c = img.getContext();
DeeV
  • 35,865
  • 9
  • 108
  • 95
  • Ah, thanks. It looks like `Context c = img.getContext();` was throwing a NullPointerException, which was solved by putting `setContentView(R.layout.activity_main);` above the call to `getContext`. The DrawView class is getting instantiated, but now the onTouchHandler event isn't getting fired, so I'm looking at that. Thanks for the help! – Tim AtLee Nov 01 '12 at 16:11