我正在做的应用程序在画布中显示一个图像。它在一台设备上运行良好,但如果我在其他设备上运行相同的应用程序,则图像无法正确显示,因为在onDraw
我没有提及 imageview 宽度或高度,所以任何一个你可以建议我如何在onDraw
方法中为 imageview 提供宽度和高度。
我的代码是:
@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);
}
答案 0 :(得分: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;
}
答案 1 :(得分: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);
}