缩放和平移android中的位图 [英] Scaling and translating a bitmap in android

查看:133
本文介绍了缩放和平移android中的位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图出售一个位图,并在每一步翻译它。

如果我们看一下下面的code,我绘制图像,平移和缩放,然后反向执行相同的操作,从而获得原来的配置了。但应用的操作后,我得到原始缩放图像(比例因子1),但图像转换吨的不同位置。

能否请你指出正确的方法做到这一点? (在上面的例子中,我怎么达到原来的配置?​​)

 保护无效的onDraw(帆布油画){
        super.onDraw(画布);
        字模=新的Matrix();        规模=(浮点)屏幕宽度/ 201.0f;
        matrix.setTranslate(-40,-40);
        matrix.setScale(秤,秤);        canvas.drawBitmap(位图,矩阵,油漆);        //回原来的
        canvas.drawColor(0,Mode.CLEAR);
        matrix.setScale(1.0F /秤,1.0F /缩放);
        matrix.setTranslate(40,40);
        canvas.drawBitmap(位图,矩阵,油漆);    }


解决方案

您应该只用于缩放和翻译画布的方法,这样,那么你可以利用的在保存()恢复()的API做你所需要的。例如:

 保护无效的onDraw(帆布油画){
    super.onDraw(画布);    //保存画布的当前状态
    canvas.save();    规模=(浮点)屏幕宽度/ 201.0f;    canvas.translate(-40,-40);
    canvas.scale(秤,秤);
    canvas.drawBitmap(位图,0,0,油漆);    //恢复到它是国家最后一次保存时
    canvas.restore();    canvas.drawColor(0,Mode.CLEAR);
    canvas.drawBitmap(位图,0,0,油漆);
}

I am trying to sale a bitmap and translating it at each step.

If we look at the following code, I am drawing an image, translating and scaling it and then performing the same operations in reverse so as to get the original configuration back. But after applying the operations, I do get the original scaled image (scale factor 1) but the image is translated t a different position.

Could you please point out the correct method do so ? (In the example above, how do I reach the original configuration? )

 protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Matrix matrix = new Matrix();

        scale = (float)screenWidth/201.0f;
        matrix.setTranslate(-40, -40);
        matrix.setScale(scale, scale);

        canvas.drawBitmap(bitMap, matrix, paint);

        //back to original
        canvas.drawColor(0, Mode.CLEAR);
        matrix.setScale(1.0f/scale, 1.0f/scale);
        matrix.setTranslate(40,40);
        canvas.drawBitmap(bitMap, matrix, paint);

    }

解决方案

You should just use the Canvas methods for scaling and translating, that way you can then take advantage of the save() and restore() APIs to do what you need. For example:

protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    //Save the current state of the canvas
    canvas.save();

    scale = (float) screenWidth / 201.0f;

    canvas.translate(-40, -40);
    canvas.scale(scale, scale);
    canvas.drawBitmap(bitMap, 0, 0, paint);

    //Restore back to the state it was when last saved
    canvas.restore();

    canvas.drawColor(0, Mode.CLEAR);
    canvas.drawBitmap(bitMap, 0, 0, paint);
}

这篇关于缩放和平移android中的位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆