Example usage for android.graphics Matrix preScale

List of usage examples for android.graphics Matrix preScale

Introduction

In this page you can find the example usage for android.graphics Matrix preScale.

Prototype

public boolean preScale(float sx, float sy) 

Source Link

Document

Preconcats the matrix with the specified scale.

Usage

From source file:Main.java

public static Bitmap toReflectionBitmap(Bitmap bitmap) {
    if (bitmap == null) {
        return null;
    }//  w w  w  .j  ava 2s.co  m

    try {
        int reflectionGap = 1;
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();

        // This will not scale but will flip on the Y axis
        Matrix matrix = new Matrix();
        matrix.preScale(1, -1);

        // Create a Bitmap with the flip matrix applied to it.
        // We only want the bottom half of the image
        Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2, width, height / 2, matrix, false);

        // Create a new bitmap with same width but taller to fit
        // reflection
        Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2),
                Bitmap.Config.ARGB_8888);

        // Create a new Canvas with the bitmap that's big enough for
        // the image plus gap plus reflection
        Canvas canvas = new Canvas(bitmapWithReflection);
        // Draw in the original image
        canvas.drawBitmap(bitmap, 0, 0, null);
        // Draw in the gap
        Paint deafaultPaint = new Paint();
        canvas.drawRect(0, height, width, height + reflectionGap, deafaultPaint);
        // Draw in the reflection
        canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
        // Create a shader that is a linear gradient that covers the
        // reflection
        Paint paint = new Paint();
        LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
                bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff,
                Shader.TileMode.CLAMP);
        // Set the paint to use this shader (linear gradient)
        paint.setShader(shader);
        // Set the Transfer mode to be porter duff and destination in
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
        // Draw a rectangle using the paint with our linear gradient
        canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);

        bitmap = bitmapWithReflection;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bitmap;
}

From source file:Main.java

public static Bitmap toReflectionBitmap(Bitmap bitmap) {
    if (bitmap == null) {
        return null;
    }/*from   w  w  w.  ja v a  2s .c om*/

    try {
        int reflectionGap = 1;
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();

        // This will not scale but will flip on the Y axis
        Matrix matrix = new Matrix();
        matrix.preScale(1, -1);

        // Create a Bitmap with the flip matrix applied to it.
        // We only want the bottom half of the image
        Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2, width, height / 2, matrix, false);

        // Create a new bitmap with same width but taller to fit
        // reflection
        Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888);

        // Create a new Canvas with the bitmap that's big enough for
        // the image plus gap plus reflection
        Canvas canvas = new Canvas(bitmapWithReflection);
        // Draw in the original image
        canvas.drawBitmap(bitmap, 0, 0, null);
        // Draw in the gap
        Paint deafaultPaint = new Paint();
        canvas.drawRect(0, height, width, height + reflectionGap, deafaultPaint);
        // Draw in the reflection
        canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
        // Create a shader that is a linear gradient that covers the
        // reflection
        Paint paint = new Paint();
        LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
                bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
        // Set the paint to use this shader (linear gradient)
        paint.setShader(shader);
        // Set the Transfer mode to be porter duff and destination in
        paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
        // Draw a rectangle using the paint with our linear gradient
        canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);

        bitmap = bitmapWithReflection;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bitmap;
}

From source file:Main.java

public static Bitmap createCorrectlyRotatedBitmapIfNeeded(Bitmap bitmap, String jpegPath, float scale) {
    // Rotate the image if necessary; all images are shot in LANDSCAPE mode
    try {/*www  .  j a va  2  s.c  o  m*/
        ExifInterface exif = new ExifInterface(jpegPath);

        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
        Log.d("CashLensUtils", "image has orientation " + Integer.toString(orientation) + ", width "
                + Integer.toString(bitmap.getWidth()) + ", height " + Integer.toString(bitmap.getHeight()));

        Matrix matrix = new Matrix();

        if (scale != 1.0f)
            matrix.preScale(scale, scale);

        // From http://sylvana.net/jpegcrop/exif_orientation.html
        // For convenience, here is what the letter F would look like if it were tagged correctly 
        // and displayed by a program that ignores the orientation tag (thus showing the stored image):
        //   (1)       2      (3)      4         5          (6)          7         (8)
        //
        //   888888  888888      88  88      8888888888  88                  88  8888888888
        //   88          88      88  88      88  88      88  88          88  88      88  88
        //   8888      8888    8888  8888    88          8888888888  8888888888          88
        //   88          88      88  88
        //   88          88  888888  888888

        if (orientation == 3)
            matrix.postRotate(180);
        else if (orientation == 6)
            matrix.postRotate(90);
        else if (orientation == 8)
            matrix.postRotate(-90);

        if (orientation != 1 || scale != 1.0f) {
            // Create a new image with the correct (maybe rotated) width/height
            Bitmap newImage = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix,
                    true);

            Log.d("CashLensUtils", "created a new image with width " + Integer.toString(newImage.getWidth())
                    + ", height " + Integer.toString(newImage.getHeight()));

            // Replace original image
            return newImage;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return bitmap;
}

From source file:com.wareninja.opensource.gravatar4android.common.Utils.java

public static Bitmap getBitmapWithReflection(Bitmap originalImage) {

    //The gap we want between the reflection and the original image
    final int reflectionGap = 4;

    int width = originalImage.getWidth();
    int height = originalImage.getHeight();

    //This will not scale but will flip on the Y axis
    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);

    //Create a Bitmap with the flip matrix applied to it.
    //We only want the bottom half of the image
    Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, height / 2, width, height / 2, matrix,
            false);//  www  .ja v  a 2s.  c  o  m

    //Create a new bitmap with same width but taller to fit reflection
    Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888);

    //Create a new Canvas with the bitmap that's big enough for
    //the image plus gap plus reflection
    Canvas canvas = new Canvas(bitmapWithReflection);
    //Draw in the original image
    canvas.drawBitmap(originalImage, 0, 0, null);
    //Draw in the gap
    Paint deafaultPaint = new Paint();
    canvas.drawRect(0, height, width, height + reflectionGap, deafaultPaint);
    //Draw in the reflection
    canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

    //Create a shader that is a linear gradient that covers the reflection
    Paint paint = new Paint();
    LinearGradient shader = new LinearGradient(0, originalImage.getHeight(), 0,
            bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
    //Set the paint to use this shader (linear gradient)
    paint.setShader(shader);
    //Set the Transfer mode to be porter duff and destination in
    paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
    //Draw a rectangle using the paint with our linear gradient
    canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);

    return bitmapWithReflection;
}

From source file:com.grarak.kerneladiutor.views.recyclerview.overallstatistics.FrequencyButtonView.java

@Override
public void onCreateView(View view) {
    AppCompatImageButton refresh = (AppCompatImageButton) view.findViewById(R.id.frequency_refresh);
    AppCompatImageButton reset = (AppCompatImageButton) view.findViewById(R.id.frequency_reset);
    AppCompatImageButton restore = (AppCompatImageButton) view.findViewById(R.id.frequency_restore);

    if (mRefreshImage == null) {
        mRefreshImage = ViewUtils// w  ww.  j a  v  a2s.co m
                .drawableToBitmap(ContextCompat.getDrawable(view.getContext(), R.drawable.ic_refresh));
    }
    refresh.setImageBitmap(mRefreshImage);

    if (mResetImage == null) {
        Matrix matrix = new Matrix();
        matrix.postRotate(180);
        matrix.preScale(-1.0f, 1.0f);
        mResetImage = Bitmap.createBitmap(mRefreshImage, 0, 0, mRefreshImage.getWidth(),
                mRefreshImage.getHeight(), matrix, true);
    }
    reset.setImageBitmap(mResetImage);

    refresh.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            rotate(v, false);
            if (mRefreshListener != null) {
                mRefreshListener.onClick(v);
            }
        }
    });
    reset.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            rotate(v, true);
            if (mResetListener != null) {
                mResetListener.onClick(v);
            }
        }
    });
    restore.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            rotate(v, true);
            if (mRestoreListener != null) {
                mRestoreListener.onClick(v);
            }
        }
    });

    setFullSpan(true);
    super.onCreateView(view);
}

From source file:com.example.google.maps.dataviz.MainActivity.java

private void addDataToMap() {
    ArrayList<LatLng> coords = new ArrayList<LatLng>();
    ArrayList<Double> samples = new ArrayList<Double>();
    double minElevation = Integer.MAX_VALUE;
    double maxElevation = Integer.MIN_VALUE;

    try {//from   w ww . jav a  2 s. c  o  m
        Resources res = getResources();
        InputStream istream = res.openRawResource(R.raw.elevation);
        byte[] buff = new byte[istream.available()];
        istream.read(buff);
        istream.close();

        String json = new String(buff);
        JSONArray jsonArray = new JSONArray(json);

        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject sample = jsonArray.getJSONObject(i);

            double elevation = sample.getDouble("elevation");
            minElevation = elevation < minElevation ? elevation : minElevation;
            maxElevation = elevation > maxElevation ? elevation : maxElevation;

            JSONObject location = sample.getJSONObject("location");
            LatLng position = new LatLng(location.getDouble("lat"), location.getDouble("lng"));

            coords.add(position);
            samples.add(elevation);
        }
    } catch (IOException e) {
        Log.e(this.getClass().getName(), "Error accessing elevation data file.");
        return;
    } catch (JSONException e) {
        Log.e(this.getClass().getName(), "Error processing elevation data (JSON).");
        return;
    }

    for (int i = 0; i < coords.size(); i++) {
        double elevation = samples.get(i);
        int height = (int) (elevation / maxElevation * MARKER_HEIGHT);

        Bitmap.Config conf = Bitmap.Config.ARGB_8888;
        Bitmap bitmap = Bitmap.createBitmap(MARKER_WIDTH, height, conf);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawPaint(mPaint);

        // Invert the bitmap (top red, bottom blue).
        Matrix matrix = new Matrix();
        matrix.preScale(1, -1);
        bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);

        LatLng position = coords.get(i);
        BitmapDescriptor bitmapDesc = BitmapDescriptorFactory.fromBitmap(bitmap);
        mMap.addMarker(new MarkerOptions().position(position).icon(bitmapDesc).alpha(.8f)
                .title(new DecimalFormat("#.## meters").format(elevation)));
    }
}

From source file:com.tbse.mywearapplication.WatchFaceDrawer.java

void onDraw(Context context, IWatchFaceConfig config, Canvas canvas, Rect bounds) {
    final Calendar calendar = config.getCalendar();
    final boolean isAmbient = config.isAmbient();
    final boolean isRound = config.isRound();
    final boolean useLightTheme = !isAmbient && config.isLightTheme();

    mBackgroundPaint.setColor(ContextCompat.getColor(context,
            useLightTheme ? R.color.watchface_background_light : R.color.watchface_background));

    /////////////////////////////////////////////////////////////////////
    // Draw your watch face here, using the provided canvas and bounds //
    /////////////////////////////////////////////////////////////////////

    final int width = bounds.width();
    final int height = bounds.height();

    // Find the center. Ignore the window insets so that, on round
    // watches with a "chin", the watch face is centered on the entire
    // screen, not just the usable portion.
    final float centerX = width / 2f;
    final float centerY = height / 2f;

    // Draw the background.
    if (mIsMobilePreview) {
        if (isRound) {
            canvas.drawCircle(centerX, centerY, centerX, mPreviewBorderPaint);
        } else {//w w w.ja v a 2 s .com
            final float radius = mPreviewSquareRadius;
            final RectF rectF = new RectF(0, 0, canvas.getWidth(), canvas.getHeight());
            canvas.drawRoundRect(rectF, radius, radius, mPreviewBorderPaint);
        }

        final float translateXY = width * 0.05f;
        canvas.translate(translateXY, translateXY);
        canvas.scale(0.9f, 0.9f);

        if (isRound) {
            canvas.drawCircle(centerX, centerY, centerX, mBackgroundPaint);
        } else {
            canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), mBackgroundPaint);
        }
    } else {
        canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), mBackgroundPaint);
    }

    // Draw weather icon
    if (image != null) {
        Matrix matrix = new Matrix();
        matrix.setTranslate(50, 90);
        matrix.preScale(0.2f, 0.2f);
        canvas.drawBitmap(image, matrix, null);
    }

    final float secRot = calendar.get(Calendar.SECOND) / 30f * (float) Math.PI;
    final int minutes = calendar.get(Calendar.MINUTE);
    final float minRot = minutes / 30f * (float) Math.PI;
    final float hrRot = ((calendar.get(Calendar.HOUR) + (minutes / 60f)) / 6f) * (float) Math.PI;

    final float secLength = centerX - mSecondOuterOffset;
    final float minLength = centerX - mMinuteOuterOffset;
    final float hrLength = centerX - mHourOuterOffset;

    if (!isAmbient) {
        final float secX = (float) Math.sin(secRot) * secLength;
        final float secY = (float) -Math.cos(secRot) * secLength;
        canvas.drawLine(centerX, centerY, centerX + secX, centerY + secY, mSecondHandPaint);
    }

    final float minX = (float) Math.sin(minRot) * minLength;
    final float minY = (float) -Math.cos(minRot) * minLength;
    canvas.drawLine(centerX, centerY, centerX + minX, centerY + minY, mMinuteHandPaint);

    final float hrX = (float) Math.sin(hrRot) * hrLength;
    final float hrY = (float) -Math.cos(hrRot) * hrLength;
    canvas.drawLine(centerX, centerY, centerX + hrX, centerY + hrY, mHourHandPaint);

    // Draw weather text
    canvas.drawText(day, 50, 50, isAmbient ? ambientTextPaint : textPaint);
    canvas.drawText(low, 50, 65, isAmbient ? ambientTextPaint : textPaint);
    canvas.drawText(high, 80, 65, isAmbient ? ambientTextPaint : textPaint);
    canvas.drawText(weatherDescription, 50, 80, isAmbient ? ambientTextPaint : textPaint);

}

From source file:com.yk.notification.util.BitmapUtil.java

/**
 * ?//from  w  ww. j a  v a  2  s . c  o  m
 * 
 * @param bitmap
 *            
 * @return ?
 */
public static Bitmap reverseByHorizontal(Bitmap bitmap) {
    Matrix matrix = new Matrix();
    matrix.preScale(-1, 1);
    return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);
}

From source file:com.yk.notification.util.BitmapUtil.java

/**
 * ?//ww w. j  a  v a 2s .c  o m
 * 
 * @param bitmap
 *            
 * @return ?
 */
public static Bitmap reverseByVertical(Bitmap bitmap) {
    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);
    return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);
}

From source file:com.yk.notification.util.BitmapUtil.java

/**
 * //from w  w  w .ja v a2s  .com
 * 
 * @param bitmap
 *            ?Bitmap
 * @return Bitmap
 */
public static Bitmap createReflectionBitmap(Bitmap bitmap) {
    final int reflectionGap = 4;
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);

    Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2, width, height / 2, matrix, false);

    Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmapWithReflection);
    canvas.drawBitmap(bitmap, 0, 0, null);
    Paint deafalutPaint = new Paint();
    canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint);

    canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

    Paint paint = new Paint();
    LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
            bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
    paint.setShader(shader);
    // Set the Transfer mode to be porter duff and destination in
    paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
    // Draw a rectangle using the paint with our linear gradient
    canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);

    return bitmapWithReflection;
}