我正在尝试将我在 XNA 中使用的类移植到 Android。该类处理读取 XML 文件。
我正在使用 Simple 进行序列化/反序列化,但是我不确定如何设置类以及是否需要 getter/setter。
这是 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;
}
}
}
是的,我确实将代码放入了转换器,但是我对其进行了很多更改(但是尚未删除自动生成的注释)。
我只是想看看如何设置其中一个类,其余的我可能可以自己完成,但是我是一名新手程序员,过去 2 天我一直在搜索和试验,但是有几件事我不知道。