简体   繁体   中英

Why I can't create repeated images and put these into an ImageView of my application?

I am absolutly new in Android development and I have the followind doubt.

I have to draw images one next to each other into a Canvas object.

So let to do an example of what I neeed:

Into the /res/drawable/ folder of my project I have this icon (I have resized it before put it into my project but I think that this is not so important):

在此处输入图片说明

So I have to put 3 of these icon one next to each other into a specific ImageView of my activity view (adding some white space between an image and the next one).

So I have done something like this:

1) This is the code of my fragment_screen_slide.xml file that represent the layout where these images have to be placed:

<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" />

    <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="horizontal">

            <TextView
                style="@style/HeaderTextStyle"
                android:text="Difficoltà:" />

            <ImageView
                android:id="@+id/starContainer"
                android:layout_width="fill_parent"
                android:scaleType="fitXY"
                android:layout_height="250dp" />

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

In particular these images have to be placed into this ImageView contained in the previous code snippet:

<ImageView
    android:id="@+id/starContainer"
    android:layout_width="fill_parent"
    android:scaleType="fitXY"
    android:layout_height="250dp" />

Here I have a first doubt because Androi Studio report an error on this line:

android:id="@+id/starContainer"

The reported error is a red underline on the @ and when I pass the mouse pointer on it I can read the following error message:

<interface declaration>, <perceable declaration>, AidToken.import or AidTokenTye.package expected, got '@'

It seems strange to me because I am only creatin an id for the ImageView and because I have not problem when Gradle build the project.

Then, into the Java code that actually put the images into the previous ImageView I have done something like this:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

   System.out.println("PAGE NUMBER: "+ mPageNumber + 1);

   // Create the Canvas where the images are putted:
   Canvas difficultyCanvas = creaImgDifficulty();

    switch (mPageNumber + 1) {

    case 1:
        imgSlideView.setImageResource(R.drawable.carbonara);
        ((TextView) rootView.findViewById(android.R.id.text1)).setText(getString(R.string.carbonara));

            difficultyCanvas = creaImgDifficulty();
            ImageView starContainer = (ImageView) rootView.findViewById(R.id.starContainer);
            starContainer.draw(difficultyCanvas);

            break;

        // OTHER CASES
    }



    private Canvas creaImgDifficulty() {
        Canvas canvas;
        Bitmap chefHatOk = BitmapFactory.decodeResource(getResources(), R.star);
        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;
    }

}

So, as you can see in the previous code sippet, when the view is created first I create a new Canvas object (that contains the repeated images) calling the creaImgDifficulty() and then I try to draw this Canvas object into the previous ImageView having id=starContainer .

The creaImgDifficulty() create the Canvas object and draw the bitmap on it 3 times.

I obtain no error when I run the application but the images is not displayed into my ImageView .

Why? What is wrong? What am I missing? How can I try to solve this issue?

XML cannot contain capitals. Switch your ID line to:

android:id="@+id/star_container"

Then change your java file references to match.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM