ActionBar的一些理解
1.基本理解
ActionBar是android中的一个控件,从名字可以知道,是一种tab分页式的控件。
当你想要分页的显示的时候,它就派上用场了。
2.另一种理解
我假设我自己要设计一个这样的控件,我会有些什么需求呢?
【UI】
我希望每个tab页都有自己的“名字”。
我希望每个tab页都有自己的“界面”。
【响应】
我希望每个tab点击的时候,tab页可以发生一些变化,这种变化是我可以自己控制的。
3.标准“解释”
【UI】
创建一个ActionBar
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); final ActionBar bar = getActionBar(); bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE); ...... }
增加一个tab页:
用函数addTab来增加一个tab页;参数Tab类,可以用下面的方法设置该tab页的一些属性,比如名称等。
bar.addTab(bar.newTab() .setText("QuickSend") .setTabListener(new TabListener<FragmentQuickSend>( this, "QuickSend", FragmentQuickSend.class)));
【响应】
tab页切换时做的动作:
如果希望在切换到新页有所响应的话,需要向tab页注册响应类TabListener。该类中需要实现一些函数,这些函数就会在特定时刻被调用。
public static class TabListener implements ActionBar.TabListener { public TabListener() { }public void onTabSelected(Tab tab, FragmentTransaction ft) { } public void onTabUnselected(Tab tab, FragmentTransaction ft) { } public void onTabReselected(Tab tab, FragmentTransaction ft) { } }
4.一个例子
MainActivity.java
1 package com.example.tabtest; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.LayoutInflater; 6 import android.view.Menu; 7 import android.view.MenuItem; 8 import android.view.View; 9 import android.view.ViewGroup; 10 import android.view.View.OnClickListener; 11 import android.widget.Button; 12 import android.widget.TextView; 13 import android.widget.Toast; 14 import android.app.ActionBar; 15 import android.app.Fragment; 16 import android.app.FragmentManager; 17 import android.app.FragmentTransaction; 18 import android.app.ActionBar.Tab; 19 20 21 public class MainActivity extends Activity { 22 public String m_strFilePath; 23 FragmentQuickSend m_fragQSend; 24 25 @Override 26 protected void onCreate(Bundle savedInstanceState) { 27 super.onCreate(savedInstanceState); 28 29 final ActionBar bar = getActionBar(); 30 bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 31 bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE); 32 33 bar.addTab(bar.newTab() 34 .setText("Photo") 35 .setTabListener(new TabListener<FragmentPhoto>( 36 this, "Photo", FragmentPhoto.class))); 37 38 } 39 40 @Override 41 protected void onSaveInstanceState(Bundle outState) { 42 super.onSaveInstanceState(outState); 43 outState.putInt("tab", getActionBar().getSelectedNavigationIndex()); 44 } 45 46 public static class TabListener<T extends Fragment> implements ActionBar.TabListener { 47 private final Activity mActivity; 48 private final String mTag; 49 private final Class<T> mClass; 50 private final Bundle mArgs; 51 private Fragment mFragment; 52 53 public TabListener(Activity activity, String tag, Class<T> clz) { 54 this(activity, tag, clz, null); 55 } 56 57 public TabListener(Activity activity, String tag, Class<T> clz, Bundle args) { 58 mActivity = activity; 59 mTag = tag; 60 mClass = clz; 61 mArgs = args; 62 63 // Check to see if we already have a fragment for this tab, probably 64 // from a previously saved state. If so, deactivate it, because our 65 // initial state is that a tab isn't shown. 66 mFragment = mActivity.getFragmentManager().findFragmentByTag(mTag); 67 if (mFragment != null && !mFragment.isDetached()) { 68 FragmentTransaction ft = mActivity.getFragmentManager().beginTransaction(); 69 ft.detach(mFragment); 70 ft.commit(); 71 } 72 } 73 74 public void onTabSelected(Tab tab, FragmentTransaction ft) { 75 if (mFragment == null) { 76 mFragment = Fragment.instantiate(mActivity, mClass.getName(), mArgs); 77 ft.add(android.R.id.content, mFragment, mTag); 78 } else { 79 ft.attach(mFragment); 80 } 81 } 82 83 public void onTabUnselected(Tab tab, FragmentTransaction ft) { 84 if (mFragment != null) { 85 ft.detach(mFragment); 86 } 87 } 88 89 public void onTabReselected(Tab tab, FragmentTransaction ft) { 90 Toast.makeText(mActivity, "Reselected!", Toast.LENGTH_SHORT).show(); 91 } 92 } 93 }
FragmentPhoto.java
1 1 /* 2 2 * Copyright (C) 2010 The Android Open Source Project 3 3 * 4 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 5 * you may not use this file except in compliance with the License. 6 6 * You may obtain a copy of the License at 7 7 * 8 8 * http://www.apache.org/licenses/LICENSE-2.0 9 9 * 10 10 * Unless required by applicable law or agreed to in writing, software 11 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 13 * See the License for the specific language governing permissions and 14 14 * limitations under the License. 15 15 */ 16 16 17 17 package com.example.tabtest; 18 18 19 19 import java.io.File; 20 20 import java.io.FileOutputStream; 21 21 import java.io.IOException; 22 22 import java.util.Date; 23 23 24 24 import com.example.tabtest.R; 25 25 26 26 import android.app.Activity; 27 27 import android.app.Fragment; 28 28 import android.app.FragmentTransaction; 29 29 import android.graphics.PixelFormat; 30 30 import android.hardware.Camera; 31 31 import android.os.AsyncTask; 32 32 import android.os.Bundle; 33 33 import android.os.Environment; 34 34 import android.text.format.DateFormat; 35 35 import android.util.Log; 36 36 import android.view.KeyEvent; 37 37 import android.view.LayoutInflater; 38 38 import android.view.SurfaceHolder; 39 39 import android.view.SurfaceView; 40 40 import android.view.View; 41 41 import android.view.ViewGroup; 42 42 import android.view.View.OnClickListener; 43 43 import android.widget.Button; 44 44 import android.widget.TextView; 45 45 46 46 public class FragmentPhoto extends Fragment { 47 47 48 48 private final static String TAG = "CameraActivity"; 49 49 private SurfaceView surfaceView; 50 50 private SurfaceHolder surfaceHolder; 51 51 private Camera camera; 52 52 private File picture; 53 53 private Button btnSave; 54 54 55 55 /** 56 56 * When creating, retrieve this instance's number from its arguments. 57 57 */ 58 58 @Override 59 59 public void onCreate(Bundle savedInstanceState) { 60 60 super.onCreate(savedInstanceState); 61 61 62 62 } 63 63 64 64 public void onActivityCreated(Bundle savedInstanceState) { 65 65 super.onActivityCreated(savedInstanceState); 66 66 67 67 setupViews(); 68 68 } 69 69 70 70 /** 71 71 * The Fragment's UI is just a simple text view showing its 72 72 * instance number. 73 73 */ 74 74 @Override 75 75 public View onCreateView(LayoutInflater inflater, ViewGroup container, 76 76 Bundle savedInstanceState) { 77 77 return inflater.inflate(R.layout.layout_photo, container, false); 78 78 } 79 79 80 80 private void setupViews(){ 81 81 surfaceView = (SurfaceView) getView().findViewById(R.id.surCameraView); // Camera interface to instantiate components 82 82 surfaceHolder = surfaceView.getHolder(); // Camera interface to instantiate components 83 83 surfaceHolder.addCallback(surfaceCallback); // Add a callback for the SurfaceHolder 84 84 surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 85 85 86 86 btnSave = (Button) getView().findViewById(R.id.btnPhoto); 87 87 88 88 btnSave.setOnClickListener(new OnClickListener() { 89 89 90 90 @Override 91 91 public void onClick(View v) { 92 92 //takePic(); 93 93 } 94 94 }); 95 95 } 96 96 97 97 private void takePic() { 98 98 99 99 camera.stopPreview();// stop the preview 100 100 101 101 camera.takePicture(null, null, pictureCallback); // picture 102 102 } 103 103 104 104 // Photo call back 105 105 Camera.PictureCallback pictureCallback = new Camera.PictureCallback() { 106 106 //@Override 107 107 public void onPictureTaken(byte[] data, Camera camera) { 108 108 new SavePictureTask().execute(data); 109 109 camera.startPreview(); 110 110 } 111 111 }; 112 112 113 113 // save pic 114 114 class SavePictureTask extends AsyncTask<byte[], String, String> { 115 115 @Override 116 116 protected String doInBackground(byte[]... params) { 117 117 String fname = DateFormat.format("yyyyMMddhhmmss", new Date()).toString()+".jpg"; 118 118 119 119 Log.i(TAG, "fname="+fname+";dir="+Environment.getExternalStorageDirectory()); 120 120 //picture = new File(Environment.getExternalStorageDirectory(),fname);// create file 121 121 122 122 picture = new File(Environment.getExternalStorageDirectory()+"/"+fname); 123 123 124 124 try { 125 125 FileOutputStream fos = new FileOutputStream(picture.getPath()); // Get file output stream 126 126 fos.write(params[0]); // Written to the file 127 127 fos.close(); 128 128 } catch (Exception e) { 129 129 e.printStackTrace(); 130 130 } 131 131 return null; 132 132 } 133 133 } 134 134 135 135 // SurfaceHodler Callback handle to open the camera, off camera and photo size changes 136 136 SurfaceHolder.Callback surfaceCallback = new SurfaceHolder.Callback() { 137 137 138 138 public void surfaceCreated(SurfaceHolder holder) { 139 139 Log.i(TAG, "surfaceCallback===="); 140 140 camera = Camera.open(); // Turn on the camera 141 141 try { 142 142 camera.setPreviewDisplay(holder); // Set Preview 143 143 } catch (IOException e) { 144 144 camera.release();// release camera 145 145 camera = null; 146 146 } 147 147 } 148 148 149 149 public void surfaceChanged(SurfaceHolder holder, int format, int width, 150 150 int height) { 151 151 Log.i(TAG,"====surfaceChanged"); 152 152 Camera.Parameters parameters = camera.getParameters(); // Camera parameters to obtain 153 153 parameters.setPictureFormat(PixelFormat.JPEG);// Setting Picture Format 154 154 // parameters.set("rotation", 180); // Arbitrary rotation 155 155 camera.setDisplayOrientation(0); 156 156 // parameters.setPreviewSize(400, 300); // Set Photo Size 157 157 camera.setParameters(parameters); // Setting camera parameters 158 158 camera.startPreview(); // Start Preview 159 159 } 160 160 161 161 public void surfaceDestroyed(SurfaceHolder holder) { 162 162 Log.i(TAG,"====surfaceDestroyed"); 163 163 camera.stopPreview();// stop preview 164 164 camera.release(); // Release camera resources 165 165 camera = null; 166 166 } 167 167 }; 168 168 169 169 }
layout_photo.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <SurfaceView 8 android:id="@+id/surCameraView" 9 android:layout_width="match_parent" 10 android:layout_height="139dp" 11 android:layout_weight="0.00" /> 12 13 <Button 14 android:id="@+id/btnPhoto" 15 android:layout_width="match_parent" 16 android:layout_height="wrap_content" 17 android:layout_weight="0.08" 18 android:text="Button" /> 19 20 <TextView 21 android:id="@+id/textView1" 22 android:layout_width="match_parent" 23 android:layout_height="wrap_content" 24 android:layout_weight="0.19" 25 android:text=" " /> 26 27 </LinearLayout>