• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

development


Commit MetaInfo

Revisão821736e1fb70be2c827c9e6a35a7739762523897 (tree)
Hora2011-02-08 04:56:55
AutorAlexander Lucas <alexlucas@goog...>
CommiterAlexander Lucas

Mensagem de Log

Added a dialog box, better drag/drop, transparent action bar. fixed crash on camera rotation, updated for new Honeycomb targets.

Change-Id: I40c7a2872d94a13a12fe89d5533b926e201f9eb5

Mudança Sumário

Diff

--- a/samples/Honeycomb-Gallery/res/layout-port/main.xml
+++ b/samples/Honeycomb-Gallery/res/layout-port/main.xml
@@ -23,6 +23,7 @@
2323 <fragment class="com.example.android.hcgallery.TitlesFragment"
2424 android:id="@+id/frag_title"
2525 android:visibility="gone"
26+ android:layout_marginTop="?android:attr/actionBarSize"
2627 android:layout_width="match_parent"
2728 android:layout_height="@dimen/titles_size"/>
2829
--- a/samples/Honeycomb-Gallery/res/layout/main.xml
+++ b/samples/Honeycomb-Gallery/res/layout/main.xml
@@ -23,6 +23,7 @@
2323 <fragment class="com.example.android.hcgallery.TitlesFragment"
2424 android:id="@+id/frag_title"
2525 android:visibility="gone"
26+ android:layout_marginTop="?android:attr/actionBarSize"
2627 android:layout_width="@dimen/titles_size"
2728 android:layout_height="match_parent" />
2829
@@ -30,5 +31,5 @@
3031 android:id="@+id/frag_content"
3132 android:layout_width="match_parent"
3233 android:layout_height="match_parent" />
33-
34+
3435 </LinearLayout>
--- a/samples/Honeycomb-Gallery/res/menu/main_menu.xml
+++ b/samples/Honeycomb-Gallery/res/menu/main_menu.xml
@@ -27,4 +27,7 @@
2727 <item android:id="@+id/toggleTheme"
2828 android:title="Day/Night"
2929 android:showAsAction="ifRoom|withText" />
30+ <item android:id="@+id/showDialog"
31+ android:title="Show a dialog"
32+ android:showAsAction="ifRoom|withText" />
3033 </menu>
--- /dev/null
+++ b/samples/Honeycomb-Gallery/res/values/colors.xml
@@ -0,0 +1,20 @@
1+<?xml version="1.0" encoding="utf-8"?>
2+<!--
3+ Copyright (C) 2011 The Android Open Source Project
4+
5+ Licensed under the Apache License, Version 2.0 (the "License");
6+ you may not use this file except in compliance with the License.
7+ You may obtain a copy of the License at
8+
9+ http://www.apache.org/licenses/LICENSE-2.0
10+
11+ Unless required by applicable law or agreed to in writing, software
12+ distributed under the License is distributed on an "AS IS" BASIS,
13+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+ See the License for the specific language governing permissions and
15+ limitations under the License.
16+ -->
17+<resources>
18+ <color name="actionbar_background_light">#ccffffff</color>
19+ <color name="actionbar_background_dark">#cc000000</color>
20+</resources>
--- a/samples/Honeycomb-Gallery/src/com/example/android/hcgallery/CameraFragment.java
+++ b/samples/Honeycomb-Gallery/src/com/example/android/hcgallery/CameraFragment.java
@@ -43,20 +43,15 @@ public class CameraFragment extends Fragment {
4343 private Preview mPreview;
4444 Camera mCamera;
4545 int mNumberOfCameras;
46- int cameraCurrentlyLocked;
46+ int mCameraCurrentlyLocked;
4747
4848 // The first rear facing camera
49- int defaultCameraId;
49+ int mDefaultCameraId;
5050
5151 @Override
5252 public void onCreate(Bundle savedInstanceState) {
5353 super.onCreate(savedInstanceState);
5454
55- // Add an up arrow to the "home" button, indicating that the button will go "up"
56- // one activity in the app's Activity heirarchy.
57- Activity activity = this.getActivity();
58- ActionBar actionBar = activity.getActionBar();
59- actionBar.setDisplayHomeAsUpEnabled(true);
6055
6156 // Create a RelativeLayout container that will hold a SurfaceView,
6257 // and set it as the content of our activity.
@@ -70,13 +65,27 @@ public class CameraFragment extends Fragment {
7065 for (int i = 0; i < mNumberOfCameras; i++) {
7166 Camera.getCameraInfo(i, cameraInfo);
7267 if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) {
73- defaultCameraId = i;
68+ mDefaultCameraId = i;
7469 }
7570 }
7671 setHasOptionsMenu(mNumberOfCameras > 1);
7772 }
7873
7974 @Override
75+ public void onActivityCreated(Bundle savedInstanceState) {
76+ super.onActivityCreated(savedInstanceState);
77+ // Add an up arrow to the "home" button, indicating that the button will go "up"
78+ // one activity in the app's Activity heirarchy.
79+ // Calls to getActionBar() aren't guaranteed to return the ActionBar when called
80+ // from within the Fragment's onCreate method, because the Window's decor hasn't been
81+ // initialized yet. Either call for the ActionBar reference in Activity.onCreate()
82+ // (after the setContentView(...) call), or in the Fragment's onActivityCreated method.
83+ Activity activity = this.getActivity();
84+ ActionBar actionBar = activity.getActionBar();
85+ actionBar.setDisplayHomeAsUpEnabled(true);
86+ }
87+
88+ @Override
8089 public View onCreateView(LayoutInflater inflater, ViewGroup container,
8190 Bundle savedInstanceState) {
8291 return mPreview;
@@ -87,8 +96,8 @@ public class CameraFragment extends Fragment {
8796 super.onResume();
8897
8998 // Open the default i.e. the first rear facing camera.
90- mCamera = Camera.open(defaultCameraId);
91- cameraCurrentlyLocked = defaultCameraId;
99+ mCamera = Camera.open(mDefaultCameraId);
100+ mCameraCurrentlyLocked = mDefaultCameraId;
92101 mPreview.setCamera(mCamera);
93102 }
94103
@@ -120,7 +129,7 @@ public class CameraFragment extends Fragment {
120129 // Handle item selection
121130 switch (item.getItemId()) {
122131 case R.id.switch_cam:
123- // Release this camera -> cameraCurrentlyLocked
132+ // Release this camera -> mCameraCurrentlyLocked
124133 if (mCamera != null) {
125134 mCamera.stopPreview();
126135 mPreview.setCamera(null);
@@ -131,8 +140,8 @@ public class CameraFragment extends Fragment {
131140 // Acquire the next camera and request Preview to reconfigure
132141 // parameters.
133142 mCamera = Camera
134- .open((cameraCurrentlyLocked + 1) % mNumberOfCameras);
135- cameraCurrentlyLocked = (cameraCurrentlyLocked + 1)
143+ .open((mCameraCurrentlyLocked + 1) % mNumberOfCameras);
144+ mCameraCurrentlyLocked = (mCameraCurrentlyLocked + 1)
136145 % mNumberOfCameras;
137146 mPreview.switchCamera(mCamera);
138147
--- a/samples/Honeycomb-Gallery/src/com/example/android/hcgallery/ContentFragment.java
+++ b/samples/Honeycomb-Gallery/src/com/example/android/hcgallery/ContentFragment.java
@@ -23,6 +23,8 @@ import android.app.Fragment;
2323 import android.content.ClipData;
2424 import android.content.ClipDescription;
2525 import android.content.ClipData.Item;
26+import android.graphics.Bitmap;
27+import android.graphics.Color;
2628 import android.os.Bundle;
2729 import android.view.DragEvent;
2830 import android.view.LayoutInflater;
@@ -30,8 +32,12 @@ import android.view.View;
3032 import android.view.ViewGroup;
3133 import android.view.View.OnClickListener;
3234 import android.widget.ImageView;
35+import android.view.Window;
36+import android.view.WindowManager;
3337
3438 public class ContentFragment extends Fragment {
39+ // The bitmap currently used by ImageView
40+ private Bitmap mBitmap = null;
3541
3642 @Override
3743 public void onActivityCreated(Bundle savedInstanceState) {
@@ -48,9 +54,16 @@ public class ContentFragment extends Fragment {
4854 view.setOnDragListener(new View.OnDragListener() {
4955 public boolean onDrag(View v, DragEvent event) {
5056 switch (event.getAction()) {
57+ case DragEvent.ACTION_DRAG_ENTERED:
58+ view.setBackgroundColor(Color.LTGRAY);
59+ break;
60+ case DragEvent.ACTION_DRAG_EXITED:
61+ view.setBackgroundColor(Color.TRANSPARENT);
62+ break;
5163 case DragEvent.ACTION_DRAG_STARTED:
5264 return processDragStarted(event);
5365 case DragEvent.ACTION_DROP:
66+ view.setBackgroundColor(Color.TRANSPARENT);
5467 return processDrop(event, imageView);
5568 }
5669 return false;
@@ -59,10 +72,9 @@ public class ContentFragment extends Fragment {
5972
6073 view.setOnClickListener(new OnClickListener() {
6174
62- @Override
75+
6376 public void onClick(View v) {
64- ActionBar bar = ContentFragment.this.getActivity()
65- .getActionBar();
77+ ActionBar bar = getActivity().getActionBar();
6678 if (bar != null) {
6779 if (bar.isShowing()) {
6880 bar.hide();
@@ -85,7 +97,7 @@ public class ContentFragment extends Fragment {
8597 return false;
8698 }
8799
88- boolean processDrop(DragEvent event, ImageView imageView) {
100+ private boolean processDrop(DragEvent event, ImageView imageView) {
89101 // Attempt to parse clip data with expected format: category||entry_id.
90102 // Ignore event if data does not conform to this format.
91103 ClipData data = event.getClipData();
@@ -106,14 +118,28 @@ public class ContentFragment extends Fragment {
106118 } catch (NumberFormatException exception) {
107119 return false;
108120 }
109- imageView.setImageBitmap(
110- Directory.getCategory(category)
111- .getEntry(entryId)
112- .getBitmap(getResources()));
121+ updateContentAndRecycleBitmap(category, entryId);
122+ // Update list fragment with selected entry.
123+ TitlesFragment titlesFrag = (TitlesFragment)
124+ getFragmentManager().findFragmentById(R.id.frag_title);
125+ titlesFrag.selectPosition(entryId);
113126 return true;
114127 }
115128 }
116129 }
117130 return false;
118131 }
132+
133+ public void updateContentAndRecycleBitmap(int category, int position) {
134+ // Get the bitmap that needs to be drawn and update the ImageView
135+ Bitmap next = Directory.getCategory(category).getEntry(position)
136+ .getBitmap(getResources());
137+ ((ImageView) getView().findViewById(R.id.image)).setImageBitmap(next);
138+ if (mBitmap != null) {
139+ // This is an advanced call and should be used if you
140+ // are working with a lot of bitmaps. The bitmap is dead
141+ // after this call.
142+ mBitmap.recycle();
143+ }
144+ }
119145 }
--- a/samples/Honeycomb-Gallery/src/com/example/android/hcgallery/Directory.java
+++ b/samples/Honeycomb-Gallery/src/com/example/android/hcgallery/Directory.java
@@ -17,13 +17,13 @@
1717 package com.example.android.hcgallery;
1818
1919 public class Directory {
20- private static DirectoryCategory[] categories;
20+ private static DirectoryCategory[] mCategories;
2121
2222 public static void initializeDirectory() {
23- categories = new DirectoryCategory[] {
23+ mCategories = new DirectoryCategory[] {
2424 new DirectoryCategory("Balloons", new DirectoryEntry[] {
25- new DirectoryEntry("Green Balloon", R.drawable.green_balloon),
2625 new DirectoryEntry("Red Balloon", R.drawable.red_balloon),
26+ new DirectoryEntry("Green Balloon", R.drawable.green_balloon),
2727 new DirectoryEntry("Blue Balloon", R.drawable.blue_balloon)}),
2828 new DirectoryCategory("Bikes", new DirectoryEntry[] {
2929 new DirectoryEntry("Old school huffy", R.drawable.blue_bike),
@@ -42,10 +42,10 @@ public class Directory {
4242 }
4343
4444 public static int getCategoryCount() {
45- return categories.length;
45+ return mCategories.length;
4646 }
4747
4848 public static DirectoryCategory getCategory(int i) {
49- return categories[i];
49+ return mCategories[i];
5050 }
5151 }
--- a/samples/Honeycomb-Gallery/src/com/example/android/hcgallery/MainActivity.java
+++ b/samples/Honeycomb-Gallery/src/com/example/android/hcgallery/MainActivity.java
@@ -23,16 +23,22 @@ import android.animation.PropertyValuesHolder;
2323 import android.animation.ValueAnimator;
2424 import android.app.ActionBar;
2525 import android.app.Activity;
26+import android.app.AlertDialog;
27+import android.app.Dialog;
28+import android.app.DialogFragment;
2629 import android.app.FragmentManager;
2730 import android.app.FragmentTransaction;
31+import android.content.DialogInterface;
2832 import android.content.Intent;
2933 import android.content.res.Configuration;
34+import android.graphics.drawable.ColorDrawable;
3035 import android.os.Bundle;
3136 import android.view.Menu;
3237 import android.view.MenuInflater;
3338 import android.view.MenuItem;
3439 import android.view.View;
3540 import android.view.ViewGroup;
41+import android.view.Window;
3642
3743 public class MainActivity extends Activity implements ActionBar.TabListener {
3844
@@ -49,16 +55,27 @@ public class MainActivity extends Activity implements ActionBar.TabListener {
4955 mThemeId = savedInstanceState.getInt("theme");
5056 this.setTheme(mThemeId);
5157 }
58+
59+ requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
5260 setContentView(R.layout.main);
5361
5462 Directory.initializeDirectory();
5563
5664 ActionBar bar = getActionBar();
5765
66+ if (mThemeId == android.R.style.Theme_Holo_Light || mThemeId == -1) {
67+ bar.setBackgroundDrawable(
68+ new ColorDrawable(getResources().getColor(R.color.actionbar_background_light)));
69+ } else {
70+ bar.setBackgroundDrawable(
71+ new ColorDrawable(getResources().getColor(R.color.actionbar_background_dark)));
72+ }
73+
5874 int i;
59- for (i = 0; i < Directory.getCategoryCount(); i++)
75+ for (i = 0; i < Directory.getCategoryCount(); i++) {
6076 bar.addTab(bar.newTab().setText(Directory.getCategory(i).getName())
6177 .setTabListener(this));
78+ }
6279
6380 mActionBarView = getLayoutInflater().inflate(
6481 R.layout.action_bar_custom, null);
@@ -108,6 +125,7 @@ public class MainActivity extends Activity implements ActionBar.TabListener {
108125 case R.id.toggleTitles:
109126 toggleVisibleTitles();
110127 return true;
128+
111129 case R.id.toggleTheme:
112130 if (mThemeId == android.R.style.Theme_Holo) {
113131 mThemeId = android.R.style.Theme_Holo_Light;
@@ -115,6 +133,12 @@ public class MainActivity extends Activity implements ActionBar.TabListener {
115133 mThemeId = android.R.style.Theme_Holo;
116134 }
117135 this.recreate();
136+ return true;
137+
138+ case R.id.showDialog:
139+ showDialog();
140+ return true;
141+
118142 default:
119143 return super.onOptionsItemSelected(item);
120144 }
@@ -203,6 +227,19 @@ public class MainActivity extends Activity implements ActionBar.TabListener {
203227 invalidateOptionsMenu();
204228 }
205229
230+ void showDialog() {
231+
232+ // DialogFragment.show() will take care of adding the fragment
233+ // in a transaction. We also want to remove any currently showing
234+ // dialog, so make our own transaction and take care of that here.
235+ FragmentTransaction ft = getFragmentManager().beginTransaction();
236+
237+ DialogFragment newFragment = MyDialogFragment.newInstance("The Dialog Of Awesome");
238+
239+ // Create and show the dialog.
240+ newFragment.show(ft, "dialog");
241+ }
242+
206243 @Override
207244 public boolean onPrepareOptionsMenu(Menu menu) {
208245 menu.getItem(1).setTitle(mToggleLabels[mLabelIndex]);
@@ -217,4 +254,37 @@ public class MainActivity extends Activity implements ActionBar.TabListener {
217254 outState.putInt("category", category);
218255 outState.putInt("theme", mThemeId);
219256 }
257+
258+
259+ public static class MyDialogFragment extends DialogFragment {
260+
261+ public static MyDialogFragment newInstance(String title) {
262+ MyDialogFragment frag = new MyDialogFragment();
263+ Bundle args = new Bundle();
264+ args.putString("title", title);
265+ frag.setArguments(args);
266+ return frag;
267+ }
268+
269+ @Override
270+ public Dialog onCreateDialog(Bundle savedInstanceState) {
271+ String title = getArguments().getString("title");
272+
273+ return new AlertDialog.Builder(getActivity())
274+ .setTitle(title)
275+ .setPositiveButton("OK",
276+ new DialogInterface.OnClickListener() {
277+ public void onClick(DialogInterface dialog, int whichButton) {
278+ }
279+ }
280+ )
281+ .setNegativeButton("Cancel",
282+ new DialogInterface.OnClickListener() {
283+ public void onClick(DialogInterface dialog, int whichButton) {
284+ }
285+ }
286+ )
287+ .create();
288+ }
289+ }
220290 }
--- a/samples/Honeycomb-Gallery/src/com/example/android/hcgallery/TitlesFragment.java
+++ b/samples/Honeycomb-Gallery/src/com/example/android/hcgallery/TitlesFragment.java
@@ -16,14 +16,18 @@
1616
1717 package com.example.android.hcgallery;
1818
19+import android.app.Fragment;
20+import android.app.FragmentManager;
1921 import android.app.ListFragment;
2022 import android.content.ClipData;
21-import android.graphics.BitmapFactory;
2223 import android.graphics.Canvas;
2324 import android.graphics.Color;
2425 import android.graphics.drawable.ColorDrawable;
2526 import android.graphics.drawable.Drawable;
27+import android.graphics.Paint;
28+import android.graphics.Typeface;
2629 import android.os.Bundle;
30+import android.text.TextPaint;
2731 import android.view.View;
2832 import android.widget.AdapterView;
2933 import android.widget.ArrayAdapter;
@@ -51,10 +55,8 @@ public class TitlesFragment extends ListFragment {
5155 lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
5256 selectPosition(mCurPosition);
5357 lv.setCacheColorHint(Color.WHITE);
54-
5558 lv.setOnItemLongClickListener(new OnItemLongClickListener() {
5659
57- @Override
5860 public boolean onItemLongClick(AdapterView<?> av, View v, int pos,
5961 long id) {
6062 final String title = (String) ((TextView) v).getText();
@@ -62,24 +64,36 @@ public class TitlesFragment extends ListFragment {
6264 // Set up clip data with the category||entry_id format.
6365 final String textData = String.format("%d||%d", mCategory, pos);
6466 ClipData data = ClipData.newPlainText(title, textData);
65- v.startDrag(data, new MyDragShadowBuilder(v), null, 0);
67+
68+ v.startDrag(data, new MyDragShadowBuilder(v, title), null, 0);
6669 return true;
6770 }
6871 });
6972 }
7073
7174 private static class MyDragShadowBuilder extends View.DragShadowBuilder {
72- private static Drawable shadow;
73-
74- public MyDragShadowBuilder(View v) {
75+ private static Drawable mShadow;
76+ private static String mLabel;
77+ private static int mViewHeight;
78+
79+ public MyDragShadowBuilder(View v, String label) {
7580 super(v);
76- shadow = new ColorDrawable(Color.BLUE);
77- shadow.setBounds(0, 0, v.getWidth(), v.getHeight());
81+ mShadow = new ColorDrawable(Color.BLUE);
82+ mShadow.setBounds(0, 0, v.getWidth(), v.getHeight());
83+ mLabel = label;
84+ mViewHeight = v.getHeight();
7885 }
7986
8087 @Override
8188 public void onDrawShadow(Canvas canvas) {
82- shadow.draw(canvas);
89+ super.onDrawShadow(canvas);
90+ mShadow.draw(canvas);
91+ Paint paint = new TextPaint();
92+ paint.setTextSize(20);
93+ paint.setColor(Color.LTGRAY);
94+ paint.setTypeface(Typeface.DEFAULT_BOLD);
95+ paint.setAntiAlias(true);
96+ canvas.drawText(mLabel, 20, (float) (mViewHeight * .6), paint);
8397 }
8498 }
8599
@@ -99,11 +113,9 @@ public class TitlesFragment extends ListFragment {
99113 }
100114
101115 private void updateImage(int position) {
102- ImageView iv = (ImageView) getFragmentManager().findFragmentById(
103- R.id.frag_content).getView().findViewById(R.id.image);
104- iv.setImageDrawable(Directory.getCategory(mCategory).getEntry(position)
105- .getDrawable(getResources()));
106- mCurPosition = position;
116+ ContentFragment frag = (ContentFragment) getFragmentManager()
117+ .findFragmentById(R.id.frag_content);
118+ frag.updateContentAndRecycleBitmap(mCategory, position);
107119 }
108120
109121 public void selectPosition(int position) {