我在Android上实现了一个用于绘图的画布,但绘图视图占据了屏幕。我想调整这个屏幕的大小。我该怎么办?
一旦我在oncreate方法中将MyView设置为使用setContentView显示。
主要活动.java
MyView view = new MyView(this);
setContentView(view);
这是画布上的绘画部分。但是,如前所述,我想调整我可以绘制的范围。我不希望整个屏幕都是绘图视图。MyView.java
public class MyView extends View {
private Paint paint = new Paint();
private Path path = new Path();
private int x,y;
public MyView(Context context){
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
paint.setColor(Color.BLACK);
//STROKE속성을 이용하여 테두리...선...
paint.setStyle(Paint.Style.STROKE);
//두께
paint.setStrokeWidth(3);
//path객체가 가지고 있는 경로를 화면에 그린다...
canvas.drawPath(path,paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
x = (int)event.getX();
y = (int)event.getY();
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
path.moveTo(x,y);
break;
case MotionEvent.ACTION_MOVE:
x = (int)event.getX();
y = (int)event.getY();
Log.e("s",x+""+y);
path.lineTo(x,y);
break;
}
//View의 onDraw()를 호출하는 메소드...
invalidate();
return true;
}
}
您可以这样设置宽度和高度:
MyView view = new MyView(this);
setContentView(view);
view.setLayoutParams(new FrameLayout.LayoutParams(500, 500));
其中FrameLayout.LayoutParams
可能因父ViewGroup而异。如果父级是LinearLayout,则应该是LinearLayout.LayoutParams
,依此类推