一个简单的Android(安卓)自定义view,实现手指划线
16lz
2021-12-04
package com.minibox.mycpp.view;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Path;import android.graphics.RectF;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.View;import java.util.ArrayList;import java.util.List;import androidx.annotation.Nullable;/** * Created by lingbeijing on 2020/8/18. */public class RandomBoll extends View { public RandomBoll(Context context) { super(context); init(); } private Paint mPaint; private Paint paintS; private float touchX; private float touchY; private List<Path> multiPath = new ArrayList<>(); private Canvas myCanvas; public RandomBoll(Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(); } public RandomBoll(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } public RandomBoll(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); init(); } private void init(){ mPaint = new Paint(); mPaint.setARGB(0xff, 0x23, 0x23,0xad); mPaint.setTextSize(59); mPaint.setStyle(Paint.Style.STROKE); mPaint.setAntiAlias(true); mPaint.setStrokeWidth(1f); paintS = new Paint(Paint.ANTI_ALIAS_FLAG); paintS.setStyle(Paint.Style.STROKE); paintS.setStrokeWidth(2f); paintS.setColor(Color.BLUE); } @Override public void draw(Canvas canvas) { super.draw(canvas); canvas.drawText("I Love You",200,60,mPaint); RectF rect = new RectF(180, 10, 180 + 320, 80); canvas.drawRoundRect(rect,51f,51f, paintS); drawMultiPath(canvas); } Path pathNew; @Override public boolean onTouchEvent(MotionEvent event) { touchX = event.getX(); touchY = event.getY(); if (event.getAction() == MotionEvent.ACTION_DOWN) { pathNew = new Path(); pathNew.reset(); pathNew.moveTo(touchX, touchY); multiPath.add(pathNew); } if (Math.abs(touchX + touchY) > 20) { pathNew.lineTo(touchX, touchY); } invalidate(); return true; } private void drawMultiPath(Canvas c) { if (c == null) { return; } for (Path p : multiPath) c.drawPath(p, paintS); }}
更多相关文章
- 代码中设置drawableleft
- android 3.0 隐藏 系统标题栏
- Android开发中activity切换动画的实现
- Android(安卓)学习 笔记_05. 文件下载
- Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
- 技术博客汇总
- android 2.3 wifi (一)
- AndRoid Notification的清空和修改
- Android中的Chronometer