1

XNAで使用したクラスをAndroidに移植しようとしています。このクラスは、XMLファイルの読み取りを扱います。

Simpleを使用してシリアル化/逆シリアル化を行っていますが、クラスの設定方法や、ゲッター/セッターが必要かどうかがわかりません。

XNAコードは次のとおりです。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace spritesheetanimation
{
    // Animation frame class
    public class Frame
    {
        // Frame Number
        [XmlAttribute("Num")]
        public int Num;

        // Sub Image X positon in the Sprite Sheet
        [XmlAttribute("X")]
        public int X;

        // Sub Image Y positon in the Sprite Sheet
        [XmlAttribute("Y")]
        public int Y;

        // Sub Image Width
        [XmlAttribute("Width")]
        public int Width;

        // Sub Image Height
        [XmlAttribute("Height")]
        public int Height;

        // The X offset of sub image
        [XmlAttribute("OffSetX")]
        public int OffsetX;

        // The Y offset fo sub image
        [XmlAttribute("OffsetY")]
        public int OffsetY;

        // The duration between two frames
        [XmlAttribute("Duration")]
        public float Duration;
    }

    // Animaiton class to hold the name and frames
    public class Animation
    {
        // Animation Name
        [XmlAttribute("Name")]
        public string Name;

        // Animation Frame Rate
        [XmlAttribute("FrameRate")]
        public int FrameRate;

        public bool Loop;

        public bool Pingpong;

        // The Frames array in an animation
        [XmlArray("Frames"), XmlArrayItem("Frame", typeof(Frame))]
        public Frame[] Frames;
    }

    // The Sprite Texture stores the Sprite Sheet path.fr
    public class SpriteTexture
    {
        // The Sprite Sheet texture file path
        [XmlAttribute("Path")]
        public string Path;
    }

    // Aniamtion Set contains the Sprite Texture and Animaitons.
    [XmlRoot("Animations")]
    public class AnimationSet
    {
        // The sprite texture object
        [XmlElement("Texture", typeof(SpriteTexture))]
        public SpriteTexture SpriteTexture;

        // The animation array in the Animation Set
        [XmlElement("Animation", typeof(Animation))]
        public Animation[] Animations;
    }

    // Sprite Animation Manager class
    public static class SpriteAnimationManager
    {
        public static int AnimationCount;

        // Read the Sprite Sheet Description information from the description xml file
        public static AnimationSet Read(string Filename)
        {
            AnimationSet animationSet = new AnimationSet();

            // Create a XML reader for the sprite sheet animaiton description file
            using (System.Xml.XmlReader reader = System.Xml.XmlReader.Create(Filename))
            {
                // Create a XMLSerializer for the AnimationSet
                XmlSerializer serializer = new XmlSerializer(typeof(AnimationSet));

                // Deserialize the Animation Set from the XmlReader to the animation set object
                animationSet = (AnimationSet)serializer.Deserialize(reader);
            }

            // Count the animations to Animation Count
            AnimationCount = animationSet.Animations.Length;

            return animationSet;
        }
    }
}

これまでの私のコードと比較するには:

package com.example.sprites;
import java.io.InputStream;

import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementArray;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
import org.xmlpull.v1.XmlSerializer;


public class SpriteAnimationManag implements
java.io.Serializable {
   // Animation frame class



   @Element(name = "Frame")
   public class Frame(int num, int x, int y, int width, int height,int offsetx,int offsety, int duration )
   {
      // Frame Number
   //C# TO JAVA CONVERTER TODO TASK: Java annotations will not correspond to .NET attributes:
      //[XmlAttribute("Num")]
      @Element(name = "Num")
      public int Num;

      // Sub Image X positon in the Sprite Sheet
   //C# TO JAVA CONVERTER TODO TASK: Java annotations will not correspond to .NET attributes:
      //[XmlAttribute("X")]
      @Element(name = "X")
      public int X;

      // Sub Image Y positon in the Sprite Sheet
   //C# TO JAVA CONVERTER TODO TASK: Java annotations will not correspond to .NET attributes:
      //[XmlAttribute("Y")]
      @Element(name = "Y")
      public int Y;

      // Sub Image Width
   //C# TO JAVA CONVERTER TODO TASK: Java annotations will not correspond to .NET attributes:
      //[XmlAttribute("Width")]
      @Element(name = "Width")
      public int Width;

      // Sub Image Height
   //C# TO JAVA CONVERTER TODO TASK: Java annotations will not correspond to .NET attributes:
      //[XmlAttribute("Height")]
      @Element(name = "Height")
      public int Height;

      // The X offset of sub image
   //C# TO JAVA CONVERTER TODO TASK: Java annotations will not correspond to .NET attributes:
      //[XmlAttribute("OffSetX")]
      @Element(name = "OffSetX")
      public int OffsetX;

      // The Y offset fo sub image
   //C# TO JAVA CONVERTER TODO TASK: Java annotations will not correspond to .NET attributes:
      //[XmlAttribute("OffsetY")]
      @Element(name = "OffSetY")
      public int OffsetY;

      // The duration between two frames
   //C# TO JAVA CONVERTER TODO TASK: Java annotations will not correspond to .NET attributes:
      //[XmlAttribute("Duration")]
      @Element(name = "Duration")
      public float Duration;
   }

   // Animaiton class to hold the name and frames
   public class Animation implements
   java.io.Serializable 
   {
      // Animation Name
   //C# TO JAVA CONVERTER TODO TASK: Java annotations will not correspond to .NET attributes:
      //[XmlAttribute("Name")]
      @Element(name = "Name")
      public String Name;

      // Animation Frame Rate
   //C# TO JAVA CONVERTER TODO TASK: Java annotations will not correspond to .NET attributes:
      //[XmlAttribute("FrameRate")]
      @Element(name = "FrameRate")
      public int FrameRate;


      public boolean Loop;

      public boolean Pingpong;

      // The Frames array in an animation
   //C# TO JAVA CONVERTER TODO TASK: Java annotations will not correspond to .NET attributes:
      //[XmlArray("Frames"), XmlArrayItem("Frame", typeof(Frame))]
      @ElementArray(name = "Frames") 
      public Frame[] Frames;
   }

   // The Sprite Texture stores the Sprite Sheet path.fr
   public class SpriteTexture implements
   java.io.Serializable 
   {
      // The Sprite Sheet texture file path
   //C# TO JAVA CONVERTER TODO TASK: Java annotations will not correspond to .NET attributes:
      //[XmlAttribute("Path")]
      @Element(name = "path")
      public String Path;
   }

   // Aniamtion Set contains the Sprite Texture and Animaitons.
   //C# TO JAVA CONVERTER TODO TASK: Java annotations will not correspond to .NET attributes:
   //[XmlRoot("Animations")]
   @Root
   public static class XNAAnimationSet implements
    java.io.Serializable
   {
      // The sprite texture object
   //C# TO JAVA CONVERTER TODO TASK: Java annotations will not correspond to .NET attributes:
      //[XmlElement("Texture", typeof(SpriteTexture))]
      @Element(name = "Texture")
      public SpriteTexture SpriteTexture;

      // The animation array in the Animation Set
   //C# TO JAVA CONVERTER TODO TASK: Java annotations will not correspond to .NET attributes:
      //[XmlElement("Animation", typeof(Animation))]
      @ElementArray(name = "Animation")
      public Animation[] Animations;
   }

   // Sprite Animation Manager class
   public final static class SpriteAnimationManager implements
    java.io.Serializable
   {
      private static final String XNAAnimationSet = null;//was static private static
      public static int AnimationCount;

      // Read the Sprite Sheet Description information from the description xml file
      public static XNAAnimationSet Read(InputStream inputStream) throws Exception
      {

          XNAAnimationSet animationSet = new XNAAnimationSet();


         // Create a XML reader for the sprite sheet animaiton description file
   //C# TO JAVA CONVERTER NOTE: The following 'using' block is replaced by its Java equivalent:
//       using (System.Xml.XmlReader reader = System.Xml.XmlReader.Create(Filename))
         //System.Xml.XmlReader reader = System.Xml.XmlReader.Create(Filename);
         //XMLReader reader = XMLReaderFactory.createXMLReader(Filename);//i added this 


            // Create a XMLSerializer for the AnimationSet
            //XmlSerializer serializer = new XmlSerializer(AnimationSet);
                //i added below using simple//
             Serializer serializer = new Persister();
             //serializer.read(AnimationSet, Filename);
             try {
               animationSet = serializer.read(XNAAnimationSet.class, inputStream );
            } 
             catch (Exception e)
             {
               // TODO Auto-generated catch block
               e.printStackTrace();
            }
            ///////////////
            // Desesrialize the Animation Set from the XmlReader to the animation set object
         // animationSet = (AnimationSet)serializer1.Deserialize(reader);
            //serializer.write(AnimationSet, animationSet);
         //}
         //finally
         //{
            //reader.dispose(); //doesn'twork
      // }

         // Count the animations to Animation Count
         AnimationCount = animationSet.Animations.length;

         return animationSet;
      }
   }
}

そして、はい、私はコードをコンバーターに通しましたが、それを大幅に変更しました(ただし、自動生成されたコメントはまだ削除されていません)。

クラスの1つを設定する方法を知りたいだけで、残りは自分でできると思いますが、私は初心者プログラマーであり、過去2日間検索と実験を行ってきましたが、わからないことがいくつかあります。

4

1 に答える 1

0

simple-xml が機能する方法は、クラスにタグで注釈を付けてから、関数を呼び出して実際にデータをシリアル化することです。トピックに関する私のブログ投稿からこの例を見てください。

// Create the Dummy Level with Apples and Oranges
Log.i(TAG, "Create level");
Level level = new Level();
Orange orange;
level.gameObjects.add(new Apple(1, 10));
orange = new Orange(2);
orange.containedJuice = 20;
level.gameObjects.add(orange);
orange = new Orange(3);
orange.containedJuice = 100;
level.gameObjects.add(orange);
level.gameObjects.add(new Apple(4, 0));

// Now write the level out to a file
Log.i(TAG, "Write Level to file.");
Serializer serial = new Persister();
File sdcardFile = new File("/sdcard/levelout.xml");

try {
   serial.write(level, sdcardFile);
} catch (Exception e) {
   // There is the possibility of error for a number of reasons. Handle this appropriately in your code
   e.printStackTrace();
}
Log.i(TAG, "XML Written to File: " + sdcardFile.getAbsolutePath());

実際には、ここには 3 つのステップしかありません。

  1. 「レベル」オブジェクトを作成します。
  2. 「Serializer」オブジェクトを作成し、xml を書き込む場所を指定します。
  3. 実際に「serial.write」関数を使用して XML を記述します。

読み取りを実行するには、注釈が付けられた「Level」クラスが必要ですが、代わりに「Serializer」クラスで「read」関数を呼び出すと、クラスが構築されます。これがあなたの質問に答えることを願っています。

于 2012-11-17T05:44:24.930 に答える