gpt4 book ai didi

android - 在android中的 Canvas 上旋转图像

转载 作者:IT老高 更新时间:2023-10-28 22:13:11 28 4
gpt4 key购买 nike

我想根据android中的特定角度旋转图像,比如指南针......

我有这段代码...它适用于 drawPath()但我想用图像替换路径和绘图的东西..我试图创建一个位图图像,DrawBitmapImage,但图像不像路径一样旋转..任何帮助请?

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);
}

最佳答案

您可以在绘制时使用矩阵旋转位图:

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

你也可以在绘制之前通过旋转 Canvas 来做到这一点:

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();

选择最适合您的。

关于android - 在android中的 Canvas 上旋转图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8712652/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com