Home

Awesome

JigsawDemo

关于我<br/> 微博:<a href="http://weibo.com/u/1804503012" target="_blank">newtonker</a>  邮箱:<a href="mailto:newtonker@gmail.com" target="_blank">newtonker@gmail.com</a><br/> 协助开发者<br/> Github:<a href="https://github.com/JochimY" target="_blank">JochimY</a>  邮箱:<a href="mailto:jochimyoung@gmail.com" target="_blank">jochimyoung@gmail.com</a>

项目简介

特别说明

效果图

Image of 1

Image of 2 Image of 3

Image of 4

Image of 5

实现原理

1. /asstes/template.xml中定义了模板中各个图片的坐标和环绕规则。

举个例子来讲,如下代码代表了一个完整的拼图模板

<layout
    numOfSlots="3">
    <id>9</id>
    <points>0:0,0.5:0,1:0,0:0.5,0.5:0.5,0:1,0.5:1,1:1</points>
    <polygons>0,1,4,3/3,4,6,5/1,2,7,6</polygons>
</layout>

其中:

Image of 6

2. /utils/ParserHelper.java中定义了解析拼图模板的方式,解析后的拼图模板封装到了HashMap中,根据JigsawType可以得到对应的拼图模板列表。

/**
 * 解析xml
 * @param is
 * @return
 */
private static HashMap<JigsawType, List<TemplateEntity>> parseXml(InputStream is)
{
    List<TemplateEntity> entityList = new ArrayList<>();
    try
    {
        TemplateEntity entity = null;

        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser parser = factory.newPullParser();
        parser.setInput(is, "utf-8");
        int eventType = parser.getEventType();

        while (eventType != XmlPullParser.END_DOCUMENT)
        {
            switch (eventType)
            {
            case XmlPullParser.START_TAG:
                String tagName = parser.getName();
                if (null != tagName && tagName.equals("layout"))
                {
                    entity = new TemplateEntity();
                    int numOfSlots = Integer.parseInt(parser.getAttributeValue(null, "numOfSlots"));
                    entity.setNumOfSlots(numOfSlots);
                }

                if(null != tagName && tagName.equals("id") && null != entity)
                {
                    String id = parser.nextText();
                    entity.setId(Integer.parseInt(id));
                }

                if(null != tagName && tagName.equals("points") && null != entity)
                {
                    String points = parser.nextText();
                    entity.setPoints(points);
                }

                if(null != tagName && tagName.equals("polygons") && null != entity)
                {
                    String polygons = parser.nextText();
                    entity.setPolygons(polygons);
                }
                break;
            case XmlPullParser.END_TAG:
                if(parser.getName().equals("layout"))
                {
                    entityList.add(entity);
                }
                break;
            default:
                break;
            }
            eventType = parser.next();
        }
    }
    catch (XmlPullParserException | IOException e)
    {
        e.printStackTrace();
        return null;
    }

    // 对list解析,并放到SparseArray中
    HashMap<JigsawType, List<TemplateEntity>> hashMap = new HashMap<>();

    List<TemplateEntity> tempList0 = new ArrayList<>();
    List<TemplateEntity> tempList1 = new ArrayList<>();
    List<TemplateEntity> tempList2 = new ArrayList<>();
    List<TemplateEntity> tempList3 = new ArrayList<>();

    for(TemplateEntity temp : entityList)
    {
        switch(temp.getNumOfSlots())
        {
        case 1:
            tempList0.add(temp);
            break;
        case 2:
            tempList1.add(temp);
            break;
        case 3:
            tempList2.add(temp);
            break;
        case 4:
            tempList3.add(temp);
            break;
        default:
            break;
        }
    }

    // 在循环结束后将模版的集合按照键值对方式存放
    hashMap.put(JigsawType.ONE_PHOTO, tempList0);
    hashMap.put(JigsawType.TWO_PHOTO, tempList1);
    hashMap.put(JigsawType.THREE_PHOTO, tempList2);
    hashMap.put(JigsawType.FOUR_PHOTO, tempList3);

    return hashMap;
}

目前存在的问题

License

Copyright 2016 newtonker

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.