gpt4 book ai didi

Android转换路径使用矩阵显示 "normally"

转载 作者:行者123 更新时间:2023-11-29 21:28:20 25 4
gpt4 key购买 nike

我在 Android 中创建了一个路径。我还无法根据需要显示此路径。当我显示路径时,它显示 0,0 坐标从左上角开始,正 y 值显示在屏幕下方。我想显示路径,就好像坐标系从左下角开始并且 y 值向上增加一样,正如使用“正常”笛卡尔坐标所预期的那样。

我想知道我需要在路径上执行哪些矩阵转换才能实现此目的。

从逻辑上讲,似乎 180 度“翻转”和 90 度旋转应该可以实现这一点。所以我做了一些测试,让图案看起来有点像它应该有 -90 度倾斜和 90 度旋转。我现在遇到的问题是,经过倾斜操作后,线条不再直了。这让我相信我的倾斜转换可能会失败。

是否可以使用 Path 和 Matrix 使线条笔直地显示在所需的方向上?如果是,怎么办?

public class MainActivity extends Activity {

private static final boolean DEBUG = true;
private static final String TAG = "MainActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initLayout();
}

private void initLayout() {
final ImageView iv = (ImageView) findViewById(R.id.imageView1);

// run the sampleCase after the View has been laid-out
iv.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

@Override
public void onGlobalLayout() {
iv.getViewTreeObserver().removeGlobalOnLayoutListener(this);

// run sampleCase
sampleCase(iv);

}
});
}

public void sampleCase(ImageView iv){
Path path = getInitialPath();

// drawPath(iv, path);

drawPath(iv, reorientPath(iv, path));
}

public Path reorientPath(View view, Path path){
Matrix matrix = new Matrix();

// skew -90
matrix.setSkew(-90f, -90f);
path.transform(matrix);
matrix.reset();

// rotate 90 CW
matrix.setRotate(90f);
path.transform(matrix);

path = translatePathToView(path);
path = scalePathToView(view, path);

return path;
}

private Path translatePathToView(Path path){
// moves the path into the visible field
RectF bounds = getBounds(path);
float xTrans = 0f - bounds.left;
float yTrans = 0f - bounds.top;

Matrix matrix = new Matrix();
matrix.setTranslate(xTrans, yTrans);
path.transform(matrix);
return path;
}


private Path scalePathToView(View view, Path path){
RectF bounds = getBounds(path);

float viewW = view.getWidth();
float viewH = view.getHeight();

float pathW = bounds.right - bounds.left;
float pathH = bounds.bottom - bounds.top;

float scaleFactor = Math.min(viewW/pathW, viewH/pathH);

Matrix matrix = new Matrix();
matrix.setScale(scaleFactor, scaleFactor);
path.transform(matrix);

return path;
}

public RectF getBounds(Path path){
RectF bounds = new RectF();
path.computeBounds(bounds, false);
if (DEBUG){
Log.d(TAG, "bounds.bottom: " + bounds.bottom);
Log.d(TAG, "bounds.top: " + bounds.top);
Log.d(TAG, "bounds.left: " + bounds.left);
Log.d(TAG, "bounds.right: " + bounds.right);
}
return bounds;
}

public Path getInitialPath(){
Path path = new Path();
path.moveTo(50, 0);
path.lineTo(90, 0);
path.lineTo(100, 10);
path.lineTo(100, 100);
path.lineTo(30, 100);
path.lineTo(0, 70);
path.lineTo(0, 50);
path.lineTo(50, 0);
return path;
}

public void drawPath(ImageView iv, Path path){
Bitmap bitmap = Bitmap.createBitmap(iv.getWidth(), iv.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
iv.setImageBitmap(bitmap);
Paint paint = setupPaint(2, Color.BLACK);
canvas.drawPath(path, paint);
}

private Paint setupPaint(int width, int color) {
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setDither(true);
paint.setColor(color);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStrokeWidth(width);
return paint;
}

}

转换前:

Before transformations

变形后,关闭但没有雪茄:

After transformations

最佳答案

为传递给 Matrix.setSkew() 的浮点值增加更高的精度给了我想要的结果。

matrix.setSkew(-180000f, -180000f);

顶部、底部、左侧和右侧的线现在是直的。

Strait lines

关于Android转换路径使用矩阵显示 "normally",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19962858/

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