使用矩阵旋转位图后缩放比例

时间:2014-02-10 20:37:35

标签: android matrix rotation scale

我得到了一张860x860像素的太阳照片。 我想旋转太阳,锚点是屏幕的中心。 这是我到目前为止所得到的:

class GraphicalMenu extends View{

    int screenH;
    int screenW;
    int angle;
    Bitmap sun, monster;

    public GraphicalMenu(Context context){
        super(context);

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inScaled = false;
        sun = BitmapFactory.decodeResource(getResources(),R.drawable.sun,options);
        monster = BitmapFactory.decodeResource(getResources(),R.drawable.monster,options);
    }

    @Override
    public void onSizeChanged (int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        screenH = h;
        screenW = w;
        sun = Bitmap.createScaledBitmap(sun, w, h, true);
        monster = Bitmap.createScaledBitmap(monster, w, h, true);
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);

      //Increase rotating angle.
        if (angle++ >360)
            angle =0;

        Matrix matrix = new Matrix();
        matrix.setRotate(angle , getWidth()/2, getHeight()/2);
        canvas.drawBitmap(sun, matrix, new Paint());

        //Call the next frame.
        canvas.drawBitmap(monster,0 , 0, null);

        invalidate();
    }
}

我试图改变这一行:

sun = Bitmap.createScaledBitmap(sun, w, h, true);

为:

sun = Bitmap.createScaledBitmap(sun, h, h, true);

然后太阳离开屏幕的中心并向右旋转。 我怎样才能让太阳适应屏幕? 我怎样才能保持它的比例?

修改的 在我的N5上运行它和太阳照片的屏幕截图。 screenshot

sun

1 个答案:

答案 0 :(得分:0)

如果我理解正确,您的代码会遇到一些问题:

  1. 如果您正在多次调用onSizeChanged(),那么您将需要保留原始位图(预先缩放),否则当您再次放大​​位图时,它们看起来会像素化,因为每次缩小它们时,你都会“失去信息。
  2. 当您提供一个矩阵,通过该矩阵将您的位图转换为绘制到画布上时,该矩阵将应用于位图本身,而不是整个屏幕。因此,当您进行drawBitmap()调用时,您实际请求的是位图围绕本地点(getWidth()/ 2,getHeight()/ 2)旋转,而不是屏幕点。
  3. 你实际上并没有围绕屏幕中心旋转,而是围绕着View的中心旋转。除非视图占据整个屏幕,否则您将无法获得预期的效果。
  4. 我无法确切地指出您的问题是什么,但上传一些图片可能会澄清您想要实现的目标以及您目前的情况。

相关问题