我在 Android 中创建了一个路径。我还不能根据需要显示此路径。当我显示路径时,它显示从左上角开始的 0,0 坐标,并且正 y 值显示在屏幕下方。我想显示路径,就好像坐标系从左下角开始并且y值增加一样,正如使用“正常”笛卡尔坐标所预期的那样。
我想知道什么矩阵转换,我需要在路径上执行来实现这一点。
从逻辑上讲,似乎 180 度“翻转”和 90 度旋转应该可以实现这一点。所以我做了一些测试,让图案看起来有点像它应该有的 -90 倾斜和 90 旋转。我现在遇到的问题是,经过倾斜操作后,线条不再笔直了。这让我相信我的倾斜变换可能已经关闭。
是否可以使用路径和矩阵使线条笔直并以所需的方向显示?如果是这样,怎么做?
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;
}
}
改造前:
转换后,关闭但没有雪茄: