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); }
DrawableView
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.