Android5.0开发范例大全 读书笔记
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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); } }
以上是关于Android5.0开发范例大全 读书笔记的主要内容,如果未能解决你的问题,请参考以下文章