Android Open Source - AndroidFramework Android Graphics






From Project

Back to project page AndroidFramework.

License

The source code is released under:

This is free and unencumbered software released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a co...

If you think the Android project AndroidFramework listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.mdobbins.implementation.framework;
// w w w . j a v  a 2  s  . c  o m
import java.io.IOException;
import java.io.InputStream;

import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;

import com.mdobbins.framework.Graphics;
import com.mdobbins.framework.Image;

public class AndroidGraphics implements Graphics {
    AssetManager assets;
    Bitmap frameBuffer;
    Canvas canvas;
    Paint paint;
    Rect srcRect = new Rect();
    Rect dstRect = new Rect();

    public AndroidGraphics(AssetManager assets, Bitmap frameBuffer) {
        this.assets = assets;
        this.frameBuffer = frameBuffer;
        this.canvas = new Canvas(frameBuffer);
        this.paint = new Paint();
    }

    @Override
    public Image newImage(String fileName, ImageFormat format) {
        Config config = null;
        if (format == ImageFormat.RGB565)
            config = Config.RGB_565;
        else if (format == ImageFormat.ARGB4444)
            config = Config.ARGB_4444;
        else
            config = Config.ARGB_8888;

        Options options = new Options();
        options.inPreferredConfig = config;
       
       
        InputStream in = null;
        Bitmap bitmap = null;
        try {
            in = assets.open(fileName);
            bitmap = BitmapFactory.decodeStream(in, null, options);
            if (bitmap == null)
                throw new RuntimeException("Couldn't load bitmap from asset '"
                        + fileName + "'");
        } catch (IOException e) {
            throw new RuntimeException("Couldn't load bitmap from asset '"
                    + fileName + "'");
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
        }

        if (bitmap.getConfig() == Config.RGB_565)
            format = ImageFormat.RGB565;
        else if (bitmap.getConfig() == Config.ARGB_4444)
            format = ImageFormat.ARGB4444;
        else
            format = ImageFormat.ARGB8888;

        return new AndroidImage(bitmap, format);
    }

    @Override
    public void clearScreen(int color) {
        canvas.drawRGB((color & 0xff0000) >> 16, (color & 0xff00) >> 8,
                (color & 0xff));
    }


    @Override
    public void drawLine(int x, int y, int x2, int y2, int color) {
        paint.setColor(color);
        canvas.drawLine(x, y, x2, y2, paint);
    }

    @Override
    public void drawRect(int x, int y, int width, int height, int color) {
        paint.setColor(color);
        paint.setStyle(Style.FILL);
        canvas.drawRect(x, y, x + width - 1, y + height - 1, paint);
    }
   
    @Override
    public void drawARGB(int a, int r, int g, int b) {
        paint.setStyle(Style.FILL);
       canvas.drawARGB(a, r, g, b);
    }
   
    @Override
    public void drawString(String text, int x, int y, Paint paint){
        canvas.drawText(text, x, y, paint);

       
    }
   

    public void drawImage(Image Image, int x, int y, int srcX, int srcY,
            int srcWidth, int srcHeight) {
        srcRect.left = srcX;
        srcRect.top = srcY;
        srcRect.right = srcX + srcWidth;
        srcRect.bottom = srcY + srcHeight;
       
       
        dstRect.left = x;
        dstRect.top = y;
        dstRect.right = x + srcWidth;
        dstRect.bottom = y + srcHeight;

        canvas.drawBitmap(((AndroidImage) Image).bitmap, srcRect, dstRect,
                null);
    }
   
    @Override
    public void drawImage(Image Image, int x, int y) {
        canvas.drawBitmap(((AndroidImage)Image).bitmap, x, y, null);
    }
   
    public void drawScaledImage(Image Image, int x, int y, int width, int height, int srcX, int srcY, int srcWidth, int srcHeight){
       
       
     srcRect.left = srcX;
        srcRect.top = srcY;
        srcRect.right = srcX + srcWidth;
        srcRect.bottom = srcY + srcHeight;
       
       
        dstRect.left = x;
        dstRect.top = y;
        dstRect.right = x + width;
        dstRect.bottom = y + height;
       
   
       
        canvas.drawBitmap(((AndroidImage) Image).bitmap, srcRect, dstRect, null);
       
    }
   
    @Override
    public int getWidth() {
        return frameBuffer.getWidth();
    }

    @Override
    public int getHeight() {
        return frameBuffer.getHeight();
    }
}




Java Source Code List

com.mdobbins.framework.Audio.java
com.mdobbins.framework.Audio.java
com.mdobbins.framework.FileIO.java
com.mdobbins.framework.FileIO.java
com.mdobbins.framework.Game.java
com.mdobbins.framework.Game.java
com.mdobbins.framework.Graphics.java
com.mdobbins.framework.Graphics.java
com.mdobbins.framework.Image.java
com.mdobbins.framework.Image.java
com.mdobbins.framework.Input.java
com.mdobbins.framework.Input.java
com.mdobbins.framework.Music.java
com.mdobbins.framework.Music.java
com.mdobbins.framework.Pool.java
com.mdobbins.framework.Pool.java
com.mdobbins.framework.Screen.java
com.mdobbins.framework.Screen.java
com.mdobbins.framework.Sound.java
com.mdobbins.framework.Sound.java
com.mdobbins.implementation.framework.AndroidAudio.java
com.mdobbins.implementation.framework.AndroidFastRenderView.java
com.mdobbins.implementation.framework.AndroidFileIO.java
com.mdobbins.implementation.framework.AndroidGame.java
com.mdobbins.implementation.framework.AndroidGraphics.java
com.mdobbins.implementation.framework.AndroidImage.java
com.mdobbins.implementation.framework.AndroidInput.java
com.mdobbins.implementation.framework.AndroidMusic.java
com.mdobbins.implementation.framework.AndroidSound.java
com.mdobbins.implementation.framework.MultiTouchHandler.java
com.mdobbins.implementation.framework.SingleTouchHandler.java
com.mdobbins.implementation.framework.TouchHandler.java
com.mdobbins.sample.Assets.java
com.mdobbins.sample.GameScreen.java
com.mdobbins.sample.LoadingScreen.java
com.mdobbins.sample.MainMenuScreen.java
com.mdobbins.sample.SampleGame.java