canvas教程

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

字号+ 作者:H5之家 来源:H5之家 2015-09-24 14:00 我要评论( )

如何用canvas在自定义view里画图各位大神们好,小弟为android初学者,请赐教。我现在有一个xml文件,里面有两个自定义的view,代码如下。XML code LinearLayout

如何用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啊,没有日志怎么找错误

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • html5canvas核心技术图形、动画与游戏开发源码

    html5canvas核心技术图形、动画与游戏开发源码

    2017-05-02 17:42

  • 打印html5中Canvas的方法

    打印html5中Canvas的方法

    2017-05-01 15:03

  • HTML5+Canvas调用手机拍照功能实现图片上传(下)

    HTML5+Canvas调用手机拍照功能实现图片上传(下)

    2017-04-30 17:00

  • 学习慕课网canvas倒计时实例笔记

    学习慕课网canvas倒计时实例笔记

    2017-04-30 14:01

网友点评