画布坐标不准确地显示我的位图

时间:2014-03-04 17:54:49

标签: java android coordinates android-canvas ondraw

对于那些喜欢这个问题的人,请给它一个upvote

我正在使用一个扩展View的类。在本课程中,我使用onDraw方法进行绘图。

当我启动我的应用程序时,一切都按预期工作,除了一点烦人的事情......每当我按下我的屏幕上的任何地方,绘图,计算我触摸屏幕的位置的坐标 - 并且应该绘制图像在那些精确的坐标中,将它们拉离我最初按下的位置。

例如,如果我在x = 150,y = 250时点击屏幕,图像将以大量像素显示。

要清楚的是,当我按下屏幕时,我希望我的图像完全显示在我的手指下或非常靠近我拍摄的位置,但是,图像通常显示在按下发生的位置下方100个左右的像素。

使用画布绘制时,有什么我不了解的坐标吗?

这是我的代码

public class PixelParticle extends View{

    private float locationX;
    private float locationY;
    private int sizeX;
    private int sizeY;
    private Paint color;
    private Rect rect;
    private int paintcolor;
    private float myLocX;
    private float myLocY;


    public PixelParticle(Context context, float locationX, float locationY, int sizeX, int sizeY, int paintcolor) {
        super(context);

        this.locationX = locationX;
        this.locationY = locationY;
        this.sizeX = sizeX;
        this.sizeY = sizeY;
        this.paintcolor = paintcolor;

        rect = new Rect();
        color = new Paint();
    }

    @Override
    protected void onDraw(Canvas canvas){
        super.onDraw(canvas);

        Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        Bitmap resizedBitmap = Bitmap.createScaledBitmap(myBitmap, 250, 250, true);
        canvas.drawBitmap(resizedBitmap,locationX ,locationY , null);
    }
}

和MainActivity ..

public class MainActivity extends Activity {

    private int pixelColor;
    private float x;
    private float y;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

     @Override
        public boolean onTouchEvent(MotionEvent event) {
            x = event.getX();
            y = event.getY();

            pixelColor = Color.RED;
            PixelParticle pp = new PixelParticle(this, x, y, 50, 50, pixelColor);
            setContentView(pp); 
            return false;
        }
}

1 个答案:

答案 0 :(得分:1)

屏幕的高度包括标题和通知栏。  使活动全屏并删除标题将解决问题。 试试这个:

... PixelParticle

    public class PixelParticle extends View {

private float locationX;
private float locationY;
private int sizeX;
private int sizeY;
private Paint color;
private Rect rect;
private int paintcolor;
private float myLocX;
private float myLocY;

public PixelParticle(Context context, float locationX, float locationY,
        int sizeX, int sizeY, int paintcolor) {
    super(context);

    this.locationX = locationX;
    this.locationY = locationY;
    this.sizeX = sizeX;
    this.sizeY = sizeY;
    this.paintcolor = paintcolor;

    rect = new Rect();
    color = new Paint();
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    Bitmap myBitmap = BitmapFactory.decodeResource(getResources(),
            R.drawable.ic_launcher);
    Bitmap resizedBitmap = Bitmap.createScaledBitmap(myBitmap, 250, 250,
            true);
    canvas.drawBitmap(resizedBitmap, locationX
            - (resizedBitmap.getWidth() / 2),
            locationY - (resizedBitmap.getHeight() / 2), null);

}}

MainActivity ..

    public class MainActivity extends Activity {

private int pixelColor;
private float x;
private float y;
PixelParticle pp ;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

 @Override
    public boolean onTouchEvent(MotionEvent event) {
        x = event.getX();
        y = event.getY();

        pixelColor = Color.RED;
         pp = new PixelParticle(this, x, y, 50, 50, pixelColor);
        setContentView(pp); 
        return false;
    }}