Android 如何在视图背景上设置可绘制?安卓

Android 如何在视图背景上设置可绘制?安卓,android,background,android-canvas,drawable,draw,Android,Background,Android Canvas,Drawable,Draw,我的自定义视图中有下一个扩展EditText的代码: protected void onDraw(Canvas canvas) { super.onDraw(canvas); int count = getLineCount(); Canvas cvs= new Canvas(); Drawable dr = this.getBackground(); Rec

我的自定义视图中有下一个扩展EditText的代码:

protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

                int count = getLineCount();
                Canvas cvs= new Canvas();
                Drawable dr = this.getBackground();
                Rect r = mRect;
                Paint paint = mPaint;
                mTextPaint.setTextSize(this.getTextSize());
                Paint PaintText = mTextPaint;

                for (int i = 0; i < count; i++) {
                    int baseline = getLineBounds(i, r);
                    cvs.drawText(Integer.toString(i+1), r.left, baseline + 1, PaintText);
                    cvs.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint); 
                }
                cvs.drawLine(PaintText.measureText(Integer.toString(count)), this.getTop(), PaintText.measureText(Integer.toString(count)), this.getBottom(), paint);
                dr.setBounds(0, 0, this.getRight(), this.getBottom());
                dr.draw(cvs);
                this.setBackgroundDrawable(dr);
            }
受保护的void onDraw(画布){
super.onDraw(帆布);
int count=getLineCount();
Canvas cvs=新画布();
Drawable dr=this.getBackground();
Rect r=mRect;
油漆油漆=mPaint;
mTextPaint.setTextSize(this.getTextSize());
Paint PaintText=mTextPaint;
for(int i=0;i

为什么这一观点背后什么都没有?

我想你是在尝试做一些更容易的事情。但是,您的代码不起作用,因为:

  • 您不需要为
    画布
    对象设置
    位图
  • 我认为您希望将
    Canvas
    的内容移动到
    Drawable
    ,但实际上您的代码执行了相反的操作。实际上,您可以在
    Canvas
    对象上绘制
    Drawable
    。试试这个:
  • 位图Bitmap=Bitmap.createBitmap(this.getWidth()、this.getHeight(), 位图.Config.ARGB_8888); 画布cvs=新画布(位图); //没有dr.setBounds()和dr.draw()的绘图工具 这是一个很好的例子( 新的BitmapDrawable(getContext().getResources(),位图));
    受保护的void onDraw(画布){
    位图Bitmap=Bitmap.createBitmap(this.getWidth()、this.getHeight()、Bitmap.Config.ARGB_8888);
    int count=getLineCount();
    画布cvs=新画布(位图);
    cvs.drawColor(Color.rgb(245245245245));
    Rect r=mRect;
    油漆油漆=mPaint;
    mTextPaint.setTextSize(this.getTextSize());
    Paint PaintText=mTextPaint;
    for(int i=0;i
    您不需要创建新的画布对象。onDraw为您创建一个,并将其设置在自己的位图中。只需使用参数中指定的画布名称(在本例中为画布)


    每次创建视图或调用invalidate()时都会调用onDraw。在onDraw()方法中,您正在创建一个新的画布对象。如果您将此代码用于任何图形化的内容(如游戏),那么您就是在泄漏内存。即使你只画一次视图,这也不是最好的方法。使用onDraw()参数中提供的画布。

    非常感谢,但现在我有两个问题:1。它是这个位图的黑色。2.android:padding作用于此背景(.I只想在此视图中设置文本的左侧填充-使用
    Canvas.drawColor()
    更改
    位图的颜色
    。您能详细描述一下您的目标吗?我正在使用行号定制edittext,但问题是,正如您在图片上看到的,用户的文本叠加在顶部绘制文本。据我所知,设置新的背景可绘制时,
    edittext
    的填充会发生变化。I只想为文本设置填充。 Bitmap bitmap = Bitmap.createBitmap(this.getWidth(), this.getHeight(), Bitmap.Config.ARGB_8888); Canvas cvs = new Canvas(bitmap); // Your drawing stuff without dr.setBounds() and dr.draw() this.setBackgroundDrawable( new BitmapDrawable(getContext().getResources(), bitmap));
    protected void onDraw(Canvas canvas) {
        Bitmap bitmap = Bitmap.createBitmap(this.getWidth(), this.getHeight(), Bitmap.Config.ARGB_8888);
        int count = getLineCount();
        Canvas cvs= new Canvas(bitmap);
        cvs.drawColor(Color.rgb(245, 245, 245));
        Rect r = mRect;
        Paint paint = mPaint;
        mTextPaint.setTextSize(this.getTextSize());
        Paint PaintText = mTextPaint;
    
        for (int i = 0; i < count; i++) {
            int baseline = getLineBounds(i, r);
            cvs.drawText(Integer.toString(i+1), 2, baseline + 1, PaintText);
            cvs.drawLine(2, baseline + 1, r.right, baseline + 1, paint); 
        }
    
        cvs.drawLine(PaintText.measureText(Integer.toString(count))+3, this.getTop(), PaintText.measureText(Integer.toString(count))+3, this.getBottom(), paint);
    
        this.setBackgroundDrawable(new BitmapDrawable(getContext().getResources(), bitmap));
        this.setPadding((int) (PaintText.measureText(Integer.toString(count))+7),3,3,3); 
        super.onDraw(canvas);
    }