android studio绘制小狗
时间: 2023-09-12 21:05:57 浏览: 121
可以通过在 Android Studio 中使用 Canvas 和 Paint 类来绘制小狗。以下是一个简单的示例:
1. 在布局文件中添加一个自定义 View:
```xml
<com.example.dogview.DogView
android:id="@+id/dogView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
```
2. 创建一个名为 DogView 的自定义 View 类,并在其中重写 onDraw 方法:
```java
public class DogView extends View {
private Paint paint;
public DogView(Context context, AttributeSet attrs) {
super(context, attrs);
paint = new Paint();
paint.setColor(Color.BLACK);
paint.setStrokeWidth(5);
paint.setStyle(Paint.Style.STROKE);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int centerX = getWidth() / 2;
int centerY = getHeight() / 2;
// 绘制头部
canvas.drawCircle(centerX, centerY - 200, 100, paint);
// 绘制耳朵
Path path = new Path();
path.moveTo(centerX - 100, centerY - 250);
path.lineTo(centerX - 50, centerY - 300);
path.lineTo(centerX + 50, centerY - 300);
path.lineTo(centerX + 100, centerY - 250);
canvas.drawPath(path, paint);
// 绘制眼睛
canvas.drawCircle(centerX - 50, centerY - 220, 20, paint);
canvas.drawCircle(centerX + 50, centerY - 220, 20, paint);
// 绘制鼻子和嘴巴
canvas.drawCircle(centerX, centerY - 150, 30, paint);
path = new Path();
path.moveTo(centerX - 30, centerY - 120);
path.lineTo(centerX + 30, centerY - 120);
path.lineTo(centerX + 30, centerY - 80);
path.lineTo(centerX - 30, centerY - 80);
canvas.drawPath(path, paint);
// 绘制身体
canvas.drawCircle(centerX, centerY + 100, 150, paint);
canvas.drawLine(centerX, centerY, centerX, centerY + 200, paint);
}
}
```
3. 运行应用程序,即可看到绘制的小狗。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)