在Android的画布旋转图片 [英] Rotating Image on A canvas in android

查看:304
本文介绍了在Android的画布旋转图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据Android的一个特定的角度旋转图像,有些事情就像指南针一样......

我有这个code ......它适用于drawPath() 但我想,以取代路径​​和绘图的事情与图像.. 我试图创建一个位图图像,DrawBitmapImage,但图像不会像旋转的path..Any帮助吗?

 公共无效画(油画画布){
    双角= calculateAngle(currentLongitude,currentLatitude,targetLongitude,targetLatitude);

    //更正;
    闭角= 90;

    //校正方位
    闭角=方位;

    如果((的getContext()的instanceof活性)及&安培; ((Activity)getContext()).getWindowManager().getDefaultDisplay().getOrientation()==Configuration.ORIENTATION_PORTRAIT)angle-=90;

    而(角小于0)=角度+ 360;

    矩形矩形= canvas.getClipBounds();

    INT高= rect.bottom-rect.top;
    INT宽度= rect.right-rect.left;
    INT左= rect.left;
    INT顶部= rect.top;

    如果(高度>宽度){
        顶+ =(高宽)/ 2;
        高度=宽度;
    }
    如果(宽>高度){
        左+ =(宽高)/ 2;
        宽度=高度;
    }

    浮centerwidth =宽度/ 2F;
    浮centerheight =身高/ 2F;

    涂料P =新的油漆();
    p.setColor(颜色);
    p.setStyle(Paint.Style.FILL);
    p.setAntiAlias​​(真正的);

    浮动STARTX =左+(浮动)(centerwidth + Math.cos(deg2rad(角度))*宽/ 3.0);
    浮startY =顶部+(浮动)(centerheight + Math.sin(deg2rad(角度))*高/ 3.0);

    路径path =新路径();
    path.moveTo(
            STARTX,
            startY);
    path.lineTo(
            左+(浮动)(centerwidth + Math.cos(deg2rad(角+ 140))*宽/ 4.0),
            顶+(浮动)(centerheight + Math.sin(deg2rad(角+ 140))*高/ 4.0));
    path.lineTo(
            左+(浮点)centerwidth,
            顶+(浮点)centerheight
            );
    path.lineTo(
            左+(浮动)(centerwidth + Math.cos(deg2rad(角+ 220))*宽/ 4.0),
            顶+(浮动)(centerheight + Math.sin(deg2rad(角+ 220))*高/ 4.0)
            );

    path.lineTo(
            STARTX,
            startY
            );




    canvas.drawPath(路径,p)的;
}
 

解决方案

您可以旋转你的位图当你画它通过使用一个矩阵:

 矩阵矩阵=新的Matrix();
matrix.setRotate(角度,imageCenterX,imageCenterY);
yourCanvas.drawBitmap(yourBitmap,矩阵,NULL);
 

您还可以通过拉伸前旋转画布上做到这一点:

  yourCanvas.save(Canvas.MATRIX_SAVE_FLAG); //保存画布,后来恢复它,只有这个图像将被旋转。
yourCanvas.rotate(-angle);
yourCanvas.drawBitmap(yourBitmap,左,上,NULL);
yourCanvas.restore();
 

挑选一个适合你的最好的之一。

I want to Rotate Image according to a specific angle in android ,some thing like a compass...

I have this code...it works on drawPath() but i want to replace the path and the Drawing thing with image.. I tried to create a bitmap image ,DrawBitmapImage , but the image does not Rotate like the path..Any Help PLease?

public void draw(Canvas canvas) {
    double angle = calculateAngle(currentLongitude, currentLatitude, targetLongitude, targetLatitude);

    //Correction;
    angle-=90;

    //Correction for azimuth
    angle-=azimuth;

    if((getContext() instanceof Activity) && ((Activity)getContext()).getWindowManager().getDefaultDisplay().getOrientation()==Configuration.ORIENTATION_PORTRAIT)angle-=90;

    while(angle<0)angle=angle+360;

    Rect rect = canvas.getClipBounds();

    int height = rect.bottom-rect.top;
    int width = rect.right-rect.left;
    int left = rect.left;
    int top = rect.top;

    if(height>width){
        top+=(height-width)/2;
        height=width;
    }
    if(width>height){
        left+=(width-height)/2;
        width=height;
    }

    float centerwidth = width/2f;
    float centerheight = height/2f;

    Paint p = new Paint();
    p.setColor(color);
    p.setStyle(Paint.Style.FILL);
    p.setAntiAlias(true);

    float startX = left+(float)(centerwidth+Math.cos(deg2rad(angle))*width/3.0);
    float startY = top+(float)(centerheight+Math.sin(deg2rad(angle))*height/3.0);

    Path path = new Path();
    path.moveTo(
            startX,
            startY);
    path.lineTo(
            left+(float)(centerwidth+Math.cos(deg2rad(angle+140))*width/4.0),
            top+(float)(centerheight+Math.sin(deg2rad(angle+140))*height/4.0));
    path.lineTo(
            left+(float)centerwidth,
            top+(float)centerheight
            );
    path.lineTo(
            left+(float)(centerwidth+Math.cos(deg2rad(angle+220))*width/4.0), 
            top+(float)(centerheight+Math.sin(deg2rad(angle+220))*height/4.0)
            );

    path.lineTo(
            startX,
            startY
            );




    canvas.drawPath(path, p);
}

解决方案

You can either rotate your bitmap when you draw it by using a matrix:

Matrix matrix = new Matrix();
matrix.setRotate(angle, imageCenterX, imageCenterY);
yourCanvas.drawBitmap(yourBitmap, matrix, null);

You can also do it by rotating the canvas before drawing:

yourCanvas.save(Canvas.MATRIX_SAVE_FLAG); //Saving the canvas and later restoring it so only this image will be rotated.
yourCanvas.rotate(-angle);
yourCanvas.drawBitmap(yourBitmap, left, top, null);
yourCanvas.restore();

Pick the one that suits you the best.

这篇关于在Android的画布旋转图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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