Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> Android5.0開發范例大全 讀書筆記(五),android5.0范例

Android5.0開發范例大全 讀書筆記(五),android5.0范例

編輯:關於android開發

Android5.0開發范例大全 讀書筆記(五),android5.0范例


(四)實現設備硬件交互與媒體交互

4.6自定義攝像頭覆蓋層

1.將Camera中的內容實時的繪制到SurfaceView中

  若要自定義拍攝界面,只要重新定義surface的界面即可

  以下展示全部代碼

public class PreviewActivity extends AppCompatActivity implements SurfaceHolder.Callback {
    Camera mCamera;
    SurfaceView mPreview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_preview);
        mPreview= (SurfaceView) findViewById(R.id.preview);
        assert mPreview != null;
        mPreview.getHolder().addCallback(this);
        mPreview.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        mCamera=Camera.open();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mCamera.stopPreview();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mCamera.release();
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        try {
            mCamera.setPreviewDisplay(mPreview.getHolder());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        Camera.Parameters params=mCamera.getParameters();
        List<Camera.Size> sizes=params.getSupportedPreviewSizes();
        Camera.Size selected=sizes.get(0);
        params.setPreviewSize(selected.width,selected.height);
        mCamera.setParameters(params);
        mCamera.setDisplayOrientation(90);
        mCamera.startPreview();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }
}

2.改變拍攝方向,調用setDisplayOrientation(90)後,拍攝方向方向才會垂直顯示

mCamera.setDisplayOrientation(90);

4.12創建傾斜監聽器

1.獲取系統的SensorManager服務

mSensorManager= (SensorManager) getSystemService(SENSOR_SERVICE);

2.獲取加速度感知器

mAccelerometer=mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

3.主方法中的監聽,實時改變顏色

    public void onSensorChanged(SensorEvent event) {
        float[] values = event.values;
        float x = values[0] / 10;
        float y = values[1] / 10;
        int scaleFactor;
        if (x > 0) {
            scaleFactor = (int) Math.min(x * 255, 255);
            mRight.setBackgroundColor(Color.TRANSPARENT);
            mLeft.setBackgroundColor(Color.argb(scaleFactor, 255, 0, 0));
        } else {
            scaleFactor = (int) Math.min(Math.abs(x) * 255, 255);
            mRight.setBackgroundColor(Color.argb(scaleFactor, 255, 0, 0));
            mLeft.setBackgroundColor(Color.TRANSPARENT);
        }
        if (y > 0) {
            scaleFactor = (int) Math.min(y * 255, 255);
            mTop.setBackgroundColor(Color.TRANSPARENT);
            mBottom.setBackgroundColor(Color.argb(scaleFactor, 255, 0, 0));
        } else {
            scaleFactor = (int) Math.min(Math.abs(y) * 255, 255);
            mTop.setBackgroundColor(Color.argb(scaleFactor, 255, 0, 0));
            mBottom.setBackgroundColor(Color.TRANSPARENT);
        }
        valueView.setText(String.format("X:%1$1.2f,Y:%2$1.2f,Z:%3$1.2f", values[0], values[1], values[2]));

4.分別在resume和pause中注冊和注銷

 @Override
    protected void onResume() {
        super.onResume();
        mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_UI);
    }
 @Override
    protected void onPause() {
        super.onPause();
        mSensorManager.unregisterListener(this);
    }

5.傳感器這塊水很深,真要學的話我發現一本書叫《Android傳感器高級編程》,哪天想不開了就去看看吧。

(五)數據持久化

5.1制作首選項界面

1.以前這種用戶信息或者系統配置的界面都是自己用組件慢慢實現,原來還有個叫PreferenceActivity的東西

2.首先,樣式還是通過XML定義,在res下新建目錄xml

  以下是res/xml/settings.xml的完整代碼

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <EditTextPreference
        android:defaultValue="Joshua"
        android:key="namePref"
        android:summary="Tell us your name"
        android:title="Name" />
    <CheckBoxPreference
        android:defaultValue="false"
        android:key="morePref"
        android:title="Enable More Settings" />
    <PreferenceScreen
        android:dependency="morePref"
        android:key="moreScreen"
        android:title="More Settings">
        <ListPreference
            android:defaultValue="GRN"
            android:entries="@array/color_names"
            android:entryValues="@array/color_values"
            android:key="colorPref"
            android:summary="Choose your favorite color"
            android:title="Favorite Color" />

        <PreferenceCategory android:title="LOCATION SETTINGS">
            <CheckBoxPreference
                android:defaultValue="true"
                android:key="gpsPref"
                android:summary="use gps to find you"
                android:title="Use GPS Location" />
            <CheckBoxPreference
                android:defaultValue="true"
                android:key="networkPref"
                android:summary="use NetWork to find you"
                android:title="Use NetWork Location" />

        </PreferenceCategory>
    </PreferenceScreen>
</PreferenceScreen>

3.接著,讓當前activity繼承PreferenceActivity

public class SettingsActivity extends AppCompatActivity

4.最後,將xml資源與當前activity綁定

addPreferencesFromResource(R.xml.color_setting);

5.此外,還可以加載默認的首選項內容,與SharedPreferences結合起來十分完美

PreferenceManager.setDefaultValues(SettingsActivity.this, R.xml.color_setting, false);
@Override
    protected void onResume() {
        super.onResume();
        SharedPreferences settings=PreferenceManager.getDefaultSharedPreferences(this);
        String name=settings.getString("namePref","");
        boolean isMoreEnabled=settings.getBoolean("morePref",false);
    }

6.不過,PreferenceActivity已經完成了其歷史使命,現在Google推薦使用PreferenceFragment

  很簡單,直接上源碼

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        PreferenceManager.setDefaultValues(SettingsActivity.this, R.xml.settings, false);
        getFragmentManager().beginTransaction().replace(R.id.preference, new PrefsFragment()).commit();


    }

    public static class PrefsFragment extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.settings);

        }
    }

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved