为什么我不能把这个运行时创建的位图作为内容或声明为片段的 ImageView?



我是Android开发的新手,我正在开发我的第一个教育应用程序,与Udacity的初学者课程有关。

这个应用程序的思想非常简单:它是一个cousine recipes应用程序,在这个应用程序中,用户可以在主活动中从一个容器切换到另一个容器,在主活动中向左或向右切换幻灯片。

这是我的GitHub与此应用程序相关的存储库:https://github.com/AndreaNobili/PastaFromRome

从一个接收交换到另一个我使用的片段,所以基本上我有一个activity_man.xml文件,只包含头和片段的viewpage:

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

然后我有fragment_screen_slide_page.xml表示与单个食谱相关的幻灯片,如下所示:

<!-- Dummy content. -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="0dp">
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="fill_parent"
        android:scaleType="fitXY"
        android:layout_height="250dp" />
        <!--
        android:src="@drawable/carbonara"/>
        android:background="@drawable/carbonara" />
        -->
    <TextView android:id="@android:id/text1"
        style="@style/pastaTitleTextStyle" />
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <LinearLayout
            android:layout_width="0px"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <TextView
                style="@style/HeaderTextStyle"
                android:text="Difficoltà:" />

            <ImageView
                android:id="@+id/difficultyContainer"
                android:layout_width="fill_parent"
                android:scaleType="fitXY"
                android:layout_height="55dp" />
        </LinearLayout>

    </LinearLayout>
</LinearLayout>

该视图是一个片段,当显示时,包含当前配方。

它包含第一个ImageView id imageView1显示与当前配方相关的图像,它工作得很好。

然后它包含第二个ImageView id hardtycontainer ,这是在运行时从应用程序创建的难度排名图像。这就是我要发疯的地方!!

这是处理片段逻辑的ScreenSlidePageFragment类(扩展了Fragment):

/**
 * A fragment representing a single step in a wizard. The fragment shows a dummy title indicating
 * the page number, along with some dummy text.
 *
 */
public class ScreenSlidePageFragment extends Fragment {
    private static final String TAG = "ScreenSlidePageFragment";
    /**
     * The argument key for the page number this fragment represents.
     */
    public static final String ARG_PAGE = "page";
    /**
     * The fragment's page number, which is set to the argument value for {@link #ARG_PAGE}.
     */
    private int mPageNumber;
    private static Context context;
    /**
     * Factory method for this fragment class. Constructs a new fragment for the given page number.
     */
    public static ScreenSlidePageFragment create(int pageNumber, Context baseContext) {
        ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_PAGE, pageNumber);
        fragment.setArguments(args);
        context = baseContext;
        return fragment;
    }
    public ScreenSlidePageFragment() {
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPageNumber = getArguments().getInt(ARG_PAGE);
    }
    /**
     * Method that will be called when the "pasta slider" is slided right or left
     * @param inflater
     * @param container
     * @param savedInstanceState
     * @return
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
       System.out.println("PAGE NUMBER: "+ mPageNumber);
        // Obtain the colosseum_icon.png from the rsources as a Drawable:
        //Drawable drawable = getResources().getDrawable(R.drawable.colosseum_icon);
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_screen_slide_page, container, false);
        ImageView imgSlideView = (ImageView) rootView.findViewById(R.id.imageView1);
        /* Retrieve the ImageView having id="difficultyContainer" (where to put the diifficulty
           image created):
         */
        //ImageView difficultyContainerImageView = (ImageView) rootView.findViewById(R.id.difficultyContainer);
        // Load the 2 images for the creation of the "difficulty graphic":
        Bitmap chefHatWhite = BitmapFactory.decodeResource(getResources(), R.drawable.chef_hat_white);
        Bitmap chefHatOk = BitmapFactory.decodeResource(getResources(), R.drawable.chef_hat_ok);
        // Where the previus image will be drawn:
        //Canvas canvas = new Canvas();
        Canvas difficultyCanvas = creaImgDifficulty();

        //switch (mPageNumber + 1) {
        switch (mPageNumber) {
            case 0:
                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(context, 3)));
                break;
            case 1:
                imgSlideView.setImageResource(R.drawable.amatriciana);
                ((TextView) rootView.findViewById(android.R.id.text1)).setText(getString(R.string.amatriciana));

                break;
            case 2:
                imgSlideView.setImageResource(R.drawable.gricia_m);
                ((TextView) rootView.findViewById(android.R.id.text1)).setText(getString(R.string.gricia));

                break;
            case 3:
                imgSlideView.setImageResource(R.drawable.cacio_e_pepe);
                ((TextView) rootView.findViewById(android.R.id.text1)).setText(getString(R.string.cacio_e_pepe));

                break;
            case 4:
                imgSlideView.setImageResource(R.drawable.ajo_ojo_e_peperoncino);
                ((TextView) rootView.findViewById(android.R.id.text1)).setText(getString(R.string.ajo_ojo_peperoncino));

                break;
        }

        return rootView;
    }

    private void createImg(ImageView imgview) {
        Canvas canvas;
        Bitmap star = BitmapFactory.decodeResource(getResources(), R.drawable.star);
        Bitmap output = Bitmap.createBitmap(32, 32, Bitmap.Config.ARGB_8888);
        canvas = new Canvas(output);
        canvas.drawBitmap(star, star.getWidth() + 2, 0, null);
        imgview.setImageDrawable(new BitmapDrawable(getResources(), output));
    }
    private Canvas creaImgDifficulty() {
        Canvas canvas;
        Bitmap chefHatOk = BitmapFactory.decodeResource(getResources(), R.drawable.chef_hat_ok);
        Bitmap output = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
        canvas = new Canvas(output);
        int space = 10; // the space between images
        for(int i = 0; i < 3; i++) {
            canvas.drawBitmap(chefHatOk, i * (chefHatOk.getWidth() + space), 0, null);
        }
        return canvas;
    }
    /**
     * Returns the page number represented by this fragment object.
     */
    public int getPageNumber() {
        return mPageNumber;
    }
}

所以基本上在onCreateView()方法中(我认为这是在加载当前片段时执行的)我有mPageNumber变量指定与特定配方相关的加载片段还有一个切换案例用于将该配方的特定信息加载到该片段中,例如:

case 0:这是第一个配方命名carbonara所以它把carbonara.png图像(r.d drawable.carbonara)到前面的ImageViewid=imageView1定义到fragment_screen_slide_page.xml文件。

工作正常

当我在前面的代码中执行

时,问题发生了:
ImageView difficultyContainerImageView1 = (ImageView) rootView.findViewById(R.id.difficultyContainer);
difficultyContainerImageView1.setImageDrawable(new BitmapDrawable(getResources(), ImgUtility.createRankingImg(context, 3))); 

您可以在代码中看到, hardtycontainerimageview1 识别ImageView在我的片段中具有id= hardtycontainer 。这里我设置了一个通过 imutility . createrankingimg ()方法创建的图像。

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

}

这个方法应该是好的,因为在我看来,返回一个位图,但这个图像没有显示到片段ImageView具有id= hardtycontainer

进入logcat,我可以看到以下错误日志,但我不知道是否与我的问题有关:

08-21 19:38:12.315 4239-4925/com.example.android.pastafromrome W/OpenGLRenderer: Bitmap too large to be uploaded into a texture (8730x1836, max=4096x4096)
08-21 19:38:12.335 4239-4925/com.example.android.pastafromrome W/OpenGLRenderer: Bitmap too large to be uploaded into a texture (8730x1836, max=4096x4096)
08-21 19:38:12.335 4239-4925/com.example.android.pastafromrome W/OpenGLRenderer: Bitmap too large to be uploaded into a texture (8730x1836, max=4096x4096)

为什么?我错过了什么?我如何解决这个问题?如果你看到我的GitHub库并帮助我修复它,我将非常高兴。

问题是Android会自动调整chef_hat_ok到1746px宽度(我想这取决于设备)。看起来不错,但你做了5个,5 x 1746px是8370,纹理无法承受。

解决方案:禁用缩放或根据密度放置资源(未勾选该选项)。

禁用扩展。

ImgUtility.java

BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
Bitmap myBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.chef_hat_ok, options);

编辑:你需要将密度设置为画布tempCanvas.setDensity(myBitmap.getDensity());

编辑2:我还建议对难度容器做一些改变

<ImageView
      android:id="@+id/difficultyContainer"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:scaleType="centerInside"
      android:adjustViewBounds="true" />

最新更新