• 5月2日学习日志


    今天学习了简单的画板的制作。

    代码为:

    public class MyView extends View{
    
        private Paint mPaint;  //绘制线条的Path
        private Path mPath;      //记录用户绘制的Path
        private Canvas mCanvas;  //内存中创建的Canvas
        private Bitmap mBitmap;  //缓存绘制的内容
    
        private int mLastX;
        private int mLastY;
    
        public MyView(Context context) {
            super(context);
            init();
        }
    
        public MyView(Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
        }
    
        public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            init();
        }
    
        private void init(){
            mPath = new Path();
            mPaint = new Paint();   //初始化画笔
            mPaint.setColor(Color.GREEN);
            mPaint.setAntiAlias(true);
            mPaint.setDither(true);
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setStrokeJoin(Paint.Join.ROUND); //结合处为圆角
            mPaint.setStrokeCap(Paint.Cap.ROUND); // 设置转弯处为圆角
            mPaint.setStrokeWidth(20);   // 设置画笔宽度
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            int width = getMeasuredWidth();
            int height = getMeasuredHeight();
            // 初始化bitmap,Canvas
            mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            mCanvas = new Canvas(mBitmap);
        }
    
        //重写该方法,在这里绘图
        @Override
        protected void onDraw(Canvas canvas) {
            drawPath();
            canvas.drawBitmap(mBitmap, 0, 0, null);
        }
    
        //绘制线条
        private void drawPath(){
            mCanvas.drawPath(mPath, mPaint);
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent event) {
    
            int action = event.getAction();
            int x = (int) event.getX();
            int y = (int) event.getY();
    
            switch (action)
            {
                case MotionEvent.ACTION_DOWN:
                    mLastX = x;
                    mLastY = y;
                    mPath.moveTo(mLastX, mLastY);
                    break;
                case MotionEvent.ACTION_MOVE:
                    int dx = Math.abs(x - mLastX);
                    int dy = Math.abs(y - mLastY);
                    if (dx > 3 || dy > 3)
                        mPath.lineTo(x, y);
                    mLastX = x;
                    mLastY = y;
                    break;
            }
    
            invalidate();
            return true;
        }
    }
  • 相关阅读:
    spring 使用 context:property-placeholder 加载 多个 properties
    Spring自动注入Bean
    正则查询符合条件的字符串
    csv测试类。用起来,就是那么简单。每个单元格都是以逗号分隔
    eclipse 设置字体高亮
    订单生成类,个人经验总结!
    java 导出Excel 大数据量,自己经验总结!
    Oracle的数据恢复——Flashback用法汇总
    org.apache.log4j.Logger详解
    Date、String和Timestamp类型转换
  • 原文地址:https://www.cnblogs.com/20193925zxt/p/14910499.html
Copyright © 2020-2023  润新知