简体   繁体   English

Android Canvas如何缩放位图但保持原始绑定?

[英]Android Canvas How to scale Bitmap but keep original bound?

I want to scale a Bitmap using Canvas 我想使用画布缩放位图

I need to keep the Bitmap original width and height 我需要保持位图原始的宽度和高度

and the scaled one aligns center-middle in bound 并且缩放的一个在边界中居中对齐

here is what I have tried: 这是我尝试过的:

public Bitmap scale(Bitmap src) {
    int width = src.getWidth();
    int height = src.getHeight();
    float scaledWidth = width * .5f;
    float scaledHeight = height * .5f;

    Bitmap transBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(transBitmap);
    Paint paint = new Paint();
    float dx = (width - scaledWidth);
    float dy = (height - scaledHeight);
    canvas.drawBitmap(src, null, new RectF(dx, dy, scaledWidth, scaledHeight), paint);

    Matrix m = new Matrix();
    m.setRectToRect(new RectF(0, 0, src.getWidth(), src.getHeight()), new RectF(0, 0, scaledWidth, scaledHeight), Matrix.ScaleToFit.CENTER);
    Bitmap output = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), m, true);

    //return transBitmap;
    return output;
}

I used this method in custom ImageView onTouch: 我在自定义ImageView onTouch中使用了此方法:

 switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            setImageBitmap(scaledBmp);
            break;
        case MotionEvent.ACTION_UP:
            setImageDrawable(originalDrawable);
            break;
    }

UPDATE 更新

MyImageView.java MyImageView.java

public class MyImageView extends ImageView {

    private Drawable mDrawable;
    private Drawable mScaled;

    public MyImageView(Context context, Drawable drawable) {
        super(context);
        setImageDrawable(drawable);
        Bitmap src = ((BitmapDrawable) drawable).getBitmap();
        mDrawable = drawable;
        mScaled = new BitmapDrawable(getResources(), makeScaled(src));
    }

    public Bitmap makeScaled(Bitmap src) {
        int width = src.getWidth();
        int height = src.getHeight();
        float scaledWidth = width * .95f;
        float scaledHeight = height * .95f;
        Matrix m = new Matrix();
        m.setRectToRect(new RectF(0, 0, src.getWidth(), src.getHeight()), new RectF(0, 0, scaledWidth, scaledHeight), Matrix.ScaleToFit.CENTER);
        Bitmap output = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), m, true);
        Canvas xfas = new Canvas(output);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setFilterBitmap(true);
        paint.setDither(true);
        xfas.drawBitmap(output, 0, 0, paint);

        return output;
    }

    @Override public boolean dispatchTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                setImageDrawable(mScaled);
                break;
            case MotionEvent.ACTION_UP:
                setImageDrawable(mDrawable);
                break;
        }
        return super.dispatchTouchEvent(event);
    }

}

If I have not misunderstood, one solution is to use a Matrix and its setRectToRect to translate the original values to the desired one. 如果我没有误会,一种解决方案是使用Matrix及其setRectToRect将原始值转换为所需的值。 From the documentation 从文档中

Set the matrix to the scale and translate values that map the source rectangle to the destination rectangle, returning true if the the result can be represented. 将矩阵设置为比例并转换将源矩形映射到目标矩形的值,如果可以表示结果,则返回true。

Matrix m = new Matrix();
m.setRectToRect(new RectF(0, 0, src.getWidth(), src.getHeight()), new RectF(0, 0, scaledWidth, scaledHeigth), Matrix.ScaleToFit.CENTER);
Bitmap output = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), m, true);

and then simply draw the scaled bitmap on your canvas 然后只需在画布上绘制缩放的位图

Solved 解决了

public static Bitmap scaleBitmap(Bitmap src, float factor) {
    int width = src.getWidth();
    int height = src.getHeight();
    int scaledWidth = (int) (width * factor);
    int scaledHeight = (int) (height * factor);
    Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Bitmap scaled = Bitmap.createScaledBitmap(src, scaledWidth, scaledHeight, false);
    Canvas canvas = new Canvas(bmp);
    Paint paint = new Paint();
    int distX = (width - scaledWidth) / 2;
    int distY = (height - scaledHeight) / 2;
    canvas.drawBitmap(scaled, distX, distY, paint);
    return bmp;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM