我有这个xml,布局表面(不覆盖整个屏幕):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/FrameLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="bottom"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="10dp"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="@+id/surface"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1" >
</LinearLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_gravity="bottom" >
<ImageView
android:id="@+id/right"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignTop="@+id/down"
android:layout_toRightOf="@+id/down"
android:src="@drawable/right" />
<ImageView
android:id="@+id/left"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignTop="@+id/down"
android:layout_toLeftOf="@+id/down"
android:src="@drawable/left" />
<ImageView
android:id="@+id/up"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/right"
android:src="@drawable/up" />
<ImageView
android:id="@+id/down"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_below="@+id/up"
android:layout_centerHorizontal="true"
android:src="@drawable/down" />
</RelativeLayout>
</LinearLayout>
我控制了mainActivity中的surface
并尝试在其边框上绘制一个宽度为10px的框架。
这里是mainActivity:
public class MainActivity extends Activity {
GameView gv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game_view);
LinearLayout surface = new LinearLayout(this);
surface = (LinearLayout)findViewById(R.id.surface);
gv= new GameView(this,surface);
surface.addView(gv);
}
}
这是GameView类:
public class GameView extends SurfaceView implements Runnable {
private boolean isRunning=false;
private final long FPS=12;
int PART_SIZE=6;
private SurfaceHolder holder;
Thread snakethread;
LinearLayout view;
public GameView(Context context,LinearLayout view) {
super(context);
this.view=view;
this.a=new Arena();
snakethread=new Thread(this);
holder= getHolder();
this.setBackgroundColor(Color.TRANSPARENT);
this.setZOrderOnTop(true);
getHolder().setFormat(PixelFormat.TRANSPARENT);
holder.addCallback(new SurfaceHolder.Callback(){
@Override
public void surfaceCreated(SurfaceHolder arg0) {
setRunning(true);
snakethread.start();
}
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2,int arg3) {
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
setRunning(false);
while(true){
try {
snakethread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
}
public void run() {
long stepPerSecond=1000/FPS;
long startTime;
long sleepTime;
Canvas c = null;
try{
c=this.getHolder().lockCanvas();
synchronized (this.getHolder()) {
c.drawColor(Color.BLUE);
Paint p=new Paint();
p.setColor(Color.BLACK);
c.drawRect(view.getLeft(), view.getTop(), view.getLeft()+10, view.getBottom(),p);
c.drawRect(view.getLeft(), view.getTop(),view.getRight(),view.getTop()+10,p);
c.drawRect(view.getRight()-10, view.getTop(), view.getRight(),view.getBottom(),p);
c.drawRect(view.getLeft(), view.getBottom()-10, view.getRight(),view.getBottom(),p);
}
}
catch(Exception e){
}
finally{
if(c!=null){
this.getHolder().unlockCanvasAndPost(c);
}
}
}
问题是整个帧在视图的顶部下方约为6px,应该在哪里。我的意思是view.getTop()
和view.getBottom()
返回一个结果,结果大约是6px然后它应该是什么(如果视图的顶部坐标是x,那么它返回x + 6)。
答案 0 :(得分:2)
花了2天后我找到了解决方案
int top = view.getTop();
int bottom = view.getBottom();
// use these line instead of upper code
int statusBarHeight = (int) Math.ceil(25 * context.getResources().getDisplayMetrics().density);
int top = (childTextView.getBottom()+childTextView.getHeight())-statusBarHeight ;
int bottom = (childTextView.getTop()+childTextView.getHeight())-statusBarHeight ;