各位大神们好,小弟为android初学者,请赐教。
我现在有一个xml文件,里面有两个自定义的view,代码如下。
<LinearLayout android:orientation="horizontal"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent"
           android:layout_weight="1">
         <com.android.imgTest1120.iniView01 android:layout_width="fill_parent"
                 android:layout_height="fill_parent"
                 android:layout_weight="1"
                 android:background="#ef3"
                 android:id="@+id/img01"/>
         <com.android.imgTest1120.iniView01 android:layout_width="fill_parent"
                 android:layout_height="fill_parent"
                 android:layout_weight="1"
                 android:background="#E8A2B4"
                 android:id="@+id/img02"/>
    </LinearLayout>
我的activity是用canvas往这里面画图,希望在每个view中画一个居于中心的圆,但是代码无法运行,总是崩溃,希望大家能帮助我,这是代码。
public class ImgTestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
} private class iniView01 extends View { public iniView01(Context context) {
super(context);
// TODO Auto-generated constructor stub
} @Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
//set background color
canvas.drawColor(Color.WHITE);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(3);
canvas.drawCircle(this.getWidth()/2, this.getHeight()/2, 30, paint);
} }
}
在此感谢大家的帮助。