2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > 安卓 View 开发 绘制尺子

安卓 View 开发 绘制尺子

时间:2019-12-06 14:44:58

相关推荐

安卓 View 开发 绘制尺子

上图:

Layout:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".MainActivity"><.example.administrator.myview.MyViewandroid: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);}//指针1paint.setStrokeWidth(2);canvas.drawLine(200, 200, 280, 120, paint);canvas.rotate(Z1, 200, 200);Z1 += 6;//指针2paint.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);//偏移量}}@Overridepublic 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;@Overrideprotected 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() {@Overridepublic void run() {myView.postInvalidate();}}, 1000, 1000);}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。