我在创建绘制矩形并可通过其角部重新调整大小的CustomView时面临问题。矩形未在其原始坐标中绘制
以下是我的代码。
public class CustomView extends View {
Canvas canvas;
private Context mContext;
private Rect rectObj;
private Paint rectPaint;
private Matrix transform;
public CustomView(Context context) {
super(context);
mContext = context;
initView();
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
initView();
}
public CustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void initView() {
rectPaint = new Paint();
rectPaint.setColor(Color.parseColor("#55000000"));
setFocusable(true); // necessary for getting the touch events
canvas = new Canvas();
// setting the start point for the balls
rectObj = new Rect(100, 100, 200, 200);
// Create a matrix to do rotation
transform = new Matrix();
}
@Override
public void onDraw(Canvas canvas) {
// This is an easy way to apply the same transformation (e.g.
// rotation)
// To the complete canvas.
canvas.setMatrix(transform);
// With the Canvas being rotated, we can simply draw
// All our elements (Rect and Point)
canvas.drawRect(rectObj, rectPaint);
}
}
当我运行这个程序时,输出如下。
如图像显示,我的“矩形”的左上角是100,100但是当我触摸屏幕上的‘矩形的左上角’XY是150,76或与原图不匹配的东西。
我必须使用canvas.setMatrix(变换)在下一个阶段旋转该矩形。
这段代码出了什么问题?
矩形的左下角坐标是100即x我猜不是左上角的坐标。我不确定,但猜测。 – Raghunandan