1

好的,所以在我问这个问题之前,我看过:

如何在android中删除不需要的重叠

底部的选项卡与列表视图重叠

Android 底部导航栏重叠 Spinner。设置 Spinner 下拉高度/边距

底部按钮栏与 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 上,图像的底部被底部的条截断了。这是比较: 问题 Nexus7 在右边,Thrive 在左边。所以我的问题是:到底发生了什么,我该如何解决这个问题?(最好适用于所有 Android 版本)哦,附带问题:那个底栏到底叫什么?哈哈

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

4

1 回答 1

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);

原因:

您定位位图的方法假定底部没有屏幕装饰。事实上,它根本不考虑装饰视图。您应该使用 Activity 视图的尺寸来处理定位。

编辑:

在您的活动的情况下,以下将为您提供视图的尺寸:

@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.

}
于 2013-10-07T01:33:27.647 回答