当前位置: 代码迷 >> Android >> 怎么用canvas在自定义view里画图
  详细解决方案

怎么用canvas在自定义view里画图

热度:30   发布时间:2016-05-01 21:30:38.0
如何用canvas在自定义view里画图
各位大神们好,小弟为android初学者,请赐教。
我现在有一个xml文件,里面有两个自定义的view,代码如下。
XML code
<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中画一个居于中心的圆,但是代码无法运行,总是崩溃,希望大家能帮助我,这是代码。
Java code
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);        }    }}

在此感谢大家的帮助。



------解决方案--------------------
程序崩溃贴下LOG啊,没有日志怎么找错误
  相关解决方案