安卓 View 开发 绘制尺子

上图:

Layout:

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:orientation="vertical"
    tools:context=".MainActivity">

<youli.com.example.administrator.myview.MyView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

</LinearLayout>

MyView: 尺子View


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

public class MyView extends View {

    private Paint paint;

    public MyView(Context context) {
        super(context);
    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.STROKE);

    }

    protected void onDraw(Canvas canvas) {
        paint.setColor(Color.BLACK);
        paint.setStrokeWidth(1);
        canvas.drawRoundRect(30, getHeight() / 2, getWidth() - 30, getHeight() / 2 + 200, 10, 10, paint);

        canvas.save(); //保存现场
        for (int i = 1; i < 92; i++) {
            String value = String.valueOf(i);
            if (value.length() == 1) {
                if (value.contains("1")) {
                    canvas.drawLine(60, getHeight() / 2 + 200, 60, getHeight() / 2 + 90, paint);
                } else if (value.contains("6")) {
                    canvas.drawLine(60, getHeight() / 2 + 200, 60, getHeight() / 2 + 120, paint);
                } else {
                    canvas.drawLine(60, getHeight() / 2 + 200, 60, getHeight() / 2 + 150, paint);
                }
            } else if (value.length() > 1) {
                String[] len = value.split("");
                if (TextUtils.equals(len[len.length - 1], "1")) {
                    canvas.drawLine(60, getHeight() / 2 + 200, 60, getHeight() / 2 + 90, paint);
                } else if (TextUtils.equals(len[len.length - 1], "6")) {
                    canvas.drawLine(60, getHeight() / 2 + 200, 60, getHeight() / 2 + 120, paint);
                } else {
                    canvas.drawLine(60, getHeight() / 2 + 200, 60, getHeight() / 2 + 150, paint);
                }
            }
            canvas.translate(20, 0);//偏移量
        }
        canvas.restore(); //回复现场

        paint.setTextSize(30);
        for (int i = 0; i < 20; i++) {
            if (i == 0) {
                canvas.drawText(String.valueOf(i) + " cm", 50, getHeight() / 2 + 70, paint);
            } else {
                canvas.drawText(String.valueOf(i), 50, getHeight() / 2 + 70, paint);
            }
            canvas.translate(199, 0);//偏移量
        }

    }

}

myview:招财进宝  



import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.annotation.UiThread;

public class MyView extends View {


    //================时间动起来

    /**
     * 指针转动方向
     */
    private int Z1 = 6;
    private int Z2 = 6;
    private int Z3 = 6;  //招财


    private Paint paint;

    public MyView(Context context) {
        super(context);
    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.STROKE);

    }

    protected void onDraw(Canvas canvas) {
        paint.setColor(Color.BLACK);
        paint.setStrokeWidth(2);
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);

        //==================================时间
        canvas.save(); //保存现场
        canvas.drawCircle(200, 200, 200, paint);
        for (int i = 0; i < 60; i++) {
            if (i == 0) {
                paint.setColor(Color.RED);
                canvas.drawLine(370, 200, 400, 200, paint);
            } else if (i == 5 || i == 10 || i == 15 || i == 20 || i == 25 || i == 30 || i == 35 || i == 40 || i == 45 || i == 50 || i == 55) {
                paint.setColor(Color.RED);
                canvas.drawLine(370, 200, 400, 200, paint);
            } else {
                paint.setColor(Color.BLACK);
                canvas.drawLine(385, 200, 400, 200, paint);
            }
            canvas.rotate(6, 200, 200);
        }

        paint.setStrokeWidth(1);
        for (int i = 0; i < 12; i++) {
            if (i == 0) {
                paint.setTextSize(30);
                canvas.drawText(String.valueOf(12), 190, 60, paint);
            } else if (i == 3 || i == 6 || i == 9) {
                paint.setTextSize(30);
                canvas.drawText(String.valueOf(i), 190, 60, paint);
            } else {
                paint.setTextSize(20);
                canvas.drawText(String.valueOf(i), 190, 60, paint);
            }
            canvas.rotate(30, 200, 200);
        }

        //指针1
        paint.setStrokeWidth(2);
        canvas.drawLine(200, 200, 280, 120, paint);
        canvas.rotate(Z1, 200, 200);
        Z1 += 6;

        //指针2
        paint.setStrokeWidth(4);
        canvas.drawLine(200, 200, 200, 120, paint);
        canvas.rotate(Z2, 200, 200);
        Z2 += 6;

        canvas.restore(); //恢复现场


//================================招财
        canvas.save(); //保存现场
        paint.setStrokeWidth(2);
        canvas.drawCircle(getWidth() / 2, 200, 200, paint);
        paint.setTextSize(60);
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.FILL);
        canvas.drawText("招财 ", getWidth() / 2 - 60, 220, paint);
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.STROKE);
        for (int i = 0; i < 192; i++) {
            canvas.drawLine(getWidth() / 2, 300, getWidth() / 2 + 180, 280, paint);
            canvas.rotate(Z3, getWidth() / 2, 200);
        }
        Z3 += 5;
        canvas.restore(); //回复现场



        //================================进宝
        canvas.save(); //保存现场
        paint.setStrokeWidth(2);
        canvas.drawCircle(getWidth() / 2+(getWidth() / 4), 200, 200, paint);
        paint.setTextSize(60);
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.FILL);
        canvas.drawText("进宝 ", getWidth() / 2+(getWidth() / 4) - 60, 220, paint);
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.STROKE);
        for (int i = 0; i < 192; i++) {
            canvas.drawLine(getWidth() / 2+(getWidth() / 4), 300, getWidth() / 2+(getWidth() / 4) + 180, 280, paint);
            canvas.rotate(Z3, getWidth() / 2+(getWidth() / 4), 200);
        }
        Z3 += 5;
        canvas.restore(); //回复现场

//======================================尺子
        canvas.save(); //保存现场
        paint.setColor(Color.RED);
        canvas.drawRoundRect(30, getHeight() / 2, getWidth() - 30, getHeight() / 2 + 200, 10, 10, paint);
        for (int i = 1; i < 92; i++) {
            String value = String.valueOf(i);
            if (value.length() == 1) {
                if (value.contains("1")) {
                    paint.setColor(Color.BLUE);
                    canvas.drawLine(60, getHeight() / 2 + 200, 60, getHeight() / 2 + 90, paint);
                } else if (value.contains("6")) {
                    paint.setColor(Color.CYAN);
                    canvas.drawLine(60, getHeight() / 2 + 200, 60, getHeight() / 2 + 120, paint);
                } else {
                    paint.setColor(Color.RED);
                    canvas.drawLine(60, getHeight() / 2 + 200, 60, getHeight() / 2 + 150, paint);
                }
            } else if (value.length() > 1) {
                String[] len = value.split("");
                if (TextUtils.equals(len[len.length - 1], "1")) {
                    paint.setColor(Color.BLUE);
                    canvas.drawLine(60, getHeight() / 2 + 200, 60, getHeight() / 2 + 90, paint);
                } else if (TextUtils.equals(len[len.length - 1], "6")) {
                    paint.setColor(Color.CYAN);
                    canvas.drawLine(60, getHeight() / 2 + 200, 60, getHeight() / 2 + 120, paint);
                } else {
                    paint.setColor(Color.RED);
                    canvas.drawLine(60, getHeight() / 2 + 200, 60, getHeight() / 2 + 150, paint);
                }
            }
            canvas.translate(20, 0);//偏移量
        }
        canvas.restore(); //回复现场

        paint.setTextSize(30);
        for (int i = 0; i < 20; i++) {
            if (i == 0) {
                canvas.drawText(String.valueOf(i) + " cm", 50, getHeight() / 2 + 70, paint);
            } else {
                canvas.drawText(String.valueOf(i), 50, getHeight() / 2 + 70, paint);
            }
            canvas.translate(199, 0);//偏移量
        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return super.onTouchEvent(event);
    }
}

mainActivity:



import android.os.Bundle;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

import java.sql.Time;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {



    private  MyView myView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        myView=findViewById(R.id.myview);


        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                myView.postInvalidate();
            }
        }, 1000, 1000);


    }


}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值