当前位置:   article > 正文

Canvas 绘制安卓机器人_绘制安卓机器人js

绘制安卓机器人js

话不多说,先上照片
这里写图片描述
MainActivity.class

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);


        }
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94

activity_main.xml
这里只有一点需要注意,布局是帧布局—-FramLayout

<?xml version="1.0" encoding="utf-8"?>
<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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

方法二,自适应
这里写图片描述
Main2Activity.java

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);


        }
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/225323
推荐阅读
相关标签
  

闽ICP备14008679号