从零开始学android<android基本绘图.四十六.>
<span style="font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <com.example.paintpic.MyView android:id="@+id/Myview" android:layout_width="wrap_content" android:layout_height="wrap_content" ></com.example.paintpic.MyView> </LinearLayout> </span>
主界面
<span style="font-size:18px;">package com.example.paintpic; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } } </span>
MyView设置
<span style="font-size:18px;">package com.example.paintpic; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; 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); // TODO Auto-generated constructor stub } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub // canvas是画板对象,背景是白色的 canvas.drawColor(Color.WHITE); // paint画笔工具 Paint paint = new Paint(); // 设置画笔颜色为蓝色 paint.setColor(Color.BLUE); // 为画板上画圆,圆心(50,50)半径50 canvas.drawCircle(50, 50, 50, paint); // 设置画笔颜色的颜色为红色 paint.setColor(Color.RED); // 画矩形 canvas.drawRect(100, 0, 160, 80, paint); // 画笔颜色为绿色 paint.setColor(Color.GREEN); // 画直线 canvas.drawLine(50, 110, 200, 110, paint); paint.setColor(Color.YELLOW); // Rect rect=new Rect(120, 120, 300, 300); // canvas.drawRect(rect, paint); // 使用RectF画多边形 RectF oval = new RectF(); oval.set(10.0f, 140.0f, 110.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); } } </span>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。