Draw rectangle with XML shape settings in Android

前端 未结 1 438
暖寄归人
暖寄归人 2021-01-16 04:39

I\'ve got a problem which I don\'t know how to solve. Please, help me if you can. In my app I have to create a custom view extended View. In this view I should draw a lot of

1条回答
  •  生来不讨喜
    2021-01-16 05:06

    You can load and use the XML defined drawable from code like so:

    public class CustomView extends View {
    
        Drawable shape;
    
        public CustomView(Context context) {
            super(context);
            shape = context.getResources().getDrawable(R.drawable.shape);
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
    
            shape.setBounds(left, top, right, bottom);
            shape.draw(canvas)
        }
    
        // ... Additional methods omitted for brevity
    
    }
    

    0 讨论(0)
提交回复
热议问题