I am absolutly new in Android and I am finding some dificulties to do the following thing related to the Context.
So I have an utility class that contain an utility method that create and return a Bitmap image, this is the code of my class:
public class ImgUtility {
/**
* Method that create the images related to the difficulty of a recepy
* @param context
* @param difficulty that represent the number of chef_hat_ok into the final image
* @return a Bitmap representing the difficult of a recepy
*/
public static Bitmap createRankingImg(Context context, int difficulty) {
// Create a Bitmap image starting from the star.png into the "/res/drawable/" directory:
Bitmap myBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.chef_hat_ok);
// Create a new image bitmap having width to hold 5 star.png image:
Bitmap tempBitmap = Bitmap.createBitmap(myBitmap.getWidth() * 5, myBitmap.getHeight(), Bitmap.Config.RGB_565);
/* Attach a brand new canvas to this new Bitmap.
The Canvas class holds the "draw" calls. To draw something, you need 4 basic components:
1) a Bitmap to hold the pixels.
2) a Canvas to host the draw calls (writing into the bitmap).
3) a drawing primitive (e.g. Rect, Path, text, Bitmap).
4) a paint (to describe the colors and styles for the drawing).
*/
Canvas tempCanvas = new Canvas(tempBitmap);
// Draw the image bitmap into the cavas:
tempCanvas.drawBitmap(myBitmap, 0, 0, null);
tempCanvas.drawBitmap(myBitmap, myBitmap.getWidth(), 0, null);
tempCanvas.drawBitmap(myBitmap, myBitmap.getWidth() * 2, 0, null);
tempCanvas.drawBitmap(myBitmap, myBitmap.getWidth() * 3, 0, null);
tempCanvas.drawBitmap(myBitmap, myBitmap.getWidth() * 4, 0, null);
return tempBitmap;
}
}
As you can see this class contains the createRankingImg that take the Context object as parameter and use it to create an image. This object is used to retrieve an image from the resources (into the BitmapFactory.decodeResource() method). What exactly represent the Context object into an Android application?
I know that to obtain the context into an activity class I can use the getResources() method.
My problem is that I have to obtain the context into a class that exetends Fragment.
I have something like this:
public class ScreenSlidePageFragment extends Fragment {
.......................................................................
.......................................................................
.......................................................................
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
.....................................................................
.....................................................................
.....................................................................
switch (mPageNumber + 1) {
case 1:
imgSlideView.setImageResource(R.drawable.carbonara);
((TextView) rootView.findViewById(android.R.id.text1)).setText(getString(R.string.carbonara));
ImageView difficultyContainerImageView1 = (ImageView) rootView.findViewById(R.id.difficultyContainer);
difficultyContainerImageView1.setImageDrawable(new BitmapDrawable(getResources(), ImgUtility.createRankingImg(getApplicationContext(), 3)));
break;
.....................................................................
.....................................................................
.....................................................................
}
return rootView;
}
My problem is that when in the previous fragment class I call the method
ImgUtility.createRankingImg(getResources(), 3)
passing to it the getResources() output (that I thinked give me the Context, the IDE give me the following error message:
Wrong 1st argument type. Found: 'android.content.res.Resources', required: 'android.content.Context'
So it seems to me that into a class that extends a Fragment and not an Activity the getResources() method return a Resources object instead a Context object (as done into an Activity class). Is it true? Why?
How can I obtain the Context inside a class that extends Fragment? And what exactly represent the Context in an Android app?