com.richtodd.android.quiltdesign.block.QuiltDesignThumbnailProvider.java Source code

Java tutorial

Introduction

Here is the source code for com.richtodd.android.quiltdesign.block.QuiltDesignThumbnailProvider.java

Source

/* Copyright (c) 2013 Richard G. Todd.
 * Licensed under the terms of the GNU General Public License (GPL) Version 3.0.
 */

package com.richtodd.android.quiltdesign.block;

import org.json.JSONException;
import org.json.JSONObject;

import com.richtodd.android.repository.ThumbnailProvider;

import android.graphics.Bitmap;

public class QuiltDesignThumbnailProvider implements ThumbnailProvider {

    public Bitmap getThumbnail(PaperPiecedBlock block) {
        RenderOptions renderOptions = new RenderOptions();
        renderOptions.setWidth(200);
        renderOptions.setHeight(200);

        Bitmap bitmap = renderOptions.createBitmap();

        block.draw(bitmap, renderOptions);

        return bitmap;
    }

    public Bitmap getThumbnail(Quilt quilt) {
        RenderOptions renderOptions = new RenderOptions();
        renderOptions.setWidth(200);
        renderOptions.setHeight(200);

        Bitmap bitmap = renderOptions.createBitmap();

        quilt.draw(bitmap, renderOptions);

        return bitmap;
    }

    public Bitmap getThumbnail(Theme theme) {
        return theme.createBitmap(250, 50);
    }

    @Override
    public Bitmap getThumbnail(JSONObject object) {

        String objectType = object.optString("objectType");
        if (objectType.equals("block")) {
            try {
                PaperPiecedBlock block = PaperPiecedBlock.createFromJSONObject(object);
                return getThumbnail(block);
            } catch (JSONException e) {
                e.printStackTrace();
                return null;
            }
        }
        if (objectType.equals("quilt")) {
            try {
                Quilt quilt = Quilt.createFromJSONObject(object);
                return getThumbnail(quilt);
            } catch (JSONException e) {
                e.printStackTrace();
                return null;
            }
        }
        if (objectType.equals("theme")) {
            try {
                Theme theme = Theme.createFromJSONObject(object);
                return getThumbnail(theme);
            } catch (JSONException e) {
                e.printStackTrace();
                return null;
            }
        }

        return null;
    }
}