android 2.2 apidemos 赏析笔记 2

PACKAGE:com.example.android.apis.app

CustomDialogActivity.java

SEE:

1.custom_dialog_activity.xml

<TextViewxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/text"

android:layout_width="match_parent"android:layout_height="match_parent"

android:gravity="center_vertical|center_horizontal"

android:text="@string/custom_dialog_activity_text"/>

2.AndroidManifest.xml

<activityandroid:name=".app.CustomDialogActivity"

android:label="@string/activity_custom_dialog"

android:theme="@style/Theme.CustomDialog">

<intent-filter>

<actionandroid:name="android.intent.action.MAIN"/>

<categoryandroid:name="android.intent.category.SAMPLE_CODE"/>

</intent-filter>

</activity>

3.values.styles.xml

<stylename="Theme.CustomDialog"parent="android:style/Theme.Dialog">

<itemname="android:windowBackground">@drawable/filled_box</item>

</style>

4.drawable.fill_box.xml

<shapexmlns:android="http://schemas.android.com/apk/res/android">

<solidandroid:color="#f0600000"/>

<strokeandroid:width="3dp"color="#ffff8080"/>

<cornersandroid:radius="3dp"/>

<paddingandroid:left="10dp"android:top="10dp"

android:right="10dp"android:bottom="10dp"/>

</shape>

SO:很是纠结的一个设置呀,解耦解成这样不容易呀。

CustomTitle.java

SEE:

1.onCreate()

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title_1);

2.layout.custom_title_1.xml

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/screen"

android:layout_width="match_parent"android:layout_height="match_parent"

android:orientation="vertical">

<TextViewandroid:id="@+id/left_text"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:text="@string/custom_title_left"/>

<TextViewandroid:id="@+id/right_text"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentRight="true"

android:text="@string/custom_title_right"/>

</RelativeLayout>

SO:

设置标题栏样式的方法。。。

DialogActivity.java

SEE:

1.onCreate()=>

requestWindowFeature(Window.FEATURE_LEFT_ICON);

getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,

android.R.drawable.ic_dialog_alert);

SO:

设置那个感叹号的方法。。。真短。。其他应该同CUSTOM_DIALOG

Forwarding.java

SEE:

1.onClick()=>

Intentintent=newIntent();

intent.setClass(Forwarding.this,ForwardTarget.class);

startActivity(intent);

finish();

SO:

只要finish()掉这个activity,就不会呆在栈上了。。回来也见不到他了。。

HelloWorld.java

无视。。

SEE:

1.res.value.string.xml

<stringname="hello_world"><b>Hello,<i>World!</i></b></string>

SO:

这里可以设置格式。。。

PersistentState.java

SEE:

1.save_restore_state.xml

<EditTextandroid:id="@+id/saved"

android:layout_width="match_parent"android:layout_height="wrap_content"

android:layout_weight="1"

android:background="@drawable/green"

android:text="@string/initial_text"

android:freezesText="true">

<requestFocus/>

</EditText>

2.onResume()

SharedPreferencesprefs=getPreferences(0);

StringrestoredText=prefs.getString("text",null);

if(restoredText!=null){

mSaved.setText(restoredText,TextView.BufferType.EDITABLE);

intselectionStart=prefs.getInt("selection-start",-1);

intselectionEnd=prefs.getInt("selection-end",-1);

if(selectionStart!=-1&&selectionEnd!=-1){

mSaved.setSelection(selectionStart,selectionEnd);

}

}

3.onPause()

SharedPreferences.Editoreditor=getPreferences(0).edit();

editor.putString("text",mSaved.getText().toString());

editor.putInt("selection-start",mSaved.getSelectionStart());

editor.putInt("selection-end",mSaved.getSelectionEnd());

editor.commit();

SO:

见sharePreferences的保存方式~。。。这个还可以保存选择文字的开始点和结束点

=========================END

QuickContactsDemo.JAVA

SEE:

1.QuickContactsDemo.java

staticfinalString[]CONTACTS_SUMMARY_PROJECTION=newString[]{

Contacts._ID,//0

Contacts.DISPLAY_NAME,//1

Contacts.STARRED,//2

Contacts.TIMES_CONTACTED,//3

Contacts.CONTACT_PRESENCE,//4

Contacts.PHOTO_ID,//5

Contacts.LOOKUP_KEY,//6

Contacts.HAS_PHONE_NUMBER,//7

};

2.onCreate()

Stringselect="(("+Contacts.DISPLAY_NAME+"NOTNULL)AND("

+Contacts.HAS_PHONE_NUMBER+"=1)AND("

+Contacts.DISPLAY_NAME+"!=''))";

Cursorc=

getContentResolver().query(Contacts.CONTENT_URI,CONTACTS_SUMMARY_PROJECTION,select,

null,Contacts.DISPLAY_NAME+"COLLATELOCALIZEDASC");

SO:

获得了联系人的表,需要什么参数什么条件再改。。。

SEE:

1.startManagingCursor(c);

SO:

startManagingCursor(Cursorc)

ThismethodallowstheactivitytotakecareofmanagingthegivenCursor'slifecycleforyoubasedontheactivity'slifecycle.

SEE:

1.

finalContactListItemCachecache=(ContactListItemCache)view.getTag();

2.

finalstaticclassContactListItemCache{

publicTextViewnameView;

publicQuickContactBadgephotoView;

publicCharArrayBuffernameBuffer=newCharArrayBuffer(128);

}

3.

publicViewnewView(Contextcontext,Cursorcursor,ViewGroupparent){

Viewview=super.newView(context,cursor,parent);

ContactListItemCachecache=newContactListItemCache();

cache.nameView=(TextView)view.findViewById(R.id.name);

cache.photoView=(QuickContactBadge)view.findViewById(R.id.badge);

view.setTag(cache);

returnview;

}

SO:

getTag()returnstheObjectstoredinthisviewasatag将某个对象作为tag保存在view中。。在newView中进行tag和View的绑定。隐藏view的细节。。。设置的时候不用一直findViewById了

SEE:

1.bindView()

SO:

abstractvoidbindView()Bindanexistingviewtothedatapointedtobycursor

SEE:

1.bindView()

finalContactListItemCachecache=(ContactListItemCache)view.getTag();

TextViewnameView=cache.nameView;

QuickContactBadgephotoView=cache.photoView;

//Setthename

cursor.copyStringToBuffer(SUMMARY_NAME_COLUMN_INDEX,cache.nameBuffer);

intsize=cache.nameBuffer.sizeCopied;

cache.nameView.setText(cache.nameBuffer.data,0,size);

finallongcontactId=cursor.getLong(SUMMARY_ID_COLUMN_INDEX);

finalStringlookupKey=cursor.getString(SUMMARY_LOOKUP_KEY);

cache.photoView.assignContactUri(Contacts.getLookupUri(contactId,lookupKey));

SO:

cursor的取值操作,绑定View的tag的设置工作。。

SEE:

1.

ContactListItemAdapteradapter=newContactListItemAdapter(this,R.layout.quick_contacts,c);

2.

layout.quick_contacts.xml

SO:看看就好adapter的每一项的布局。。

ReseiveResult.java

SEE:

1.onClick()

Intentintent=newIntent(ReceiveResult.this,SendResult.class);

startActivityForResult(intent,GET_CODE);

2.

protectedvoidonActivityResult(intrequestCode,intresultCode,

Intentdata)

SO:

开始一个Activity来接收result

SEE:SendResult.java

1.

setResult(RESULT_OK,(newIntent()).setAction("Corky!"));

finish();

SO:

返回ok并带回一个结果。。。。居然以ACTION的形式传回来。。而不是intent.putExtra,懒呀懒呀。

RedirectMain.java

SEE:

1.RedirectEnter.java

Intentintent=newIntent(RedirectEnter.this,RedirectMain.class);

startActivity(intent);

2.RedirectMain.java

if(!loadPrefs()){

Intentintent=newIntent(this,RedirectGetter.class);

startActivityForResult(intent,INIT_TEXT_REQUEST);

}

...

if(resultCode==RESULT_CANCELED){

finish();

SO:

进来无数据就跳走,返回为CANCELLED就finish();这个逻辑真纠结。

SEE:

1.RedirectGetter.java

SharedPreferencespreferences=getSharedPreferences("RedirectData",0);

SharedPreferences.Editoreditor=preferences.edit();

editor.putString("text",mText.getText().toString());

if(editor.commit()){

setResult(RESULT_OK);

}

SO:

在复习一次sharedPreferences保存方法。

ReorderOnLaunch.java

SEE:

1.ReorderFour.java

Intentintent=newIntent(ReorderFour.this,ReorderTwo.class);

intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

startActivity(intent);

SO:

如果栈中有这个Activity则放到最前端。点了N多下之后,如果返回的话,会发现second只有一个。。。因为总是被带到前端来,而不是重新创建一个实例。。singletask?

SaveRestoreState.java

1.

/**

*Retrievethetextthatiscurrentlyinthe"saved"editor.

*/

CharSequencegetSavedText(){

return((EditText)findViewById(R.id.saved)).getText();

}

/**

*Changethetextthatiscurrentlyinthe"saved"editor.

*/

voidsetSavedText(CharSequencetext){

((EditText)findViewById(R.id.saved)).setText(text);

}

2.

<EditTextandroid:id="@+id/saved"

android:layout_width="match_parent"android:layout_height="wrap_content"

android:layout_weight="1"

android:background="@drawable/green"

android:text="@string/initial_text"

android:freezesText="true">

<requestFocus/>

</EditText>

SO:

横竖屏的切换实际上是销毁一个ACTIVITY并创建另一个ACTIVITY,当然不能保存EditText里的数据,根据一个属性android:freezesText="true"两个方法getSavedText()setSavedText()就能保存里面的数据。

SetWallpaperActivity.java

SEE:

1.layout.wallpaper.xml

<FrameLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="wrap_content"

android:layout_height="wrap_content">

<ImageView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/imageview"/>

<LinearLayout

android:orientation="horizontal"

android:layout_width="wrap_content"

android:layout_height="match_parent">

SO:

这样布局就有背景与按钮两层了。。。FrameLayout原来以为很废物的。。

SEE:

1.onCreate()

finalWallpaperManagerwallpaperManager=WallpaperManager.getInstance(this);

finalDrawablewallpaperDrawable=wallpaperManager.getDrawable();

finalImageViewimageView=(ImageView)findViewById(R.id.imageview);

imageView.setDrawingCacheEnabled(true);

imageView.setImageDrawable(wallpaperDrawable);

SO:

获取WallpaperManager然后又获取Drawable然后就可以获取壁纸了。

SEE:

1.

intmColor=(int)Math.floor(Math.random()*mColors.length);

wallpaperDrawable.setColorFilter(mColors[mColor],PorterDuff.Mode.MULTIPLY);

imageView.setImageDrawable(wallpaperDrawable);

imageView.invalidate();

SO:

设置一个Drawable的ColorFilter就可以有滤镜的效果。。。待研究

SEE:

1.

wallpaperManager.setBitmap(imageView.getDrawingCache());

2.imageView.setDrawingCacheEnabled(true);

SO:获取View的DrawingCache,输出到WallpaperManager成壁纸。应该也可以输出到别的地方吧,把一个view的DrawingCache来做变换效果。根view的难道可以实现截屏?。。MARK

TranslucentActivity.java

SEE:

1.AndroidManifest.xml

<activityandroid:name=".app.TranslucentActivity"

android:label="@string/activity_translucent"

android:theme="@style/Theme.Translucent">

2.values.styles.xml

<stylename="Theme.Translucent"parent="android:style/Theme.Translucent">

<itemname="android:windowBackground">@drawable/translucent_background</item>

<itemname="android:windowNoTitle">true</item>

<itemname="android:colorForeground">#fff</item>

</style>

SO:

将android:windowBackground设置为@drawable/translucent_background应该就可以了吧

TranslucentBlueActivity.java

SEE:

1.onCreate()

getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,

WindowManager.LayoutParams.FLAG_BLUR_BEHIND);

SO:

背景模糊效果~

WallpaperActivity.java

SEE

1.values.style.xml

<stylename="Theme.Wallpaper"parent="android:style/Theme.Wallpaper">

<itemname="android:colorForeground">#fff</item>

</style>

SO:不是Theme.Translucent而已。。。

==========================终于完了

相关推荐