gpt4 book ai didi

android 二维位图绘制

转载 作者:行者123 更新时间:2023-11-29 18:09:55 24 4
gpt4 key购买 nike

谁能告诉我为什么我不能像这样在我的 surfaceView 上添加位图:

    steering = new Steering(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher), getWidth()-50,getHeight()-50);

如果我使用整数而不是“getHeight()”方法,位图添加得很好。但是因为我希望这款游戏可以在不止一部手机上运行而不会看起来很奇怪,所以我想使用这两种方法添加它。

任何人都可以帮忙吗?

谢谢!

最佳答案

您究竟在哪里添加该行?如果它在您的 onCreate 上,那么它不会向您显示图像,因为方法 getWidth()getHeight() 将返回 0。所以要绘制您必须等到系统实际创建 View 。要测试您是否确实收到了一个值,请尝试更改您实际拥有的代码,如下所示:

final int width = getWidth();
final int height = getHeight();
final Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
steering = new Steering(bitmap, width-50,height-50);

并在转向线上添加一个断点并对其进行调试。如果宽度和高度为 0,则必须等待 View 绘制。

编辑:在您的 Activity/Fragment 上,您可以像这样添加一个树观察者:

 myView.getViewTreeObserver().addOnGlobalLayoutListener( new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
//Do something here since now you have the width and height of your view
}
});

这是一个关于如何在类里面进行的小例子:

我的辅导类:

public class Steering {

private Bitmap mBitmap;
private int mWidth;
private int mHeight;

public Steering(Bitmap bitmap, int width, int height) {
this.mBitmap = bitmap;
this.mWidth = width;
this.mHeight = height;
}

public Bitmap getBitmap() {
//reescaling from anddev.org/resize_and_rotate_image_-_example-t621
final int imageWidth = mBitmap.getWidth();
final int imageHeight = mBitmap.getHeight();
// calculate the scale -
float scaleWidth = ((float) mWidth) / imageWidth;
float scaleHeight = ((float) mHeight) / imageHeight;
// createa matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);

Bitmap resizedBitmap = Bitmap.createBitmap(mBitmap, 0, 0, imageWidth, imageHeight, matrix, true);
return resizedBitmap;
}

}

我的 Activity

public class MainActivity extends Activity {

MyView mView;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mView = (MyView) findViewById(R.id.viewid);
OnGlobalLayoutListener listener = new OnGlobalLayoutListener() {

@Override
public void onGlobalLayout() {
final int width = mView.getWidth();
final int height = mView.getHeight();
final Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.android);
//image from anddev
final Steering steering = new Steering(bitmap, width-50,height-50);
mView.setObject(steering);
}
};
mView.getViewTreeObserver().addOnGlobalLayoutListener(listener);
}

}

和我的 View 类

public class MyView extends View{

Steering steering = null;

public MyView(Context context) {
super(context);
}

public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
}

public void setObject(Steering steering){
this.steering = steering;
}

final Paint paint = new Paint();

@Override
protected void onDraw(Canvas canvas) {
canvas.save();
if(steering!=null){
canvas.drawBitmap(steering.getBitmap(), 0, 0, paint);
}
canvas.restore();
}

}

您可以将其用于普通 View 或 surfaceView,无论哪种方式都有效。对不起,如果答案有点太长:P

关于android 二维位图绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11299001/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com