package com.jsjrjsb1504.sdut.gesture;
import android.content.Context;
import android.graphics.*;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FrameLayout mFrameLayout = findViewById(R.id.framelayout);
//将当前的布局和内部类相关联
mFrameLayout.addView(new Myview(this));
}
//内部类用于绘画
public class Myview extends View{
public Myview(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//创建一支画笔
Paint paint = new Paint();
paint.setColor(Color.GREEN);
paint.setAntiAlias(true);
//设置画笔粗细
paint.setStrokeWidth(2);
//设置是否为空心
paint.setStyle(Paint.Style.FILL_AND_STROKE);
//绘制头部
RectF mRectF_head = new RectF(110,110,canvas.getHeight()/2,canvas.getHeight()/2);
mRectF_head.offset(100,10);
canvas.drawArc(mRectF_head,-10,-160,false,paint);
//天线
paint.setColor(Color.GREEN);
paint.setStrokeWidth(10);
canvas.drawLine(220,115,435,300,paint);
canvas.drawLine(900,115,685,300,paint);
//眼睛
paint.setColor(Color.WHITE);
paint.setStrokeWidth(20);
canvas.drawCircle(700,300,4,paint);
canvas.drawCircle(400,300,4,paint);
paint.setStrokeWidth(2);
paint.setColor(Color.GREEN);
//身体
canvas.drawRect(220,450,885,900,paint);
RectF rextf_body = new RectF(220,800,885,1200);
canvas.drawRoundRect(rextf_body,50,50,paint);
//胳膊
RectF rectF_arm = new RectF(100,430,200,1000);
canvas.drawRoundRect(rectF_arm,180,180,paint);
rectF_arm.offset(805,0);
canvas.drawRoundRect(rectF_arm,180,180,paint);
//dialog
paint.setColor(Color.BLACK);
paint.setTextSize(30);
paint.setTextAlign(Paint.Align.CENTER); //字体居中
canvas.drawText("你是真牛逼",950,250,paint);
//腿
paint.setColor(Color.GREEN);
RectF rectF_foot = new RectF(400,1100,500,1500);
canvas.drawRoundRect(rectF_foot,180,180,paint);
rectF_foot.offset(200,0);
canvas.drawRoundRect(rectF_foot,180,180,paint);
}
}
}
activity_main.xml
这里只有一点需要注意,布局是帧布局—-FramLayout
<FrameLayout
android:id="@+id/framelayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>FrameLayout>
package com.jsjrjsb1504.sdut.gesture;
import android.content.Context;
import android.graphics.*;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
public class Main2Activity extends AppCompatActivity {
float rationpieceOfWidth = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
FrameLayout mFrameLayout = findViewById(R.id.framelayout);
//将当前的布局和内部类相关联
mFrameLayout.addView(new Myview(this));
}
//内部类用于绘画
public class Myview extends View {
// //自定义测量
// @Override
// protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// int widthSize = MeasureSpec.getSize(widthMeasureSpec);
// int widthMode = MeasureSpec.getMode(widthMeasureSpec);
//
// int heightSize = MeasureSpec.getSize(heightMeasureSpec);
// int heightMode = MeasureSpec.getMode(heightMeasureSpec);
//
// int width = Math.min(heightSize, widthSize);
//
// if (widthMode == MeasureSpec.UNSPECIFIED) {
// width = heightSize;
// } else if (heightMode == MeasureSpec.UNSPECIFIED) {
// width = widthSize;
// }
// rationpieceOfWidth = width;
//
// setMeasuredDimension(width, width);
// }
public Myview(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
rationpieceOfWidth = canvas.getWidth();
super.onDraw(canvas);
//创建一支画笔
Paint paint = new Paint();
paint.setColor(Color.GREEN);
paint.setAntiAlias(true);
//设置画笔粗细
paint.setStrokeWidth(2);
//设置是否为空心
paint.setStyle(Paint.Style.FILL_AND_STROKE);
//绘制头部
RectF mRectF_head = new RectF(rationpieceOfWidth / 4, rationpieceOfWidth / 8, rationpieceOfWidth / 4 + rationpieceOfWidth / 2, rationpieceOfWidth / 8 + rationpieceOfWidth / 2);
canvas.drawArc(mRectF_head, -10, -160, false, paint); //顺时针旋转
//天线
paint.setColor(Color.GREEN);
paint.setStrokeWidth(10);
canvas.drawLine(rationpieceOfWidth / 4, 0, (rationpieceOfWidth / 4 + rationpieceOfWidth / 2) * 2 / 3, (rationpieceOfWidth / 2) * 2 / 3, paint);
canvas.drawLine(rationpieceOfWidth / 4 + rationpieceOfWidth / 2, 0, (rationpieceOfWidth / 4 + rationpieceOfWidth / 2) * 2 / 3, (rationpieceOfWidth / 2) * 2 / 3, paint);
// //眼睛
paint.setColor(Color.WHITE);
paint.setStrokeWidth(20);
canvas.drawCircle(rationpieceOfWidth * 3 / 8, rationpieceOfWidth / 4, 4, paint);
canvas.drawCircle(rationpieceOfWidth * 5 / 8, rationpieceOfWidth / 4, 4, paint);
paint.setStrokeWidth(2);
paint.setColor(Color.GREEN);
//身体
canvas.drawRect(rationpieceOfWidth / 4,
rationpieceOfWidth / 3 + rationpieceOfWidth / 20,
rationpieceOfWidth / 4 + rationpieceOfWidth / 2,
rationpieceOfWidth * 5 / 7, paint);
RectF rextf_body = new RectF(rationpieceOfWidth / 4, rationpieceOfWidth / 3 + rationpieceOfWidth / 10, rationpieceOfWidth / 4 + rationpieceOfWidth / 2, rationpieceOfWidth);
canvas.drawRoundRect(rextf_body, 50, 50, paint);
//胳膊
RectF rectF_arm = new RectF(rationpieceOfWidth / 5 - rationpieceOfWidth / 15,
rationpieceOfWidth / 3,
rationpieceOfWidth / 4-rationpieceOfWidth/50 ,
rationpieceOfWidth * 5 / 7);
canvas.drawRoundRect(rectF_arm, 180, 180, paint);
rectF_arm.offset(rationpieceOfWidth/2+rationpieceOfWidth/5-rationpieceOfWidth/15,0);
canvas.drawRoundRect(rectF_arm,180,180,paint);
//
//
//
//dialog
paint.setColor(Color.BLACK);
paint.setTextSize(30);
paint.setTextAlign(Paint.Align.CENTER); //字体居中
canvas.drawText("你是真牛逼",950,250,paint);
//
//腿
paint.setColor(Color.GREEN);
RectF rectF_foot = new RectF(rationpieceOfWidth/3,rationpieceOfWidth-rationpieceOfWidth/20,rationpieceOfWidth/3+rationpieceOfWidth/10,rationpieceOfWidth+rationpieceOfWidth/3);
canvas.drawRoundRect(rectF_foot,180,180,paint);
rectF_foot.offset(rationpieceOfWidth/4,0);
canvas.drawRoundRect(rectF_foot,180,180,paint);
}
}
}