AnyCat 是android一個(gè)小的項(xiàng)目,雖然麻雀很小但是知識(shí)很多。別看小程序就沒學(xué)習(xí)的地方,錯(cuò)也。現(xiàn)將學(xué)習(xí)總結(jié)如下:以下為AnyCat項(xiàng)目精髓之處。
轉(zhuǎn)載請注明:QQ:273733055 ppwuyi@sohu.com
程序核心代碼如下:
http://hi.baidu.com/adnroidorg/blog/item/b79194a4c424809bd14358ba.html
/****
* result.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
* result.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); //-----------廣播接收者
* result.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent); //---------------加載進(jìn)去
* result.putExtra(Intent.EXTRA_SHORTCUT_NAME, info.loadLabel(mPackageManager));//------------獲取名字 地址路徑
* result.putExtra(Intent.EXTRA_SHORTCUT_ICON, bd.getBitmap());//----------------加載進(jìn)去
*/
sendBroadcast(result); //注意這才是創(chuàng)建快接方式的核心地點(diǎn)
將裝配好的Intent通過廣播的形式發(fā)送到com.android.launcher.action.INSTALL_SHORTCUT;
不過中小功能的地方我們也學(xué)習(xí)到不少,現(xiàn)總結(jié)如下:
1. FrontDoorActivity.java 無學(xué)習(xí)要點(diǎn)。
2 .ActivityPickerActivity.java 學(xué)習(xí)到怎么獲取系統(tǒng)應(yīng)用程序的程序名和路徑。
view.setTag(view.findViewById(android.R.id.text1));
final TextView textView = (TextView) view.getTag();
Intent queryIntent = new Intent(Intent.ACTION_MAIN); //過濾地址
List<ResolveInfo> list = mPackageManager.queryIntentActivities(queryIntent, 0); //返回符合 地址要求的列表
textView.setText(info.getInfo().loadLabel(mPackageManager)); //取出項(xiàng)目名
mPackageManager = getPackageManager();//獲取包
//獲取應(yīng)用程序的圖標(biāo)
Drawable drawable = info.loadIcon(mPackageManager); //---------------------------讀取程序圖標(biāo)
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bd = (BitmapDrawable) drawable;
result.putExtra(Intent.EXTRA_SHORTCUT_ICON, bd.getBitmap());//----------------加載進(jìn)去
}
//獲取鏈接應(yīng)用程序的橋梁
Intent intent = new Intent();//準(zhǔn)備
intent.setComponent(new ComponentName(info.activityInfo.applicationInfo.packageName,
info.activityInfo.name)); //這是核心
轉(zhuǎn)載請注明:QQ:273733055 ppwuyi@sohu.com
3.CreateShortcutActivity.java 學(xué)習(xí)到很多東西
//獲取單一的聯(lián)系人
Intent intent = new Intent(Intent.ACTION_PICK, Phones.CONTENT_URI); //顯示聯(lián)系人列表并重中選擇一個(gè)聯(lián)系人的URI鏈接方式
intent.putExtra(Contacts.Intents.UI.TITLE_EXTRA_KEY, getText(R.string.callShortcutActivityTitle)); //傳遞一個(gè)標(biāo)題給聯(lián)系人列表
startActivityForResult(intent, REQUEST_PHONE);//開啟聯(lián)系人選擇的activity等待用戶選擇
//別樣的構(gòu)造函數(shù)
return new ShortcutEditorDialog(this, this, this); //注意這個(gè)this,this,this 應(yīng)為實(shí)現(xiàn)了onClick,onCancel接口
public ShortcutEditorDialog(Context context, OnClickListener onClick, OnCancelListener onCancel){.....}
//很和諧的代碼
int iconSize = (int) r.getDimension(android.R.dimen.app_icon_size);//獲得當(dāng)前系統(tǒng)快捷方式的圖片的限制大小
Bitmap icon = Bitmap.createBitmap(iconSize, iconSize, Bitmap.Config.ARGB_8888); //建立一個(gè)空的BItMap
Canvas canvas = new Canvas(icon);//初始化畫布 繪制的圖像到icon上
// Copy in the photo
Paint photoPaint = new Paint(); //建立畫筆
photoPaint.setDither(true); //獲取跟清晰的圖像采樣
photoPaint.setFilterBitmap(true);//過濾一些
Rect src = new Rect(0, 0, photo.getWidth(), photo.getHeight());//創(chuàng)建一個(gè)指定的新矩形的坐標(biāo)
Rect dst = new Rect(0, 0, iconSize, iconSize);//創(chuàng)建一個(gè)指定的新矩形的坐標(biāo)
canvas.drawBitmap(photo, src, dst, photoPaint);//將photo 縮放或則擴(kuò)大到 dst使用的填充區(qū)photoPaint
//再次被和諧的代碼
Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);//設(shè)置畫筆
textPaint.setTextSize(20.0f);//字體大小
textPaint.setTypeface(Typeface.DEFAULT_BOLD);//采用默認(rèn)的寬度
textPaint.setColor(r.getColor(R.color.textColorIconOverlay));//采用的顏色
textPaint.setShadowLayer(3f, 1, 1, r.getColor(R.color.textColorIconOverlayShadow));//影音的設(shè)置
canvas.drawText(overlay, 2, 16, textPaint);//繪制上去 字,開始未知x,y采用那只筆繪制
4.ShortcutEditorDialog.java
唯一學(xué)習(xí)到了一點(diǎn)是
mNameView.setError(getContext().getText(R.string.errorEmptyName));//顯示輸入框?yàn)榭盏母婢?br /> 想看效果自己動(dòng)手吧。