首页 > 代码库 > android之简单图形绘制
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>
android之简单图形绘制
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。