Search in sources :

Example 31 with Matrix

use of android.graphics.Matrix in project android_frameworks_base by ResurrectionRemix.

the class CursorAnchorInfoTest method testMatrixIsCopied.

@SmallTest
public void testMatrixIsCopied() throws Exception {
    final Matrix MATRIX1 = new Matrix();
    MATRIX1.setTranslate(10.0f, 20.0f);
    final Matrix MATRIX2 = new Matrix();
    MATRIX2.setTranslate(110.0f, 120.0f);
    final Matrix MATRIX3 = new Matrix();
    MATRIX3.setTranslate(210.0f, 220.0f);
    final Matrix matrix = new Matrix();
    final Builder builder = new Builder();
    matrix.set(MATRIX1);
    builder.setMatrix(matrix);
    matrix.postRotate(90.0f);
    final CursorAnchorInfo firstInstance = builder.build();
    assertEquals(MATRIX1, firstInstance.getMatrix());
    matrix.set(MATRIX2);
    builder.setMatrix(matrix);
    final CursorAnchorInfo secondInstance = builder.build();
    assertEquals(MATRIX1, firstInstance.getMatrix());
    assertEquals(MATRIX2, secondInstance.getMatrix());
    matrix.set(MATRIX3);
    assertEquals(MATRIX1, firstInstance.getMatrix());
    assertEquals(MATRIX2, secondInstance.getMatrix());
}
Also used : Matrix(android.graphics.Matrix) Builder(android.view.inputmethod.CursorAnchorInfo.Builder) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 32 with Matrix

use of android.graphics.Matrix in project android_frameworks_base by ResurrectionRemix.

the class CursorAnchorInfoTest method testBuilder.

@SmallTest
public void testBuilder() throws Exception {
    final int SELECTION_START = 30;
    final int SELECTION_END = 40;
    final int COMPOSING_TEXT_START = 32;
    final String COMPOSING_TEXT = "test";
    final int INSERTION_MARKER_FLAGS = FLAG_HAS_VISIBLE_REGION | FLAG_HAS_INVISIBLE_REGION | FLAG_IS_RTL;
    final float INSERTION_MARKER_HORIZONTAL = 10.5f;
    final float INSERTION_MARKER_TOP = 100.1f;
    final float INSERTION_MARKER_BASELINE = 110.4f;
    final float INSERTION_MARKER_BOTOM = 111.0f;
    Matrix TRANSFORM_MATRIX = new Matrix(Matrix.IDENTITY_MATRIX);
    TRANSFORM_MATRIX.setScale(10.0f, 20.0f);
    final Builder builder = new Builder();
    builder.setSelectionRange(SELECTION_START, SELECTION_END).setComposingText(COMPOSING_TEXT_START, COMPOSING_TEXT).setInsertionMarkerLocation(INSERTION_MARKER_HORIZONTAL, INSERTION_MARKER_TOP, INSERTION_MARKER_BASELINE, INSERTION_MARKER_BOTOM, INSERTION_MARKER_FLAGS).setMatrix(TRANSFORM_MATRIX);
    for (int i = 0; i < MANY_BOUNDS.length; i++) {
        final RectF bounds = MANY_BOUNDS[i];
        final int flags = MANY_FLAGS_ARRAY[i];
        builder.addCharacterBounds(i, bounds.left, bounds.top, bounds.right, bounds.bottom, flags);
    }
    final CursorAnchorInfo info = builder.build();
    assertEquals(SELECTION_START, info.getSelectionStart());
    assertEquals(SELECTION_END, info.getSelectionEnd());
    assertEquals(COMPOSING_TEXT_START, info.getComposingTextStart());
    assertTrue(TextUtils.equals(COMPOSING_TEXT, info.getComposingText()));
    assertEquals(INSERTION_MARKER_FLAGS, info.getInsertionMarkerFlags());
    assertEquals(INSERTION_MARKER_HORIZONTAL, info.getInsertionMarkerHorizontal());
    assertEquals(INSERTION_MARKER_TOP, info.getInsertionMarkerTop());
    assertEquals(INSERTION_MARKER_BASELINE, info.getInsertionMarkerBaseline());
    assertEquals(INSERTION_MARKER_BOTOM, info.getInsertionMarkerBottom());
    assertEquals(TRANSFORM_MATRIX, info.getMatrix());
    for (int i = 0; i < MANY_BOUNDS.length; i++) {
        final RectF expectedBounds = MANY_BOUNDS[i];
        assertEquals(expectedBounds, info.getCharacterBounds(i));
    }
    assertNull(info.getCharacterBounds(-1));
    assertNull(info.getCharacterBounds(MANY_BOUNDS.length + 1));
    for (int i = 0; i < MANY_FLAGS_ARRAY.length; i++) {
        final int expectedFlags = MANY_FLAGS_ARRAY[i];
        assertEquals(expectedFlags, info.getCharacterBoundsFlags(i));
    }
    assertEquals(0, info.getCharacterBoundsFlags(-1));
    assertEquals(0, info.getCharacterBoundsFlags(MANY_BOUNDS.length + 1));
    // Make sure that the builder can reproduce the same object.
    final CursorAnchorInfo info2 = builder.build();
    assertEquals(SELECTION_START, info2.getSelectionStart());
    assertEquals(SELECTION_END, info2.getSelectionEnd());
    assertEquals(COMPOSING_TEXT_START, info2.getComposingTextStart());
    assertTrue(TextUtils.equals(COMPOSING_TEXT, info2.getComposingText()));
    assertEquals(INSERTION_MARKER_FLAGS, info2.getInsertionMarkerFlags());
    assertEquals(INSERTION_MARKER_HORIZONTAL, info2.getInsertionMarkerHorizontal());
    assertEquals(INSERTION_MARKER_TOP, info2.getInsertionMarkerTop());
    assertEquals(INSERTION_MARKER_BASELINE, info2.getInsertionMarkerBaseline());
    assertEquals(INSERTION_MARKER_BOTOM, info2.getInsertionMarkerBottom());
    assertEquals(TRANSFORM_MATRIX, info2.getMatrix());
    for (int i = 0; i < MANY_BOUNDS.length; i++) {
        final RectF expectedBounds = MANY_BOUNDS[i];
        assertEquals(expectedBounds, info2.getCharacterBounds(i));
    }
    assertNull(info2.getCharacterBounds(-1));
    assertNull(info2.getCharacterBounds(MANY_BOUNDS.length + 1));
    for (int i = 0; i < MANY_FLAGS_ARRAY.length; i++) {
        final int expectedFlags = MANY_FLAGS_ARRAY[i];
        assertEquals(expectedFlags, info2.getCharacterBoundsFlags(i));
    }
    assertEquals(0, info2.getCharacterBoundsFlags(-1));
    assertEquals(0, info2.getCharacterBoundsFlags(MANY_BOUNDS.length + 1));
    assertEquals(info, info2);
    assertEquals(info.hashCode(), info2.hashCode());
    // Make sure that object can be marshaled via {@link Parsel}.
    final CursorAnchorInfo info3 = cloneViaParcel(info2);
    assertEquals(SELECTION_START, info3.getSelectionStart());
    assertEquals(SELECTION_END, info3.getSelectionEnd());
    assertEquals(COMPOSING_TEXT_START, info3.getComposingTextStart());
    assertTrue(TextUtils.equals(COMPOSING_TEXT, info3.getComposingText()));
    assertEquals(INSERTION_MARKER_FLAGS, info3.getInsertionMarkerFlags());
    assertEquals(INSERTION_MARKER_HORIZONTAL, info3.getInsertionMarkerHorizontal());
    assertEquals(INSERTION_MARKER_TOP, info3.getInsertionMarkerTop());
    assertEquals(INSERTION_MARKER_BASELINE, info3.getInsertionMarkerBaseline());
    assertEquals(INSERTION_MARKER_BOTOM, info3.getInsertionMarkerBottom());
    assertEquals(TRANSFORM_MATRIX, info3.getMatrix());
    for (int i = 0; i < MANY_BOUNDS.length; i++) {
        final RectF expectedBounds = MANY_BOUNDS[i];
        assertEquals(expectedBounds, info3.getCharacterBounds(i));
    }
    assertNull(info3.getCharacterBounds(-1));
    assertNull(info3.getCharacterBounds(MANY_BOUNDS.length + 1));
    for (int i = 0; i < MANY_FLAGS_ARRAY.length; i++) {
        final int expectedFlags = MANY_FLAGS_ARRAY[i];
        assertEquals(expectedFlags, info3.getCharacterBoundsFlags(i));
    }
    assertEquals(0, info3.getCharacterBoundsFlags(-1));
    assertEquals(0, info3.getCharacterBoundsFlags(MANY_BOUNDS.length + 1));
    assertEquals(info.hashCode(), info3.hashCode());
    builder.reset();
    final CursorAnchorInfo uninitializedInfo = builder.build();
    assertEquals(-1, uninitializedInfo.getSelectionStart());
    assertEquals(-1, uninitializedInfo.getSelectionEnd());
    assertEquals(-1, uninitializedInfo.getComposingTextStart());
    assertNull(uninitializedInfo.getComposingText());
    assertEquals(0, uninitializedInfo.getInsertionMarkerFlags());
    assertEquals(Float.NaN, uninitializedInfo.getInsertionMarkerHorizontal());
    assertEquals(Float.NaN, uninitializedInfo.getInsertionMarkerTop());
    assertEquals(Float.NaN, uninitializedInfo.getInsertionMarkerBaseline());
    assertEquals(Float.NaN, uninitializedInfo.getInsertionMarkerBottom());
    assertEquals(Matrix.IDENTITY_MATRIX, uninitializedInfo.getMatrix());
}
Also used : RectF(android.graphics.RectF) Matrix(android.graphics.Matrix) Builder(android.view.inputmethod.CursorAnchorInfo.Builder) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 33 with Matrix

use of android.graphics.Matrix in project android_frameworks_base by ResurrectionRemix.

the class CursorAnchorInfoTest method testEquality.

@SmallTest
public void testEquality() throws Exception {
    final Matrix MATRIX1 = new Matrix();
    MATRIX1.setTranslate(10.0f, 20.0f);
    final Matrix MATRIX2 = new Matrix();
    MATRIX2.setTranslate(110.0f, 120.0f);
    final Matrix NAN_MATRIX = new Matrix();
    NAN_MATRIX.setValues(new float[] { Float.NaN, Float.NaN, Float.NaN, Float.NaN, Float.NaN, Float.NaN, Float.NaN, Float.NaN, Float.NaN });
    final int SELECTION_START1 = 2;
    final int SELECTION_END1 = 7;
    final String COMPOSING_TEXT1 = "0123456789";
    final int COMPOSING_TEXT_START1 = 0;
    final int INSERTION_MARKER_FLAGS1 = FLAG_HAS_VISIBLE_REGION;
    final float INSERTION_MARKER_HORIZONTAL1 = 10.5f;
    final float INSERTION_MARKER_TOP1 = 100.1f;
    final float INSERTION_MARKER_BASELINE1 = 110.4f;
    final float INSERTION_MARKER_BOTOM1 = 111.0f;
    final int SELECTION_START2 = 4;
    final int SELECTION_END2 = 8;
    final String COMPOSING_TEXT2 = "9876543210";
    final int COMPOSING_TEXT_START2 = 3;
    final int INSERTION_MARKER_FLAGS2 = FLAG_HAS_VISIBLE_REGION | FLAG_HAS_INVISIBLE_REGION | FLAG_IS_RTL;
    final float INSERTION_MARKER_HORIZONTAL2 = 14.5f;
    final float INSERTION_MARKER_TOP2 = 200.1f;
    final float INSERTION_MARKER_BASELINE2 = 210.4f;
    final float INSERTION_MARKER_BOTOM2 = 211.0f;
    // Default instance should be equal.
    assertEquals(new Builder().build(), new Builder().build());
    assertEquals(new Builder().setSelectionRange(SELECTION_START1, SELECTION_END1).build(), new Builder().setSelectionRange(SELECTION_START1, SELECTION_END1).build());
    assertNotEquals(new Builder().setSelectionRange(SELECTION_START1, SELECTION_END1).build(), new Builder().setSelectionRange(SELECTION_START1, SELECTION_END2).build());
    assertNotEquals(new Builder().setSelectionRange(SELECTION_START1, SELECTION_END1).build(), new Builder().setSelectionRange(SELECTION_START2, SELECTION_END1).build());
    assertNotEquals(new Builder().setSelectionRange(SELECTION_START1, SELECTION_END1).build(), new Builder().setSelectionRange(SELECTION_START2, SELECTION_END2).build());
    assertEquals(new Builder().setComposingText(COMPOSING_TEXT_START1, COMPOSING_TEXT1).build(), new Builder().setComposingText(COMPOSING_TEXT_START1, COMPOSING_TEXT1).build());
    assertNotEquals(new Builder().setComposingText(COMPOSING_TEXT_START1, COMPOSING_TEXT1).build(), new Builder().setComposingText(COMPOSING_TEXT_START2, COMPOSING_TEXT1).build());
    assertNotEquals(new Builder().setComposingText(COMPOSING_TEXT_START1, COMPOSING_TEXT1).build(), new Builder().setComposingText(COMPOSING_TEXT_START1, COMPOSING_TEXT2).build());
    assertNotEquals(new Builder().setComposingText(COMPOSING_TEXT_START1, COMPOSING_TEXT1).build(), new Builder().setComposingText(COMPOSING_TEXT_START2, COMPOSING_TEXT2).build());
    // For insertion marker locations, {@link Float#NaN} is treated as if it was a number.
    assertEquals(new Builder().setMatrix(MATRIX1).setInsertionMarkerLocation(Float.NaN, Float.NaN, Float.NaN, Float.NaN, INSERTION_MARKER_FLAGS1).build(), new Builder().setMatrix(MATRIX1).setInsertionMarkerLocation(Float.NaN, Float.NaN, Float.NaN, Float.NaN, INSERTION_MARKER_FLAGS1).build());
    // Check Matrix.
    assertEquals(new Builder().setMatrix(MATRIX1).build(), new Builder().setMatrix(MATRIX1).build());
    assertNotEquals(new Builder().setMatrix(MATRIX1).build(), new Builder().setMatrix(MATRIX2).build());
    assertNotEquals(new Builder().setMatrix(MATRIX1).build(), new Builder().setMatrix(NAN_MATRIX).build());
    // Unlike insertion marker locations, {@link Float#NaN} in the matrix is treated as just a
    // NaN as usual (NaN == NaN -> false).
    assertNotEquals(new Builder().setMatrix(NAN_MATRIX).build(), new Builder().setMatrix(NAN_MATRIX).build());
    assertEquals(new Builder().setMatrix(MATRIX1).setInsertionMarkerLocation(INSERTION_MARKER_HORIZONTAL1, INSERTION_MARKER_TOP1, INSERTION_MARKER_BASELINE1, INSERTION_MARKER_BOTOM1, INSERTION_MARKER_FLAGS1).build(), new Builder().setMatrix(MATRIX1).setInsertionMarkerLocation(INSERTION_MARKER_HORIZONTAL1, INSERTION_MARKER_TOP1, INSERTION_MARKER_BASELINE1, INSERTION_MARKER_BOTOM1, INSERTION_MARKER_FLAGS1).build());
    assertNotEquals(new Builder().setMatrix(MATRIX1).setInsertionMarkerLocation(Float.NaN, INSERTION_MARKER_TOP1, INSERTION_MARKER_BASELINE1, INSERTION_MARKER_BOTOM1, INSERTION_MARKER_FLAGS1).build(), new Builder().setMatrix(MATRIX1).setInsertionMarkerLocation(INSERTION_MARKER_HORIZONTAL1, INSERTION_MARKER_TOP1, INSERTION_MARKER_BASELINE1, INSERTION_MARKER_BOTOM1, INSERTION_MARKER_FLAGS1).build());
    assertNotEquals(new Builder().setMatrix(MATRIX1).setInsertionMarkerLocation(INSERTION_MARKER_HORIZONTAL1, INSERTION_MARKER_TOP1, INSERTION_MARKER_BASELINE1, INSERTION_MARKER_BOTOM1, INSERTION_MARKER_FLAGS1).build(), new Builder().setMatrix(MATRIX1).setInsertionMarkerLocation(INSERTION_MARKER_HORIZONTAL2, INSERTION_MARKER_TOP1, INSERTION_MARKER_BASELINE1, INSERTION_MARKER_BOTOM1, INSERTION_MARKER_FLAGS1).build());
    assertNotEquals(new Builder().setMatrix(MATRIX1).setInsertionMarkerLocation(INSERTION_MARKER_HORIZONTAL1, INSERTION_MARKER_TOP1, INSERTION_MARKER_BASELINE1, INSERTION_MARKER_BOTOM1, INSERTION_MARKER_FLAGS1).build(), new Builder().setMatrix(MATRIX1).setInsertionMarkerLocation(INSERTION_MARKER_HORIZONTAL1, INSERTION_MARKER_TOP2, INSERTION_MARKER_BASELINE1, INSERTION_MARKER_BOTOM1, INSERTION_MARKER_FLAGS1).build());
    assertNotEquals(new Builder().setMatrix(MATRIX1).setInsertionMarkerLocation(INSERTION_MARKER_HORIZONTAL1, INSERTION_MARKER_TOP1, INSERTION_MARKER_BASELINE1, INSERTION_MARKER_BOTOM1, INSERTION_MARKER_FLAGS1).build(), new Builder().setMatrix(MATRIX1).setInsertionMarkerLocation(INSERTION_MARKER_HORIZONTAL1, INSERTION_MARKER_TOP1, INSERTION_MARKER_BASELINE2, INSERTION_MARKER_BOTOM1, INSERTION_MARKER_FLAGS1).build());
    assertNotEquals(new Builder().setMatrix(MATRIX1).setInsertionMarkerLocation(INSERTION_MARKER_HORIZONTAL1, INSERTION_MARKER_TOP1, INSERTION_MARKER_BASELINE1, INSERTION_MARKER_BOTOM1, INSERTION_MARKER_FLAGS1).build(), new Builder().setMatrix(MATRIX1).setInsertionMarkerLocation(INSERTION_MARKER_HORIZONTAL2, INSERTION_MARKER_TOP1, INSERTION_MARKER_BASELINE1, INSERTION_MARKER_BOTOM1, INSERTION_MARKER_FLAGS1).build());
    assertNotEquals(new Builder().setMatrix(MATRIX1).setInsertionMarkerLocation(INSERTION_MARKER_HORIZONTAL1, INSERTION_MARKER_TOP1, INSERTION_MARKER_BASELINE1, INSERTION_MARKER_BOTOM1, INSERTION_MARKER_FLAGS1).build(), new Builder().setMatrix(MATRIX1).setInsertionMarkerLocation(INSERTION_MARKER_HORIZONTAL1, INSERTION_MARKER_TOP1, INSERTION_MARKER_BASELINE1, INSERTION_MARKER_BOTOM2, INSERTION_MARKER_FLAGS1).build());
    assertNotEquals(new Builder().setMatrix(MATRIX1).setInsertionMarkerLocation(INSERTION_MARKER_HORIZONTAL1, INSERTION_MARKER_TOP1, INSERTION_MARKER_BASELINE1, INSERTION_MARKER_BOTOM1, INSERTION_MARKER_FLAGS1).build(), new Builder().setMatrix(MATRIX1).setInsertionMarkerLocation(INSERTION_MARKER_HORIZONTAL1, INSERTION_MARKER_TOP1, INSERTION_MARKER_BASELINE1, INSERTION_MARKER_BOTOM1, INSERTION_MARKER_FLAGS2).build());
}
Also used : Matrix(android.graphics.Matrix) Builder(android.view.inputmethod.CursorAnchorInfo.Builder) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 34 with Matrix

use of android.graphics.Matrix in project android_frameworks_base by ResurrectionRemix.

the class CropImageView method onDraw.

@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (mIsInitialized) {
        setMatrix();
        matrix.reset();
        matrix.postConcat(this.mMatrix);
        Bitmap bm = mBitmap;
        if (bm != null) {
            canvas.drawBitmap(transparentLayer, 0, 0, null);
            canvas.drawBitmap(bm, matrix, mPaintBitmap);
            if (mIsDrawEnabled) {
                paths.clear();
                pathAttr.clear();
                Point oldPoint = new Point();
                Path path = null;
                for (int i = 0; i < points.size(); i++) {
                    boolean startNewPath = false;
                    Point newPoint;
                    if (newLine.contains(i) || i == 0) {
                        startNewPath = true;
                        path = new Path();
                        path.setFillType(Path.FillType.EVEN_ODD);
                        newPoint = points.get(i);
                        path.moveTo(newPoint.x, newPoint.y);
                    } else {
                        newPoint = points.get(i);
                        path.lineTo(newPoint.x, newPoint.y);
                    }
                    if (newPoint.color != oldPoint.color) {
                        startNewPath = true;
                    }
                    if (newPoint.penSize != oldPoint.penSize) {
                        startNewPath = true;
                    }
                    if (startNewPath) {
                        paths.add(path);
                        oldPoint.color = newPoint.color;
                        oldPoint.penSize = newPoint.penSize;
                        pathAttr.add(newPoint);
                    }
                }
                final Paint paint = getDrawPaint();
                for (int j = 0; j < paths.size(); j++) {
                    Path currentPath = paths.get(j);
                    Point currentPathAttr = pathAttr.get(j);
                    paint.setColor(currentPathAttr.color);
                    paint.setStrokeWidth(currentPathAttr.penSize);
                    Matrix invertMatrix = new Matrix();
                    matrix.invert(invertMatrix);
                    currentPath.transform(invertMatrix);
                    mCanvas.drawPath(currentPath, paint);
                }
            }
            // draw edit frame
            drawEditFrame(canvas);
        }
    }
}
Also used : Path(android.graphics.Path) Bitmap(android.graphics.Bitmap) Matrix(android.graphics.Matrix) Paint(android.graphics.Paint) Paint(android.graphics.Paint)

Example 35 with Matrix

use of android.graphics.Matrix in project LshUtils by SenhLinsh.

the class ImageUtils method scaleImage.

/**
     * 缩放图片-到指定的比例
     */
public static Bitmap scaleImage(Bitmap org, float scaleWidth, float scaleHeight) {
    if (org == null) {
        return null;
    }
    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);
    return Bitmap.createBitmap(org, 0, 0, org.getWidth(), org.getHeight(), matrix, true);
}
Also used : Matrix(android.graphics.Matrix)

Aggregations

Matrix (android.graphics.Matrix)1590 Bitmap (android.graphics.Bitmap)487 Paint (android.graphics.Paint)344 RectF (android.graphics.RectF)276 Test (org.junit.Test)154 Canvas (android.graphics.Canvas)139 Rect (android.graphics.Rect)127 ColorMatrix (android.graphics.ColorMatrix)87 Point (android.graphics.Point)79 View (android.view.View)78 Path (android.graphics.Path)74 ImageView (android.widget.ImageView)73 ShadowBitmap (org.robolectric.shadows.ShadowBitmap)67 ShadowMatrix (org.robolectric.shadows.ShadowMatrix)67 IOException (java.io.IOException)61 Drawable (android.graphics.drawable.Drawable)48 BitmapFactory (android.graphics.BitmapFactory)47 Bundle (android.os.Bundle)46 ViewGroup (android.view.ViewGroup)44 PointF (android.graphics.PointF)42