Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/* 
 * Copyright (C) 2011 team-cachebox.de
 *
 * Licensed under the : GNU General Public License (GPL);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.gnu.org/licenses/gpl.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;

import android.graphics.Rect;

import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

public class Main {
    public static int PutImageTargetHeight(Canvas canvas, Drawable image, int x, int y, int height) {
        // float scale = (float)height / (float)image.getBounds().height();
        // int width = (int)Math.round(image.getBounds().width() * scale);

        float scale = (float) height / (float) image.getIntrinsicHeight();
        int width = (int) Math.round((float) image.getIntrinsicWidth() * scale);

        Rect oldBounds = image.getBounds();
        image.setBounds(x, y, x + width, y + height);
        image.draw(canvas);
        image.setBounds(oldBounds);

        return width;
    }

    @SuppressWarnings("deprecation")
    public static int PutImageTargetHeight(Canvas canvas, Drawable image, double Angle, int x, int y,
            int newHeight) {

        float scale = (float) newHeight / (float) image.getIntrinsicHeight();
        float newWidth = (int) Math.round((float) image.getIntrinsicWidth() * scale);

        Bitmap bmp = ((BitmapDrawable) image).getBitmap();
        int width = bmp.getWidth();
        int height = bmp.getHeight();

        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        // createa matrix for the manipulation
        Matrix matrix = new Matrix();
        // resize the bit map
        matrix.postScale(scaleWidth, scaleHeight);
        // rotate the Bitmap
        matrix.postRotate((float) Angle);
        // recreate the new Bitmap
        Bitmap resizedBitmap = Bitmap.createBitmap(bmp, 0, 0, width, height, matrix, true);
        // make a Drawable from Bitmap to allow to set the BitMap
        // to the ImageView, ImageButton or what ever
        BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);

        bmd.setBounds(x, y, x + bmd.getIntrinsicWidth(), y + bmd.getIntrinsicHeight());
        bmd.draw(canvas);

        return bmd.getIntrinsicWidth();

    }
}