Android5.0 development examples Book Reading Notes (5) and android5.0 examples

Source: Internet
Author: User

Android5.0 development examples Book Reading Notes (5) and android5.0 examples

(4) Hardware interaction and media interaction

4.6 custom camera cover

1. Draw the content in Camera to SurfaceView in real time.

To customize the shooting interface, you just need to redefine the surface interface.

The following code is displayed:

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. Change the shooting direction. After setDisplayOrientation (90) is called, the shooting direction is displayed vertically.

mCamera.setDisplayOrientation(90);

4.12 create a listener

1. Obtain the system's SensorManager Service

mSensorManager= (SensorManager) getSystemService(SENSOR_SERVICE);

2. Obtain the Accelerometer Sensor

mAccelerometer=mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

3. The Listener in the main method changes the color in real time.

    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. Register and log out in resume and pause respectively.

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

5. The sensor is very deep. If you really want to learn it, I found a book called "Android sensor advanced programming". If you can't think of it any day, let's see it.

(5) Data Persistence

5.1 create a preference page

1. In the past, such user information or system configuration interfaces were implemented slowly using components. In the past, there was another item called PreferenceActivity.

2. First, the style is defined in XML, and the Directory xml is created under res.

The complete res/xml/settings. xml Code is as follows:

<?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. Next, let the current activity inherit PreferenceActivity

public class SettingsActivity extends AppCompatActivity

4. Finally, bind the xml resource to the current activity.

addPreferencesFromResource(R.xml.color_setting);

5. In addition, the Default preferences can be loaded, which is perfectly combined with 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. However, PreferenceActivity has completed its historical mission. Currently, Google recommends PreferenceFragment.

Very simple. go directly to the source code

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.