我正面对一个需要一些物理学的应用程序而且我决定使用带有box2d扩展名的Andengine。
从Andengine示例和一些(很多)谷歌搜索开始,我最终得到了我需要的结构:一个包含RenderSurfaceView的标准android布局,其中发生了物理内容。
BTW我现在有一个问题,经过多次尝试后我无法解决: 这是我的屏幕
黑匣子是物理事物发生的地方,而且evertything按预期工作,但我需要它有一个透明的背景。我尝试了很多东西,但没办法。
这是我的xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/main_gradient_background">
<LinearLayout
android:background="@android:color/transparent"
android:id="@+id/main2_dataContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/border_top_white"
android:orientation="vertical"
android:paddingTop="15dp">
<com.squadrone.android.ui.SuperLabel
android:id="@+id/btn_get_involved"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp"
android:gravity="center"
android:visibility="invisible"
android:text="12,29502 BTC = 4862,51 EUR"
android:textColor="@color/white"
android:textSize="20sp"/>
<com.squadrone.android.ui.SuperButton
android:id="@+id/btnContinueSell"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:background="#0099FF"
android:gravity="center"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="TEST"
android:textColor="@color/white"
android:textSize="@dimen/medium_text"/>
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:id="@+id/main2_physicContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_red_dark"
android:layout_above="@+id/main2_dataContainer"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp">
<org.andengine.opengl.view.RenderSurfaceView
android:id="@+id/xmllayoutexample_rendersurfaceview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="0px"
android:background="@color/transparent"
android:alpha="1"/>
<com.squadrone.android.ui.SuperLabel
android:id="@+id/btn_get_involved"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp"
android:gravity="center"
android:text="12€"
android:textColor="@color/white"
android:textSize="40sp"/>
</RelativeLayout>
</RelativeLayout>
这是我的活动
public class MyPhysicsActivity extends SimpleLayoutGameActivity implements IAccelerationListener, IOnSceneTouchListener {
// ===========================================================
// Constants
// ===========================================================
protected static final int CAMERA_WIDTH = ConioApplication.getScreenWidth();
protected static final int CAMERA_HEIGHT = ConioApplication.getScreenHeight();
// ===========================================================
// Fields
// ===========================================================
protected ITiledTextureRegion mCircleFaceTextureRegion;
protected PhysicsWorld mPhysicsWorld;
private BitmapTextureAtlas mBitmapTextureAtlas;
private Scene mScene;
private int mFaceCount = 0;
@Override
protected int getLayoutID() {
return R.layout.xmllayoutexample;
}
@Override
protected int getRenderSurfaceViewID() {
return R.id.xmllayoutexample_rendersurfaceview;
}
@Override
public EngineOptions onCreateEngineOptions() {
Toast.makeText(this, "Touch the screen to add objects.", Toast.LENGTH_LONG).show();
final Camera camera = new Camera(0, 0, MyPhysicsActivity.CAMERA_WIDTH, MyPhysicsActivity.CAMERA_HEIGHT);
return new EngineOptions(true, ScreenOrientation.PORTRAIT_FIXED, new
FixedResolutionPolicy(MyPhysicsActivity.CAMERA_WIDTH, MyPhysicsActivity.CAMERA_HEIGHT), camera);
}
@Override
protected void onCreateResources() {
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 64, 64, TextureOptions.BILINEAR);
this.mCircleFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "face_circle_tiled.png", 0, 0, 1, 1); // 64x32
this.mBitmapTextureAtlas.load();
}
@Override
protected void onSetContentView() {
super.onSetContentView();
}
@Override
protected Scene onCreateScene() {
Debug.d("oncreate scene");
this.mEngine.registerUpdateHandler(new FPSLogger());
this.mScene = new Scene();
this.mScene.setBackground(new Background(Color.TRANSPARENT));
this.mScene.setColor(Color.TRANSPARENT);
this.mScene.setOnSceneTouchListener(this);
this.mPhysicsWorld = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH), false);
final VertexBufferObjectManager vertexBufferObjectManager = this.getVertexBufferObjectManager();
int h = findViewById(R.id.main2_dataContainer).getMeasuredHeight();
int marginBottom = ((RelativeLayout.LayoutParams) findViewById(R.id.main2_dataContainer).getLayoutParams()).bottomMargin;
int baseX = h + marginBottom;
int martingLeft = ((RelativeLayout.LayoutParams) findViewById(R.id.main2_physicContainer).getLayoutParams()).leftMargin;
int martingRight = ((RelativeLayout.LayoutParams) findViewById(R.id.main2_physicContainer).getLayoutParams()).rightMargin;
int physicsW = (CAMERA_WIDTH) - martingLeft - martingRight;
int physicsH = (CAMERA_HEIGHT) - marginBottom - h;
final Rectangle ground = new Rectangle(0 + (physicsW / 2), baseX + 0, physicsW, 0, vertexBufferObjectManager);
final Rectangle roof = new Rectangle(0 + (physicsW / 2), (CAMERA_HEIGHT - 0), physicsW, 0, vertexBufferObjectManager);
final Rectangle left = new Rectangle(0,baseX+physicsH/2, 0, physicsH, vertexBufferObjectManager);
final Rectangle right = new Rectangle(physicsW-0, baseX+physicsH/2, 0, physicsH, vertexBufferObjectManager);
// right.setColor(Color.YELLOW);
// ground.setColor(Color.RED);
// roof.setColor(Color.YELLOW);
// left.setColor(Color.GREEN);
// right.setColor(Color.WHITE);
final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, ground, BodyType.StaticBody, wallFixtureDef);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, roof, BodyType.StaticBody, wallFixtureDef);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, left, BodyType.StaticBody, wallFixtureDef);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, right, BodyType.StaticBody, wallFixtureDef);
this.mScene.attachChild(ground);
this.mScene.attachChild(roof);
this.mScene.attachChild(left);
this.mScene.attachChild(right);
this.mScene.registerUpdateHandler(this.mPhysicsWorld);
return this.mScene;
}
@Override
public boolean onSceneTouchEvent(final Scene pScene, final TouchEvent pSceneTouchEvent) {
if (this.mPhysicsWorld != null) {
if (pSceneTouchEvent.isActionDown()) {
this.addItem(pSceneTouchEvent.getX(), pSceneTouchEvent.getY());
return true;
}
}
return false;
}
@Override
public void onAccelerationAccuracyChanged(final AccelerationData pAccelerationData) {
}
@Override
public void onAccelerationChanged(final AccelerationData pAccelerationData) {
final Vector2 gravity = Vector2Pool.obtain(pAccelerationData.getX(), pAccelerationData.getY());
this.mPhysicsWorld.setGravity(gravity);
Vector2Pool.recycle(gravity);
}
@Override
public void onResumeGame() {
super.onResumeGame();
this.enableAccelerationSensor(this);
}
@Override
public void onPauseGame() {
super.onPauseGame();
this.disableAccelerationSensor();
}
// ===========================================================
// Methods
// ===========================================================
private void addItem(final float pX, final float pY) {
Debug.d("add item");
this.mFaceCount++;
Debug.d("items: " + this.mFaceCount);
final Sprite face;
final Body body;
final FixtureDef objectFixtureDef = PhysicsFactory.createFixtureDef(1, 0.5f, 0f);
face = new Sprite(pX, pY, this.mCircleFaceTextureRegion, this.getVertexBufferObjectManager());
face.setScale(MathUtils.random(0.5f, 1.25f));
body = PhysicsFactory.createCircleBody(this.mPhysicsWorld, face, BodyType.DynamicBody, objectFixtureDef);
this.mScene.attachChild(face);
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face, body, true, true));
}
}
我尝试了这个没有运气的链接
AndEngine Applying Transparancy to AndEngine View
ANDROID ANDENGINE -- Set Scene background as trasparent
有可能做我需要的吗?
谢谢
答案 0 :(得分:0)
我将AndEngine更改为View使用,并且在透明度方面存在同样的问题。
Here你可以找到我解决它的简单例子。
在屏幕截图中拍摄:此处是#880000背景和AndEngine视图,背景为#22000000,并且20dp徘徊。