安卓界面开发绘制一只兔子
程序员文章站
2022-05-06 15:01:50
文章目录创建安卓应用创建custom定义变量构造方法,初始化画笔绘制方法--绘制兔子编写主界面类运行效果创建安卓应用创建custom定义变量构造方法,初始化画笔绘制方法–绘制兔子package net.zjs.draw_circle_view;import android.app.Activity;import android.content.Context;import android.graphics.Canvas;import android.graphics.Co...
创建安卓应用
创建custom
定义变量
构造方法,初始化画笔
绘制方法–绘制兔子
package net.zjs.draw_circle_view;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.view.View;
public class CustomView extends View {
private Paint paint;//画笔
private int screenWidth;//屏幕宽度
private int screenHeight;//屏幕高度
/*
* 构造方法--初始化画笔
* */
public CustomView(Context context) {
super(context);
//实例化画笔
paint=new Paint();
//设置画笔防锯齿形
paint.setAntiAlias(true);
// //设置画笔颜色
// paint.setColor(Color.BLACK);
//获取屏幕宽度
screenWidth=((Activity) context).getWindowManager().getDefaultDisplay().getWidth();
//获取屏幕高度
screenHeight=((Activity) context).getWindowManager().getDefaultDisplay().getHeight();
}
/*
* 绘制方法
* 绘制兔子*/
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
//设置画笔颜色--黑色
paint.setColor(Color.BLACK);
//
paint.setStyle(Paint.Style.STROKE);
//绘制椭圆(设置其大小)--左耳朵
canvas.drawArc(new RectF(200,200,400,1000),0,360,true,paint);
//绘制椭圆(右耳朵)
canvas.drawArc(new RectF(700,200,900,1000),0,360,true,paint);
//绘制圆(脸)
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.FILL);
canvas.drawCircle(550,1200,500,paint);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);//空心圆
canvas.drawCircle(550,1200,500,paint);
//绘制实心圆(眼睛)
paint.setStyle(Paint.Style.FILL);
canvas.drawCircle(350,1100,40,paint);
canvas.drawCircle(750,1100,40,paint);
//直线(嘴)
canvas.drawLine(300,1400,800,1400,paint);
//矩形(牙齿)
paint.setStyle(Paint.Style.STROKE);
canvas.drawRect(new Rect(400,1500,500,1400),paint);
canvas.drawRect(new Rect(600,1500,700,1400),paint);
}
}
编写主界面类
package net.zjs.draw_circle_view;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
private CustomView mCustomview;//声明自定义视图
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//实例化自定义视图
mCustomview=new CustomView(this);
//将自定义视图设置为用户界面
setContentView(mCustomview);
}
}
运行效果
本文地址:https://blog.csdn.net/qq_48838980/article/details/112546570