Android - 底栏重叠SurfaceView

时间:2013-10-07 00:50:47

标签: android surfaceview

好的,所以在我提出这个问题之前,我已经看过了:

How to remove unwanted overlapping in android

Tabs at bottom overlapping with the list view

Android bottom navigation bar overlapping Spinner. Set Spinner dropdown height / margin

Bottom button bar overlaps the last element of Listview!

但是我还没有看到我认为能解决我的特殊情况。 所以这是我的问题:我正在编写一个自定义SurfaceView,它将在屏幕上显示图像。在那个SurfaceView中,我将图像绘制到右下角。这是代码:

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.os.Build;
import android.os.Bundle;
import android.view.Display;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;

public class demosf extends Activity {

    OurView v;
    int Measuredwidth;
    int Measuredheight;
    WindowManager w;
    Bitmap whatever;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Measuredwidth = 0;
        Measuredheight = 0;
        getScreenWidthAndHeight();
        whatever = BitmapFactory.decodeResource(getResources(), R.raw.dragarrow);
        v = new OurView(this);
        setContentView(v);
    }

    public class OurView extends SurfaceView implements Runnable {

        Thread t = null;
        SurfaceHolder holder;
        boolean isItOK;
        public OurView(Context context) {
            super(context);
            holder = getHolder();
        }

        @Override
        public void run() {
            while (isItOK) {
                try {
                    Thread.sleep((long) 50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (!holder.getSurface().isValid()) {
                    continue;
                }
                Canvas c = holder.lockCanvas();
                c.drawARGB(255, 0, 0, 0);
                c.drawBitmap(whatever, ((float) Measuredwidth - (float) whatever.getWidth()), ((float) Measuredheight - (float) whatever.getHeight()), null);
                holder.unlockCanvasAndPost(c);
            }
        }
        public void pause() {
            isItOK = false;
            while (true) {
                try {
                    t.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                break;
            }
            t = null;
        }

        public void resume() {
            isItOK = true;
            t = new Thread(this);
            t.start();
        }
    }
    @Override
    protected void onPause() {
        super.onPause();
        v.pause();
    }
    @Override
    protected void onResume() {
        super.onResume();
        v.resume();
    }

    @SuppressLint("NewApi")
    @SuppressWarnings("deprecation")
    public void getScreenWidthAndHeight() {
        Point size = new Point();
        w = getWindowManager();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
            w.getDefaultDisplay().getSize(size);

            Measuredwidth = size.x;
            Measuredheight = size.y;
        } else {
            Display d = w.getDefaultDisplay();
            Measuredwidth = d.getWidth();
            Measuredheight = d.getHeight();

        }
    }
}

只是要彻底,这里也是清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.something"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.something.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name="com.example.something.demosf"
            android:label="@string/app_name"
            android:screenOrientation="landscape"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
            >
            <intent-filter>
                <action android:name="com.example.something.DEMO" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

    </application>

</manifest>

现在我似乎遇到的问题是,在我的Nexus 7上,这完全符合我的预期。然而,在我的朋友Toshiba Thrive上,图像的底部被底部的条形切断。这是比较: problem Nexus7位于右侧,Thrive位于左侧。所以我的问题是:究竟发生了什么,我该如何解决这个问题呢? (最好适用于所有Android版本) 哦,旁边的问题:那个底栏究竟叫什么?洛尔

编辑:我知道它不是最好的代码,我只是使用这段代码来演示正在发生的事情,并且可能找到一种不同的方法来解决我的“getScreenWidthAndHeight()”方法,这种方法可以解释设备中这种奇怪的重叠它发生在

1 个答案:

答案 0 :(得分:2)

而不是:

c.drawBitmap(whatever, ((float) Measuredwidth - (float) whatever.getWidth()), 
          ((float) Measuredheight - (float) whatever.getHeight()), null);

使用:

c.drawBitmap(whatever, ((float) this.getWidth() - (float) whatever.getWidth()), 
          ((float) this.getHeight() - (float) whatever.getHeight()), null);

原因:

定位Bitmap的方法假设底部没有屏幕装饰。事实上,它根本没有考虑装饰视图。您应该使用“活动”视图的维度来处理定位。

修改

在您的活动中,以下内容将为您提供视图的尺寸:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Measuredwidth = 0;
    Measuredheight = 0;
    getScreenWidthAndHeight();
    whatever = BitmapFactory.decodeResource(getResources(), R.raw.dragarrow);
    v = new OurView(this);

    v.post(new Runnable() {

        @Override
        public void run() {
            Measuredwidth = v.getWidth();
            Measuredheight = v.getHeight();    
        }
    });

    setContentView(v);
}

编辑2:

LinearLayout llMain;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Measuredwidth = 0;
    Measuredheight = 0;
    //getScreenWidthAndHeight();
    whatever = BitmapFactory.decodeResource(getResources(), R.raw.dragarrow);

    llMain = new LinearLayout(this);

    setContentView(llMain);

    llMain.post(new Runnable() {

        @Override
        public void run() {

            Measuredwidth = llMain.getWidth();
            Measuredheight = llMain.getHeight();    

            scaleAndTrimImages();

            v = new OurView(demosf.this);

            llMain.addView(v);                
        }
    });

}

public void scaleAndTrimImages() {

    // Use Measuredwidth and Measuredheight
    // Since you are calling this method from onCreate(Bundle),
    // it runs only once.

}