码迷,mamicode.com
首页 > 移动开发 > 详细

android之简单图形绘制

时间:2014-10-04 21:37:27      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   color   io   ar   java   文件   

首先编写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之简单图形绘制

标签:android   style   blog   http   color   io   ar   java   文件   

原文地址:http://www.cnblogs.com/lxk2010012997/p/4006330.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!