android之简单图形绘制
首先编写MyView类
代码如下:
package com.example.myhello; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.Style; import android.graphics.Rect; import android.graphics.RectF; import android.util.AttributeSet; import android.view.View; public class MyView extends View{ public MyView(Context context,AttributeSet attrs){ super(context,attrs); } protected void onDraw(Canvas canvas){ canvas.drawColor(Color.WHITE); Paint paint = new Paint(); paint.setColor(Color.BLUE); canvas.drawCircle(50, 50, 30, paint); paint.setColor(Color.BLACK); canvas.drawRect(80,20,160,80,paint); Rect rect = new Rect(); rect.set(180,20,300,80); canvas.drawRect(rect, paint); paint.setStyle(Style.STROKE); paint.setColor(Color.RED); paint.setTextSize(20); canvas.drawText("hello", 10, 108, paint); paint.setColor(Color.BLACK); canvas.drawLine(10, 120, 300, 120, paint); RectF oval = new RectF(); oval.set(10.0f,140.0f,108.0f,200.0f); canvas.drawOval(oval, paint); oval = new RectF(); oval.set(150.0f,140.0f,210.0f,200.0f); canvas.drawArc(oval, 150.0f, 140.0f, true, paint); } }
然后改写main.xml文件
代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <com.example.myhello.MyView android:id="@+id/myview" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </LinearLayout>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。