Android 使用视图的一半和直接设置圆心坐标的差异
我的问题是关于使用视图的一半直接设置圆心坐标的差异。我的设备是Android 使用视图的一半和直接设置圆心坐标的差异,android,Android,我的问题是关于使用视图的一半直接设置圆心坐标的差异。我的设备是240*320,当我使用getwidt()/2和getHeight()方法在屏幕中心画一个圆时,我成功了。但是,当我使用120值代替getWidth/2和160值代替getHight()/2时,我无法在屏幕中心绘制圆。而我认为getWidth()/2值等于120值,getHeight()/2值等于160值 public class MainActivity extends Activity { RelativeLayo
240*320
,当我使用getwidt()/2
和getHeight()
方法在屏幕中心画一个圆时,我成功了。但是,当我使用120
值代替getWidth/2
和160
值代替getHight()/2
时,我无法在屏幕中心绘制圆。而我认为getWidth()/2
值等于120
值,getHeight()/2
值等于160
值
public class MainActivity extends Activity {
RelativeLayout relativeLayout;
C c;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
relativeLayout = (RelativeLayout) findViewById(R.id.re);
c = new C(getApplicationContext());
relativeLayout.addView(c);
}
}
class C extends View {
Paint paint;
C(Context context) {
super(context);
paint = new Paint();
}
@Override
protected void onDraw(Canvas canvas) {
paint.setAntiAlias(true);
paint.setColor(Color.RED);
canvas.drawCircle(120,160,20,paint);//this is not in the center of the screen.
canvas.drawCircle(getWidth/2, getHeight/2, 20 , paint);// But this is in center of the screen
}
}
您是否打印了getWidth和getHeight?它可能不是160和120,因为它根据您的视图大小,有时它的大小会减少某些Android设备上的状态栏…我解决了我的问题。因为
relativeLayout
对象具有padding
属性。我应该从relativeLayout
对象中删除填充属性。有关更多信息,请参阅