2015年10月15日 星期四

Android Layout getHeight = 0問題解決

在onWindowFocusChanged取得就可以

@Override
 public void onWindowFocusChanged(boolean hasFocus) {
RelativeLayout.getHeight();

}

2015年7月22日 星期三

Android 相機 切換鏡頭

出處:http://stackoverflow.com/questions/16765527/android-switch-camera-when-button-clicked



Button otherCamera = (Button) findViewById(R.id.OtherCamera);

OtherCamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (inPreview) {
    camera.stopPreview();
}
//NB: if you don't release the current camera before switching, you app will crash
camera.release();

//swap the id of the camera to be used
if(currentCameraId == Camera.CameraInfo.CAMERA_FACING_BACK){
    currentCameraId = Camera.CameraInfo.CAMERA_FACING_FRONT;
}
else {
    currentCameraId = Camera.CameraInfo.CAMERA_FACING_BACK;
}
camera = Camera.open(currentCameraId);

setCameraDisplayOrientation(CameraActivity.this, currentCameraId, camera);
try {

    camera.setPreviewDisplay(previewHolder);
} catch (IOException e) {
    e.printStackTrace();
}
camera.startPreview();
}
If you want to make the camera image show in the same orientation as the display, you can use the following code.
public static void setCameraDisplayOrientation(Activity activity,
         int cameraId, android.hardware.Camera camera) {
     android.hardware.Camera.CameraInfo info =
             new android.hardware.Camera.CameraInfo();
     android.hardware.Camera.getCameraInfo(cameraId, info);
     int rotation = activity.getWindowManager().getDefaultDisplay()
             .getRotation();
     int degrees = 0;
     switch (rotation) {
         case Surface.ROTATION_0: degrees = 0; break;
         case Surface.ROTATION_90: degrees = 90; break;
         case Surface.ROTATION_180: degrees = 180; break;
         case Surface.ROTATION_270: degrees = 270; break;
     }

     int result;
     if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
         result = (info.orientation + degrees) % 360;
         result = (360 - result) % 360;  // compensate the mirror
     } else {  // back-facing
         result = (info.orientation - degrees + 360) % 360;
     }
     camera.setDisplayOrientation(result);
 }

2015年6月16日 星期二

Android 變更Toast位置

Toast toast=Toast.makeText(mActivity,"test", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER, 0, 0);
toast.show();

2015年5月21日 星期四

Android ScrollView + WebView Layout

<ScrollView
        android:id="@+id/my_scroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:descendantFocusability="blocksDescendants"
            android:orientation="vertical" >

            <WebView
                android:id="@+id/my_web"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="125dp" >
            </WebView>
        </LinearLayout>
    </ScrollView>

2015年4月15日 星期三

Android Wear 取消往左滑關閉功能

If the user interaction model of your app interferes with the swipe-to-dismiss gesture, you can disable it for your app. To disable the swipe-to-dismiss gesture in your app, extend the default theme and set theandroid:windowSwipeToDismiss attribute to false:
<style name="AppTheme" parent="Theme.DeviceDefault">
    <item name="android:windowSwipeToDismiss">false</item>
</style>