在 Android canvas 上设置屏幕范围
Setting the screen range on the Android canvas
我实现了 canvas 用于在 Android 上绘图,但绘图视图填满了屏幕。我想调整这个屏幕的大小。我该怎么办?
一旦我将 MyView 设置为在 oncreate 方法中使用 setContentView 显示。
MainActivity.java
MyView view = new MyView(this);
setContentView(view);
这里是canvas上画的部分。但是,如前所述,我想调整我可以绘制的范围。我不希望整个屏幕都是绘图视图。
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 而异。如果parent是LinearLayout,应该是LinearLayout.LayoutParams
,依此类推
我实现了 canvas 用于在 Android 上绘图,但绘图视图填满了屏幕。我想调整这个屏幕的大小。我该怎么办?
一旦我将 MyView 设置为在 oncreate 方法中使用 setContentView 显示。
MainActivity.java
MyView view = new MyView(this);
setContentView(view);
这里是canvas上画的部分。但是,如前所述,我想调整我可以绘制的范围。我不希望整个屏幕都是绘图视图。 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 而异。如果parent是LinearLayout,应该是LinearLayout.LayoutParams
,依此类推