自己定制一个view(利用代码实现,而不是用xml)。怎么在特定位置添加一个图片?

解决方案 »

  1.   

    onDraw内画图片
     public class MyView extends View {
            Bitmap bitmap;
            int posX,posY;
      
         MyView(Context context) {
           super(context);
         }
            
            void setBitmap(Bitmap bm,int x,int y)
            {
               bitmap=bm;
               posX=x;
               posY=y;
            } @Override
    protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
                    //在这里画
                      if(bitmap!=null)
                      canvas.drawBitmap(bitmap, posX, posY, null); 
    }
     
        }
    }
      

  2.   

    @Override
    protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.onDraw(canvas);
    }