android paint 圆角 绘制_Android自定义View基础

本文介绍了Android自定义View的基础,包括Paint和Canvas的使用,如绘制圆角矩形、文字、位图等,并探讨了画布的平移、裁剪及保存恢复操作。同时提到了在不同Android版本下处理位图的方法以及DEMO链接。
摘要由CSDN通过智能技术生成

7c3edcbc55c895b2f5a118d9aa7704f4.png

Android自定义控件绘图基础

首先继承View,并进行相关初始化操作。

对于DrawAllocation的Lint警告提示:

不要在View绘制和做布局操作的时候实例化数据,将创建对象等这些分配内存资源和会引起垃圾回收机制的操作在onDraw/onLayout之前进行,例如设置为全局变量,提取一个init()方法来实例化对象。

因为在View及其子类的onDraw(Canvas canvas)方法,会实时调用以更新界面,会频繁的创建对象和进行垃圾回收,垃圾回收的GC线程会抢占CPU资源影响UI的显示性能,这样一个显示很顺畅的用户界面就会因对象分配引起的一些垃圾回收机制进行短暂的停滞。

public class BasisView extends View {
    private Paint mPaint;
    private RectF mRectF;
    private Rect mRect;
    private Bitmap mBitmap;
    private Path mPath;

    public BasisView(Context context) {
        this(context, null);
    }

    public BasisView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);

    }

    public BasisView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    /**
     * Lint警告:
     * Avoid object allocations during draw/layout operations (preallocate and reuse instead)
     * Issue: Looks for memory allocations within drawing code
     * Id: DrawAllocation
     * You should avoid allocating objects during a drawing or layout operation. These are called frequently, so a smooth UI can be interrupted by garbage collection pauses caused by the object allocations.
     * The way this is generally handled is to allocate the needed objects up front and to reuse them for each drawing operation.
     * Some methods allocate memory on your behalf (such as Bitmap.create), and these should be handled in the same way.
     */
    private void init() {
        mPaint = new Paint();
        mRectF = new RectF();
        mRect = new Rect();
        mBitmap = getBitmap(getContext(), R.mipmap.ic_launcher);
        mPath = new Path();
    }
    
    @Override
 protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

 }
}

0、画笔Paint

文本相关

setColor(@ColorInt int color) 设置画笔颜色 
s
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值