Search in sources :

Example 36 with Canvas

use of android.graphics.Canvas in project glide by bumptech.

the class TransformationUtils method applyMatrix.

private static void applyMatrix(@NonNull Bitmap inBitmap, @NonNull Bitmap targetBitmap, Matrix matrix) {
    BITMAP_DRAWABLE_LOCK.lock();
    try {
        Canvas canvas = new Canvas(targetBitmap);
        canvas.drawBitmap(inBitmap, matrix, DEFAULT_PAINT);
        clear(canvas);
    } finally {
        BITMAP_DRAWABLE_LOCK.unlock();
    }
}
Also used : Canvas(android.graphics.Canvas)

Example 37 with Canvas

use of android.graphics.Canvas in project platform_frameworks_base by android.

the class TextViewPerformanceTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    Bitmap mBitmap = Bitmap.createBitmap(320, 240, Bitmap.Config.RGB_565);
    mCanvas = new Canvas(mBitmap);
    ViewGroup.LayoutParams p = new ViewGroup.LayoutParams(320, 240);
    mLabelView = new PerformanceLabelView(mContext);
    mLabelView.setText(mString);
    mLabelView.measure(View.MeasureSpec.AT_MOST | 320, View.MeasureSpec.AT_MOST | 240);
    mLabelView.mySetFrame(320, 240);
    mLabelView.setLayoutParams(p);
    mLabelView.myDraw(mCanvas);
    mPaint = new Paint();
    mCanvas.save();
    mTextView = new PerformanceTextView(mContext);
    mTextView.setLayoutParams(p);
    mTextView.setText(mString);
    mTextView.mySetFrame(320, 240);
    mTextView.measure(View.MeasureSpec.AT_MOST | 320, View.MeasureSpec.AT_MOST | 240);
}
Also used : Bitmap(android.graphics.Bitmap) ViewGroup(android.view.ViewGroup) Canvas(android.graphics.Canvas) Paint(android.graphics.Paint)

Example 38 with Canvas

use of android.graphics.Canvas in project platform_frameworks_base by android.

the class IconTest method testWithBitmap.

@SmallTest
public void testWithBitmap() throws Exception {
    final Bitmap bm1 = Bitmap.createBitmap(100, 200, Bitmap.Config.ARGB_8888);
    final Bitmap bm2 = Bitmap.createBitmap(100, 200, Bitmap.Config.RGB_565);
    final Bitmap bm3 = ((BitmapDrawable) getContext().getDrawable(R.drawable.landscape)).getBitmap();
    final Canvas can1 = new Canvas(bm1);
    can1.drawColor(0xFFFF0000);
    final Canvas can2 = new Canvas(bm2);
    can2.drawColor(0xFF00FF00);
    final Icon im1 = Icon.createWithBitmap(bm1);
    final Icon im2 = Icon.createWithBitmap(bm2);
    final Icon im3 = Icon.createWithBitmap(bm3);
    final Drawable draw1 = im1.loadDrawable(mContext);
    final Drawable draw2 = im2.loadDrawable(mContext);
    final Drawable draw3 = im3.loadDrawable(mContext);
    final Bitmap test1 = Bitmap.createBitmap(draw1.getIntrinsicWidth(), draw1.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    final Bitmap test2 = Bitmap.createBitmap(draw2.getIntrinsicWidth(), draw2.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    final Bitmap test3 = Bitmap.createBitmap(draw3.getIntrinsicWidth(), draw3.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    draw1.setBounds(0, 0, draw1.getIntrinsicWidth(), draw1.getIntrinsicHeight());
    draw1.draw(new Canvas(test1));
    draw2.setBounds(0, 0, draw2.getIntrinsicWidth(), draw2.getIntrinsicHeight());
    draw2.draw(new Canvas(test2));
    draw3.setBounds(0, 0, draw3.getIntrinsicWidth(), draw3.getIntrinsicHeight());
    draw3.draw(new Canvas(test3));
    final File dir = getContext().getExternalFilesDir(null);
    L("writing temp bitmaps to %s...", dir);
    bm1.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(new File(dir, "bitmap1-original.png")));
    test1.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(new File(dir, "bitmap1-test.png")));
    if (!equalBitmaps(bm1, test1)) {
        findBitmapDifferences(bm1, test1);
        fail("bitmap1 differs, check " + dir);
    }
    bm2.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(new File(dir, "bitmap2-original.png")));
    test2.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(new File(dir, "bitmap2-test.png")));
    if (!equalBitmaps(bm2, test2)) {
        findBitmapDifferences(bm2, test2);
        fail("bitmap2 differs, check " + dir);
    }
    bm3.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(new File(dir, "bitmap3-original.png")));
    test3.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(new File(dir, "bitmap3-test.png")));
    if (!equalBitmaps(bm3, test3)) {
        findBitmapDifferences(bm3, test3);
        fail("bitmap3 differs, check " + dir);
    }
}
Also used : Bitmap(android.graphics.Bitmap) Canvas(android.graphics.Canvas) FileOutputStream(java.io.FileOutputStream) File(java.io.File) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 39 with Canvas

use of android.graphics.Canvas in project platform_frameworks_base by android.

the class IconTest method testWithBitmapResource.

@SmallTest
public void testWithBitmapResource() throws Exception {
    final Bitmap res1 = ((BitmapDrawable) getContext().getDrawable(R.drawable.landscape)).getBitmap();
    final Icon im1 = Icon.createWithResource(getContext(), R.drawable.landscape);
    final Drawable draw1 = im1.loadDrawable(mContext);
    final Bitmap test1 = Bitmap.createBitmap(draw1.getIntrinsicWidth(), draw1.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    draw1.setBounds(0, 0, test1.getWidth(), test1.getHeight());
    draw1.draw(new Canvas(test1));
    final File dir = getContext().getExternalFilesDir(null);
    res1.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(new File(dir, "res1-original.png")));
    test1.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(new File(dir, "res1-test.png")));
    if (!equalBitmaps(res1, test1)) {
        findBitmapDifferences(res1, test1);
        fail("res1 differs, check " + dir);
    }
}
Also used : Bitmap(android.graphics.Bitmap) Canvas(android.graphics.Canvas) FileOutputStream(java.io.FileOutputStream) File(java.io.File) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 40 with Canvas

use of android.graphics.Canvas in project platform_frameworks_base by android.

the class IconTest method testParcel.

@SmallTest
public void testParcel() throws Exception {
    final Bitmap originalbits = ((BitmapDrawable) getContext().getDrawable(R.drawable.landscape)).getBitmap();
    final ByteArrayOutputStream ostream = new ByteArrayOutputStream(// guess 50% compression
    originalbits.getWidth() * originalbits.getHeight() * 2);
    originalbits.compress(Bitmap.CompressFormat.PNG, 100, ostream);
    final byte[] pngdata = ostream.toByteArray();
    L("starting testParcel; bitmap: %d bytes, PNG: %d bytes", originalbits.getByteCount(), pngdata.length);
    final File dir = getContext().getExternalFilesDir(null);
    final File originalfile = new File(dir, "parcel-original.png");
    new FileOutputStream(originalfile).write(pngdata);
    ArrayList<Icon> imgs = new ArrayList<>();
    final Icon file1 = Icon.createWithFilePath(originalfile.getAbsolutePath());
    imgs.add(file1);
    final Icon bit1 = Icon.createWithBitmap(originalbits);
    imgs.add(bit1);
    final Icon data1 = Icon.createWithData(pngdata, 0, pngdata.length);
    imgs.add(data1);
    final Icon res1 = Icon.createWithResource(getContext(), R.drawable.landscape);
    imgs.add(res1);
    ArrayList<Icon> test = new ArrayList<>();
    final Parcel parcel = Parcel.obtain();
    int pos = 0;
    parcel.writeInt(imgs.size());
    for (Icon img : imgs) {
        img.writeToParcel(parcel, 0);
        L("used %d bytes parceling: %s", parcel.dataPosition() - pos, img);
        pos = parcel.dataPosition();
    }
    // rewind
    parcel.setDataPosition(0);
    final int N = parcel.readInt();
    for (int i = 0; i < N; i++) {
        Icon img = Icon.CREATOR.createFromParcel(parcel);
        L("test %d: read from parcel: %s", i, img);
        final File testfile = new File(dir, String.format("parcel-test%02d.png", i));
        final Drawable draw1 = img.loadDrawable(mContext);
        if (draw1 == null) {
            fail("null drawable from img: " + img);
        }
        final Bitmap test1 = Bitmap.createBitmap(draw1.getIntrinsicWidth(), draw1.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        draw1.setBounds(0, 0, test1.getWidth(), test1.getHeight());
        draw1.draw(new Canvas(test1));
        try {
            test1.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(testfile));
        } catch (java.io.FileNotFoundException ex) {
            fail("couldn't create test file " + testfile + ": " + ex);
        }
        if (!equalBitmaps(originalbits, test1)) {
            findBitmapDifferences(originalbits, test1);
            fail(testfile + " differs from original: " + originalfile);
        }
    }
}
Also used : Parcel(android.os.Parcel) Canvas(android.graphics.Canvas) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Bitmap(android.graphics.Bitmap) FileOutputStream(java.io.FileOutputStream) File(java.io.File) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Aggregations

Canvas (android.graphics.Canvas)1237 Bitmap (android.graphics.Bitmap)890 Paint (android.graphics.Paint)609 Rect (android.graphics.Rect)257 BitmapDrawable (android.graphics.drawable.BitmapDrawable)192 RectF (android.graphics.RectF)136 PorterDuffXfermode (android.graphics.PorterDuffXfermode)103 Matrix (android.graphics.Matrix)80 Point (android.graphics.Point)76 Drawable (android.graphics.drawable.Drawable)68 BitmapShader (android.graphics.BitmapShader)60 Test (org.junit.Test)46 IOException (java.io.IOException)43 FileOutputStream (java.io.FileOutputStream)40 View (android.view.View)39 Path (android.graphics.Path)38 File (java.io.File)37 SuppressLint (android.annotation.SuppressLint)36 ColorMatrix (android.graphics.ColorMatrix)36 ColorMatrixColorFilter (android.graphics.ColorMatrixColorFilter)33