draw object/image on canvas

前端 未结 2 1305
故里飘歌
故里飘歌 2020-12-02 14:03

Is there another way to draw an object on a canvas in android?

This code inside draw() doesn\'t work:

Bitmap bmp = BitmapFactory.decodeResource(getResour         


        
2条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-02 14:18

    I prefer to do this as it only generates the image once:

    public class CustomView extends View {
    
        private Drawable mCustomImage;
    
        public CustomView(Context context, AttributeSet attrs) {
            super(context, attrs);
            mCustomImage = context.getResources().getDrawable(R.drawable.my_image);
        }
    
        ...
    
        protected void onDraw(Canvas canvas) {
            Rect imageBounds = canvas.getClipBounds();  // Adjust this for where you want it
    
            mCustomImage.setBounds(imageBounds);
            mCustomImage.draw(canvas);
        }
    }
    

提交回复
热议问题