Android Open Source - AndroidQuiz Image View Helper






From Project

Back to project page AndroidQuiz.

License

The source code is released under:

MIT License

If you think the Android project AndroidQuiz 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.bignerdranch.android.androidquiz;
/*from w w  w  . j  a v a2 s . c  om*/
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

public class ImageViewHelper {

    public ImageViewHelper() {}

    public enum ROTATION {
        VERTICAL, HORIZONTAL
    }

    public Bitmap flipImage(Bitmap src, ROTATION type) {
        // create new matrix for transformations
        Matrix matrix = new Matrix();
        // if vertical
        if (type == ROTATION.VERTICAL) {
            // y = y * -1
            matrix.preScale(1.0f, -1.0f);
        }
        // if horizonal
        else if (type == ROTATION.HORIZONTAL) {
            // x = x * -1
            matrix.preScale(-1.0f, 1.0f);
            // unknown type
        } else {
            return null;
        }

        // return transformed image

        return Bitmap.createBitmap(src, 0, 0, src.getWidth(),
                src.getHeight(), matrix, true);
    }

    public static Bitmap drawableToBitmap (Drawable drawable) {
        if (drawable instanceof BitmapDrawable) {
            return ((BitmapDrawable)drawable).getBitmap();
        }

        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);

        return bitmap;
    }
}




Java Source Code List

com.bignerdranch.android.androidquiz.ApplicationTest.java
com.bignerdranch.android.androidquiz.CheatActivity.java
com.bignerdranch.android.androidquiz.ImageViewHelper.java
com.bignerdranch.android.androidquiz.QuizActivity.java
com.bignerdranch.android.androidquiz.TrueFalse.java