0

我正在做的应用程序在画布中显示一个图像。它在一台设备上运行良好,但是如果我在另一台设备上运行相同的应用程序,则图像无法正确显示,因为在此onDraw我没有提及imageview 的宽度或高度,所以任何人都可以建议我如何给它设置宽度和高度侧面方法中的图像视图。onDraw

我的代码是:

@Override
public void onDraw(Canvas canvas) {
    Paint painto = new Paint();
    painto.setAntiAlias(true);
    painto.setColor(getResources().getColor(R.color.magnata));
    painto.setStrokeWidth(3);
    painto.setStyle(Paint.Style.FILL);
    Paint paint = new Paint();
    int leftx2 = (int) (10 * (screenWidth / 1024));
    int topy1 = (int) (10 * (screenHeight / 600));

    Bitmap kangoo = BitmapFactory.decodeResource(getResources(),
            R.drawable.kangoo);
    canvas.drawColor(Color.BLACK);
    canvas.drawBitmap(kangoo, leftx2, topy1, painto);
}
4

2 回答 2

1

您可以使用 onSizeChange() 设置宽度和高度。这可以为您提供不断变化的宽度和高度

protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        // TODO Auto-generated method stub
        super.onSizeChanged(w, h, oldw, oldh);
         newwidth=w;
                 newheight=h;
    }
于 2012-11-15T10:43:36.847 回答
1

像这样试试

@Override  
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            // TODO Auto-generated method stub
            super.onSizeChanged(w, h, oldw, oldh);
             newwidth=w;
                     newheight=h;
        }
@Override
    public void onDraw(Canvas canvas) {
        Paint painto = new Paint();
        painto.setAntiAlias(true);
        painto.setColor(getResources().getColor(R.color.magnata));
        painto.setStrokeWidth(3);
        painto.setStyle(Paint.Style.FILL);
        Paint paint = new Paint();
        int leftx2 = (int) (10 * (newwidth / 1024));
        int topy1 = (int) (10 * (newheight / 600));

        Bitmap kangoo = BitmapFactory.decodeResource(getResources(),
                R.drawable.kangoo);
        canvas.drawColor(Color.BLACK);
        canvas.drawBitmap(kangoo, leftx2, topy1, painto);
    }
于 2012-11-15T10:59:52.223 回答