[英]android 2D bitmap drawing
可以任意告诉我为什么我不能在我的surfaceView上添加一个位图,如下所示:
steering = new Steering(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher), getWidth()-50,getHeight()-50);
如果我使用整数而不是“getHeight()” - 方法,则添加位图就好了。 但是因为我希望这个游戏可以在超过1个手机上运行而不会看起来很奇怪,我想用这两种方法添加它。
可以帮忙吗?
谢谢!
你在哪里添加那条线? 如果它在你的onCreate
然后它不会显示你的图像,因为方法getWidth()
和getHeight()
将返回0.所以要绘制它你必须等到系统实际创建了视图。 要测试您实际上是在接收值,请尝试更改您实际拥有的代码,例如:
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,那么你将不得不等待视图绘制。
编辑:在你的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;
}
}
我的活动
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);
}
}
和我的视图类
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();
}
}
无论哪种方式都可以,您可以将它用于普通视图或surfaceView。 对不起,如果答案有点太长:P
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.