Android wear没有为圆形屏幕上的定制手表表面提供合适的布局

Android wear没有为圆形屏幕上的定制手表表面提供合适的布局,android,mobile,android-activity,wear-os,Android,Mobile,Android Activity,Wear Os,我正在为Android Wear定制一款手表。我有一个问题,在模拟器和圆形屏幕的设备上,手表表面没有正确充气。我能够成功地将圆形布局加载到设备上的其他活动中,但它不适用于手表面活动本身 这是我的清单文件: <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.aegiswallet"

我正在为Android Wear定制一款手表。我有一个问题,在模拟器和圆形屏幕的设备上,手表表面没有正确充气。我能够成功地将圆形布局加载到设备上的其他活动中,但它不适用于手表面活动本身

这是我的清单文件:

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

    <uses-feature android:name="android.hardware.type.watch" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.DeviceDefault"
        android:name=".application.AegisWearApplication">

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <activity
            android:name=".activities.MyActivity"
            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:theme="@android:style/Theme.DeviceDefault.NoActionBar"
            android:name=".activities.WatchFaceActivity"
            android:allowEmbedded="true"
            android:label="Aegis Wallet">

            <meta-data
                android:name="com.google.android.clockwork.home.preview"
                android:resource="@drawable/watchface" />

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="com.google.android.clockwork.home.category.HOME_BACKGROUND" />
            </intent-filter>
        </activity>

        <activity android:name=".activities.ReceiveBitcoin" android:label="@string/receive_bitcoin"></activity>
        <service android:name=".services.MessageListenService">
            <intent-filter>
                <action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
            </intent-filter>
        </service>

    </application>
</manifest>

以下是每个项目的代码:

public class MyActivity extends Activity implements SimpleGestureFilter.SimpleGestureListener {

    private SimpleGestureFilter detector;
    private TextView mTextView;
    private TextView balanceView;

    private static final int SPEECH_REQUEST_CODE = 0;
    private QRCodeWriter qrCodeWriter;
    private SharedPreferences prefs;
    private String address;
    private ImageView addressImageView;
    private Context context = this;

    private int STATE_QR = 100;
    private int STATE_BAL = 101;
    private int CURRENT_STATE = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_my);
        WatchViewStub stub = new WatchViewStub(this);

        stub.setRectLayout(R.layout.rect_activity_my);
        stub.setRoundLayout(R.layout.round_activity_my);

        detector = new SimpleGestureFilter(this, this);

        stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
            @Override
            public void onLayoutInflated(WatchViewStub stub) {

                View rootView = stub.getRootView();
                mTextView = (TextView) stub.findViewById(R.id.text);
                addressImageView = (ImageView) stub.findViewById(R.id.address_image);
                balanceView = (TextView) stub.findViewById(R.id.balance_text);

                prefs = PreferenceManager.getDefaultSharedPreferences(context);
                address = prefs.getString("ADDRESS", null);

                if (address != null && addressImageView != null) {

                    qrCodeWriter = new QRCodeWriter();
                    Bitmap addressBitmap = encodeAsBitmap(address, BarcodeFormat.QR_CODE, 200);
                    addressImageView.setImageBitmap(addressBitmap);

                    addressImageView.setVisibility(View.VISIBLE);
                    CURRENT_STATE = STATE_QR;

                } else {
                    Log.d("MAINACTIVITY", "address image view must be null");
                }

                rootView.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
                    @Override
                    public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {

                        final boolean round = insets.isRound();
                        Log.d("MainActivity", "Is the screen round: " + round);
                        return insets;
                    }
                });

            }
        });

        setContentView(stub);

        //displaySpeechRecognizer();
    }

    // Create an intent that can start the Speech Recognizer activity
    private void displaySpeechRecognizer() {
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        // Start the activity, the intent will be populated with the speech text
        startActivityForResult(intent, SPEECH_REQUEST_CODE);
    }

    // This callback is invoked when the Speech Recognizer returns.
    // This is where you process the intent and extract the speech text from the intent.
    @Override
    protected void onActivityResult(int requestCode, int resultCode,
                                    Intent data) {
        if (requestCode == SPEECH_REQUEST_CODE && resultCode == RESULT_OK) {
            List<String> results = data.getStringArrayListExtra(
                    RecognizerIntent.EXTRA_RESULTS);
            String spokenText = results.get(0);

            Log.d("Main", "spoken text: " + spokenText);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    public Bitmap encodeAsBitmap(String contents, BarcodeFormat format, int dimension) {

        Bitmap bitmap = null;

        try {
            final Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
            hints.put(EncodeHintType.MARGIN, 1);
            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
            final BitMatrix result = qrCodeWriter.encode(contents, BarcodeFormat.QR_CODE, dimension, dimension, hints);

            final int width = result.getWidth();
            final int height = result.getHeight();
            final int[] pixels = new int[width * height];

            for (int y = 0; y < height; y++) {
                final int offset = y * width;
                for (int x = 0; x < width; x++) {
                    pixels[offset + x] = result.get(x, y) ? Color.BLACK : Color.WHITE;
                }
            }

            bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
            return bitmap;

        } catch (WriterException e) {
            Log.e("Basic Utils", "cannot write to bitmap " + e.getMessage());
        }
        return bitmap;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent me) {
        // Call onTouchEvent of SimpleGestureFilter class
        this.detector.onTouchEvent(me);
        return super.dispatchTouchEvent(me);
    }

    @Override
    public void onSwipe(int direction) {
        String str = "";

        switch (direction) {

            case SimpleGestureFilter.SWIPE_RIGHT:
                str = "Swipe Right";
                break;
            case SimpleGestureFilter.SWIPE_LEFT:
                str = "Swipe Left";
                break;
            case SimpleGestureFilter.SWIPE_DOWN:
                str = "Swipe Down";
                handleSwipe(SimpleGestureFilter.SWIPE_DOWN);
                break;
            case SimpleGestureFilter.SWIPE_UP:
                str = "Swipe Up";
                handleSwipe(SimpleGestureFilter.SWIPE_UP);
                break;

        }
    }

    @Override
    public void onDoubleTap() {
       Log.d("MyActivity", "Double Tap");
    }

    private void handleSwipe(int swipeType){

        if(CURRENT_STATE == STATE_QR){
            addressImageView.setVisibility(View.GONE);
            balanceView.setVisibility(View.VISIBLE);
            CURRENT_STATE = STATE_BAL;

            balanceView.setText("WALLET BALANCE:\n" + prefs.getString("BALANCE", "Balance not Synced"));
        }
        else if(CURRENT_STATE == STATE_BAL){
            addressImageView.setVisibility(View.VISIBLE);
            balanceView.setVisibility(View.GONE);
            CURRENT_STATE = STATE_QR;
        }


    }
}

public class WatchFaceActivity extends Activity {

    private final static IntentFilter intentFilter;
    private boolean isDimmed = false;

    private String TAG = "AegisWearWatchFace";

    private Handler mHandler;

    TextView time;
    TextView timeAmPm;
    TextView btcValue;
    TextView walletBalance;

    SharedPreferences prefs;

    static {
        intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_TIME_TICK);
        intentFilter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
        intentFilter.addAction(Intent.ACTION_TIME_CHANGED);
    }

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

        Log.d(TAG, "inside the watch face activity");
        WatchViewStub stub = new WatchViewStub(this);
        stub.setRectLayout(R.layout.rect_activity_watch_face);
        stub.setRoundLayout(R.layout.round_activity_watch_face);

        stub.requestApplyInsets();


        stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
            @Override
            public void onLayoutInflated(WatchViewStub stub) {

                Log.d(TAG, "Layout is inflated!");

                time = (TextView) stub.findViewById(R.id.time);
                timeAmPm = (TextView) stub.findViewById(R.id.time_ampm);
                btcValue = (TextView) stub.findViewById(R.id.bitcoin_value);
                walletBalance = (TextView) stub.findViewById(R.id.wallet_balance);

                View rootView = stub.getRootView();

                rootView.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
                    @Override
                    public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
                        final boolean round = insets.isRound();
                        Log.d("WatchFaceActivity", "Is the screen round: " + round);
                        return insets;
                    }
                });

            }
        });



        setContentView(stub);

        prefs = PreferenceManager.getDefaultSharedPreferences(this);

        timeInfoReceiver.onReceive(this, registerReceiver(null, intentFilter));
        registerReceiver(timeInfoReceiver, intentFilter);
    }

    public BroadcastReceiver timeInfoReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context arg0, Intent intent) {
            Log.v("WatchFace", "timeChanged();");
            updateLayout();
        }
    };


    @Override
    protected void onPause() {
        super.onPause();
        isDimmed = true;
        Log.d(TAG, "dimmed");
        updateLayout();
    }

    @Override
    protected void onResume() {
        super.onResume();
        isDimmed = false;
        Log.d(TAG, "not dimmed");
        updateLayout();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(timeInfoReceiver);
    }

    public void updateLayout() {

        Calendar calendar = Calendar.getInstance();

        int hour = calendar.get(Calendar.HOUR);
        int minute = calendar.get(Calendar.MINUTE);
        String am_pm;

        if(calendar.get(Calendar.AM_PM) == 0)   am_pm = "AM";   else   am_pm = "PM";

        String btcValueString = prefs.getString("BTCAMOUNT", "");

        String hourText = hour + "";

        if(hour == 0)
           hour = 12;

        if(hour < 10)
            hourText = "0" + hourText;

        String minuteText = minute + "";
        if(minute < 10)
            minuteText = "0" + minuteText;

        if(time != null) {
            time.setText(hourText + ":" + minuteText);
            timeAmPm.setText(am_pm);
            btcValue.setText(btcValueString);
            walletBalance.setText(prefs.getString("BALANCE", ""));
        }
        else {
            Log.d("WatchFace", "time is null for some reason...");
        }

    }
}
公共类MyActivity扩展活动实现SimpleGetStureFilter.SimpleGetStureListener{
专用单纯形滤波器检测器;
私有文本视图mTextView;
私有文本视图平衡视图;
专用静态最终整型语音请求代码=0;
私人QRCodeWriter QRCodeWriter;
私人共享参考优先权;
私有字符串地址;
私有ImageView地址ImageView;
私有上下文=这个;
私有int STATE_QR=100;
私有int状态_BAL=101;
私有int当前_状态=0;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_my);
WatchViewStub存根=新的WatchViewStub(此);
stub.setRectLayout(R.layout.rect\u activity\u my);
stub.setRoundLayout(R.layout.round\u活动\u my);
检测器=新的SimpleGestureFilter(这个,这个);
stub.setOnlayOutingFlatedListener(新的WatchViewStub.OnlayOutingFlatedListener(){
@凌驾
仅限公共空间布局平坦(WatchViewStub){
View rootView=stub.getRootView();
mTextView=(TextView)stub.findViewById(R.id.text);
addressImageView=(ImageView)stub.findViewById(R.id.address\u image);
balanceView=(TextView)stub.findViewById(R.id.balance\u text);
prefs=PreferenceManager.GetDefaultSharedReferences(上下文);
地址=prefs.getString(“地址”,null);
if(地址!=null&&addressImageView!=null){
qrCodeWriter=新的qrCodeWriter();
位图地址位图=encodeAsBitmap(地址,条形码格式.QR_码,200);
addressImageView.setImageBitmap(addressBitmap);
addressImageView.setVisibility(View.VISIBLE);
当前状态=状态;
}否则{
Log.d(“MAINACTIVITY”,“地址图像视图必须为空”);
}
setonapplyWindowInsertsListener(新视图.onapplyWindowInsertsListener(){
@凌驾
应用程序窗口插图上的公共窗口插图(视图v,窗口插图插图){
最终布尔舍入=insets.isRound();
Log.d(“MainActivity”,“是屏幕圆形:”+圆形);
返回插图;
}
});
}
});
setContentView(存根);
//DisplaySpeechRecognitor();
}
//创建可以启动语音识别器活动的意图
私有void displaySpeechRecognizer(){
意向意向=新意向(识别意向、行动、识别言语);
intent.putExtra(识别器intent.EXTRA_语言_模型,
识别者意图、语言、模型、自由形式);
//开始活动时,将用语音文本填充意图
startActivityForResult(意图、言语请求代码);
}
//语音识别器返回时调用此回调。
//这是您处理意图并从意图中提取语音文本的地方。
@凌驾
ActivityResult上受保护的void(int请求代码、int结果代码、,
意图(数据){
if(requestCode==SPEECH\u REQUEST\u CODE&&resultCode==RESULT\u OK){
列表结果=data.getStringArrayListExtra(
识别者意图。额外结果);
字符串spokenText=results.get(0);
Log.d(“主要”、“口语文本:+spokenText”);
}
super.onActivityResult(请求代码、结果代码、数据);
}
公共位图encodeAsBitmap(字符串内容、条形码格式、整数维){
位图=空;
试一试{
最终哈希表提示=新哈希表();
提示.put(EncodeHintType.MARGIN,1);
提示.put(EncodeHintType.ERROR\u CORRECTION,ErrorCorrectionLevel.H);
最终比特矩阵结果=qrCodeWriter.encode(内容、条形码格式.QR_码、尺寸、尺寸、提示);
final int width=result.getWidth();
最终整数高度=result.getHeight();
最终整数[]像素=新整数[宽度*高度];
对于(int y=0;ypublic static boolean heightSameAsWidth(Context context) {
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();

    DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);

    int width = metrics.widthPixels;
    int height = metrics.heightPixels;

    return height == width;
}

private void checkIfWatchIsRound() {
    if (heightSameAsWidth(getApplicationContext())) {
        isRound = false;
    } else {
        isRound = true;
    }
}