如何将Context对象转换为类扩展Fragment?

时间:2016-08-20 17:12:55

标签: java android android-fragments android-resources android-context

我是Android中的新手,我发现了一些与上下文有关的事情。

所以我有一个实用程序类,它包含一个实用程序方法,可以创建并返回 Bitmap 图像,这是我的类的代码:

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;

    }

}

正如您所看到的,此类包含 createRankingImg(),它将上下文对象作为参数并使用它来创建图像。此对象用于从资源中检索图像(进入 BitmapFactory.decodeResource()方法)。什么将Context对象直接表示为Android应用程序?

我知道要在活动类中获取上下文,我可以使用 getResources()方法。

我的问题是我必须将一个上下文放到一个代表Fragment的类中。

我有这样的事情:

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

我的问题是,在前一个片段类中,我调用方法:

ImgUtility.createRankingImg(getResources(), 3)

传递 getResources()输出(我认为给我上下文,IDE会给我以下错误消息:

  

错误的第一个参数类型。发现:' android.content.res.Resources',   必需:' android.content.Context'

所以在我看来,在一个扩展 Fragment 而不是 Activity 的类中, getResources()方法会返回一个资源对象而不是 Context 对象(与Activity类一样)。这是真的吗?为什么呢?

如何在扩展Fragment的类中获取Context?究竟什么代表了Android应用中的Context?我错过了什么?我该如何解决这个问题呢?

1 个答案:

答案 0 :(得分:0)

创建一个以Context作为参数的初始函数,以便您可以从activity类传递上下文对象。