我想我在我的screen..im的中心位尝试这种方式,但它不工作...
位图MYBITMAP = BitmapFactory.de codeResource(getResources(),R.drawable.compass);
INT W = canvas.getWidth();
INT H = canvas.getHeight();
INT体重= myBitmap.getWidth();
INT BH = myBitmap.getHeight();
INT CX = W / 2;
INT CY = H / 2;
显示D = getWindowManager()getDefaultDisplay()。
INT X = d.getWidth();
INT Y = d.getHeight();
INT DX = X / 2;
INT DW = Y / 2;
canvas.translate(CX,CY);
如果(mValues!= NULL){
canvas.rotate(-mValues [0]);
}
INT CENTREX =(X - 体重)/ 2;
INT centreY =(CY - BH)/ 2;
//canvas.drawPath(mPath,mPaint);
canvas.drawBitmap(MYBITMAP,CENTREX,centreY,NULL);
解决方案
这里的code,你需要在你的看法:
私人诠释mWidth;
私人诠释mHeight;
私人浮动裂伤;
@覆盖保护无效onMeasure(INT widthMeasureSpec,INT heightMeasureSpec)
{
mWidth = View.MeasureSpec.getSize(widthMeasureSpec);
mHeight = View.MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(mWidth,mHeight);
}
@覆盖保护无效的OnDraw(帆布油画)
{
super.onDraw(画布);
位图MYBITMAP = BitmapFactory.de codeResource(getResources(),R.drawable.compass);
//这里的神奇。无论你做到这一点,其逻辑是:
//可用空间 - 位图大小和除以二的结果。
//必须有对图像的两侧像素的量相等。
//因此无论空间中显示的图像后,左,一半进入
//左/上半向右/向下。你减去的可用空间
//图像的宽度/高度从屏幕尺寸。祝你好运。
INT CX =(mWidth - myBitmap.getWidth())>> 1; //相同(...)/ 2
INT CY =(mHeight - myBitmap.getHeight())>> 1;
如果(裂伤大于0){
canvas.rotate(裂伤,mWidth>> 1,mHeight>> 1);
}
canvas.drawBitmap(MYBITMAP,CX,CY,NULL);
}
截图只是为了好玩: http://imgur.com/EYpMJ (对角线的code不属于这里张贴)
编辑:添加NickT的解决方案。 编辑2:更改mvalues [0],裂伤,并使其有条件。改变除以2操作bitshifts。删除旋转code,如果你不需要它。
i want my bitmap in the center of my screen..i m trying it that way but its not working...
Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.compass);
int w = canvas.getWidth();
int h = canvas.getHeight();
int bw=myBitmap.getWidth();
int bh=myBitmap.getHeight();
int cx = w / 2;
int cy = h / 2;
Display d = getWindowManager().getDefaultDisplay();
int x = d.getWidth();
int y = d.getHeight();
int dx = x / 2;
int dw = y /2;
canvas.translate(cx, cy);
if (mValues != null) {
canvas.rotate(-mValues[0]);
}
int centreX = (x - bw) /2;
int centreY = (cy - bh) /2;
//canvas.drawPath(mPath, mPaint);
canvas.drawBitmap(myBitmap, centreX,centreY, null);
解决方案
Here's the code you need in your view:
private int mWidth;
private int mHeight;
private float mAngle;
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
mWidth = View.MeasureSpec.getSize(widthMeasureSpec);
mHeight = View.MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(mWidth, mHeight);
}
@Override protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.compass);
// Here's the magic. Whatever way you do it, the logic is:
// space available - bitmap size and divide the result by two.
// There must be an equal amount of pixels on both sides of the image.
// Therefore whatever space is left after displaying the image, half goes to
// left/up and half to right/down. The available space you get by subtracting the
// image's width/height from the screen dimensions. Good luck.
int cx = (mWidth - myBitmap.getWidth()) >> 1; // same as (...) / 2
int cy = (mHeight - myBitmap.getHeight()) >> 1;
if (mAngle > 0) {
canvas.rotate(mAngle, mWidth >> 1, mHeight >> 1);
}
canvas.drawBitmap(myBitmap, cx, cy, null);
}
Screenshot just for fun: http://imgur.com/EYpMJ (Diagonal lines not part of the code posted here)
EDIT: Added NickT's solution. EDIT 2: Changed mvalues[0] to mAngle and made it conditional. Changed divide by 2 operations to bitshifts. Remove rotation code if you don't need it.