我有三个文件。XML,绘图功能和主要活动。LinearLayout
我的XML文件中有一些。
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#ef3"
android:id="@+id/img01"/>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#E8A2B4"
android:id="@+id/img02"/>
</LinearLayout>
这是绘制函数:
public class getBorder extends TextView {
public getBorder(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(android.graphics.Color.RED);
canvas.drawLine(0, 0, this.getWidth() - 1, 0, paint);
canvas.drawLine(0, 0, 0, this.getHeight() - 1, paint);
canvas.drawLine(this.getWidth() - 1, 0, this.getWidth() - 1,
this.getHeight() - 1, paint);
canvas.drawLine(0, this.getHeight() - 1, this.getWidth() - 1,
this.getHeight() - 1, paint);
}
}
这是主要的活动:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final getBorder getBorder = new getBorder(this);
final LinearLayout img01 = (LinearLayout) findViewById(R.id.img01);
img01.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
getBorder.setWidth(100);
getBorder.setHeight(100);
img01.addView(getBorder);
}
});
}
该程序可以绘制边框,但尺寸不合适LinearLayout
。当我LinearLayout
再次单击时,程序崩溃。
另一件事,我想在的中心绘制两个圆LinearLayout
,但是我如何找出中心坐标呢?