BitmapDrawable mBitmapDrawable ; int x=1; int y = 1; int w=0; int h=0; public DrawableView(Context context) { super(context); /*读取res->drawable文件下的图片*/ mBitmapDrawable = (BitmapDrawable) getResources().getDrawable(R.drawable.box); //w = mBitmapDrawable.getIntrinsicWidth();//460 // h = mBitmapDrawable.getIntrinsicHeight();//434 DisplayMetrics dm = new DisplayMetrics(); w = dm.widthPixels; h=dm.heightPixels; Log.i("test",w+","+h); if (mBitmapDrawable != null) { mBitmapDrawable.setBounds(x,y,w,h);//画一个矩形,x,y是起点,w是矩形的宽,h是矩形的高,图片就显示在矩形中 mBitmapDrawable.setGravity(Gravity.CENTER_HORIZONTAL);//图片在矩形中居中显示 } // mBitmapDrawable.setAntiAlias(true); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); mBitmapDrawable.draw(canvas); Paint p = mBitmapDrawable.getPaint(); p.setTextSize(30); p.setColor(Color.BLUE); canvas.drawText("宝箱",50,50,p); }
时间: 2024-10-13 15:41:54