下面列出了android.app.ActivityManager#getLauncherLargeIconDensity ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Generates a generic icon to be used in the launcher. This is just a rounded rectangle with
* a letter in the middle taken from the website's domain name.
*
* @param url URL of the shortcut.
* @param red Red component of the dominant icon color.
* @param green Green component of the dominant icon color.
* @param blue Blue component of the dominant icon color.
* @return Bitmap Either the touch-icon or the newly created favicon.
*/
@CalledByNative
public static Bitmap generateHomeScreenIcon(String url, int red, int green, int blue) {
Context context = ContextUtils.getApplicationContext();
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
final int outerSize = am.getLauncherLargeIconSize();
final int iconDensity = am.getLauncherLargeIconDensity();
Bitmap bitmap = null;
try {
bitmap = Bitmap.createBitmap(outerSize, outerSize, Bitmap.Config.ARGB_8888);
} catch (OutOfMemoryError e) {
Log.w(TAG, "OutOfMemoryError while trying to draw bitmap on canvas.");
return null;
}
Canvas canvas = new Canvas(bitmap);
// Draw the drop shadow.
int padding = (int) (GENERATED_ICON_PADDING_RATIO * outerSize);
Rect outerBounds = new Rect(0, 0, outerSize, outerSize);
Bitmap iconShadow =
getBitmapFromResourceId(context, R.mipmap.shortcut_icon_shadow, iconDensity);
Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
canvas.drawBitmap(iconShadow, null, outerBounds, paint);
// Draw the rounded rectangle and letter.
int innerSize = outerSize - 2 * padding;
int cornerRadius = Math.round(ICON_CORNER_RADIUS_RATIO * outerSize);
int fontSize = Math.round(GENERATED_ICON_FONT_SIZE_RATIO * outerSize);
int color = Color.rgb(red, green, blue);
RoundedIconGenerator generator = new RoundedIconGenerator(
innerSize, innerSize, cornerRadius, color, fontSize);
Bitmap icon = generator.generateIconForUrl(url);
if (icon == null) return null; // Bookmark URL does not have a domain.
canvas.drawBitmap(icon, padding, padding, null);
return bitmap;
}
/**
* Generates a generic icon to be used in the launcher. This is just a rounded rectangle with
* a letter in the middle taken from the website's domain name.
*
* @param url URL of the shortcut.
* @param red Red component of the dominant icon color.
* @param green Green component of the dominant icon color.
* @param blue Blue component of the dominant icon color.
* @return Bitmap Either the touch-icon or the newly created favicon.
*/
@CalledByNative
public static Bitmap generateHomeScreenIcon(String url, int red, int green, int blue) {
Context context = ContextUtils.getApplicationContext();
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
final int outerSize = am.getLauncherLargeIconSize();
final int iconDensity = am.getLauncherLargeIconDensity();
Bitmap bitmap = null;
try {
bitmap = Bitmap.createBitmap(outerSize, outerSize, Bitmap.Config.ARGB_8888);
} catch (OutOfMemoryError e) {
Log.w(TAG, "OutOfMemoryError while trying to draw bitmap on canvas.");
return null;
}
Canvas canvas = new Canvas(bitmap);
// Draw the drop shadow.
int padding = (int) (GENERATED_ICON_PADDING_RATIO * outerSize);
Rect outerBounds = new Rect(0, 0, outerSize, outerSize);
Bitmap iconShadow =
getBitmapFromResourceId(context, R.mipmap.shortcut_icon_shadow, iconDensity);
Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
canvas.drawBitmap(iconShadow, null, outerBounds, paint);
// Draw the rounded rectangle and letter.
int innerSize = outerSize - 2 * padding;
int cornerRadius = Math.round(ICON_CORNER_RADIUS_RATIO * outerSize);
int fontSize = Math.round(GENERATED_ICON_FONT_SIZE_RATIO * outerSize);
int color = Color.rgb(red, green, blue);
RoundedIconGenerator generator = new RoundedIconGenerator(
innerSize, innerSize, cornerRadius, color, fontSize);
Bitmap icon = generator.generateIconForUrl(url);
if (icon == null) return null; // Bookmark URL does not have a domain.
canvas.drawBitmap(icon, padding, padding, null);
return bitmap;
}
public IconCache(Context context) {
ActivityManager activityManager = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
mContext = context;
mPackageManager = context.getPackageManager();
mIconDpi = activityManager.getLauncherLargeIconDensity();
// need to set mIconDpi before getting default icon
mDefaultIcon = makeDefaultIcon();
}
public IconCache(Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
mContext = context;
mPackageManager = context.getPackageManager();
mIconDpi = activityManager.getLauncherLargeIconDensity();
mDefaultIcon = makeDefaultIcon();
mIconPackHelper = new IconPackHelper(context);
loadIconPack();
}
/**
* Generates a generic icon to be used in the launcher. This is just a rounded rectangle with
* a letter in the middle taken from the website's domain name.
*
* @param url URL of the shortcut.
* @param red Red component of the dominant icon color.
* @param green Green component of the dominant icon color.
* @param blue Blue component of the dominant icon color.
* @return Bitmap Either the touch-icon or the newly created favicon.
*/
@CalledByNative
public static Bitmap generateHomeScreenIcon(String url, int red, int green, int blue) {
Context context = ContextUtils.getApplicationContext();
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
final int outerSize = am.getLauncherLargeIconSize();
final int iconDensity = am.getLauncherLargeIconDensity();
Bitmap bitmap = null;
try {
bitmap = Bitmap.createBitmap(outerSize, outerSize, Bitmap.Config.ARGB_8888);
} catch (OutOfMemoryError e) {
Log.w(TAG, "OutOfMemoryError while trying to draw bitmap on canvas.");
return null;
}
Canvas canvas = new Canvas(bitmap);
// Draw the drop shadow.
int padding = (int) (GENERATED_ICON_PADDING_RATIO * outerSize);
Rect outerBounds = new Rect(0, 0, outerSize, outerSize);
Bitmap iconShadow =
getBitmapFromResourceId(context, R.mipmap.shortcut_icon_shadow, iconDensity);
Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
canvas.drawBitmap(iconShadow, null, outerBounds, paint);
// Draw the rounded rectangle and letter.
int innerSize = outerSize - 2 * padding;
int cornerRadius = Math.round(ICON_CORNER_RADIUS_RATIO * outerSize);
int fontSize = Math.round(GENERATED_ICON_FONT_SIZE_RATIO * outerSize);
int color = Color.rgb(red, green, blue);
RoundedIconGenerator generator = new RoundedIconGenerator(
innerSize, innerSize, cornerRadius, color, fontSize);
Bitmap icon = generator.generateIconForUrl(url);
if (icon == null) return null; // Bookmark URL does not have a domain.
canvas.drawBitmap(icon, padding, padding, null);
return bitmap;
}
public IconCache(Context context) {
ActivityManager activityManager =
(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
mContext = context;
mPackageManager = context.getPackageManager();
mUserManager = UserManagerCompat.getInstance(mContext);
mLauncherApps = LauncherAppsCompat.getInstance(mContext);
mIconDpi = activityManager.getLauncherLargeIconDensity();
// need to set mIconDpi before getting default icon
UserHandleCompat myUser = UserHandleCompat.myUserHandle();
mDefaultIcons.put(myUser, makeDefaultIcon(myUser));
}
public IconCache(Context context) {
ActivityManager activityManager =
(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
mContext = context;
mPackageManager = context.getPackageManager();
mIconDpi = activityManager.getLauncherLargeIconDensity();
// need to set mIconDpi before getting default icon
mDefaultIcon = makeDefaultIcon();
}
public IconCacheHelper(Context context) {
ActivityManager activityManager =
(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
mContext = context;
mPackageManager = context.getPackageManager();
mIconDpi = activityManager.getLauncherLargeIconDensity();
// need to set mIconDpi before getting default icon
mIconPackHelper = new IconPackHelper(context);
loadIconPack();
}
/**
* Creates an icon to be associated with this bookmark. If available, the touch icon
* will be used, else we draw our own.
* @param context Context used to create the intent.
* @param favicon Bookmark favicon bitmap.
* @param rValue Red component of the dominant favicon color.
* @param gValue Green component of the dominant favicon color.
* @param bValue Blue component of the dominant favicon color.
* @return Bitmap Either the touch-icon or the newly created favicon.
*/
private static Bitmap createIcon(Context context, Bitmap favicon, int rValue,
int gValue, int bValue) {
Bitmap bitmap = null;
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
final int iconSize = am.getLauncherLargeIconSize();
final int iconDensity = am.getLauncherLargeIconDensity();
try {
bitmap = Bitmap.createBitmap(iconSize, iconSize, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
if (favicon == null) {
favicon = getBitmapFromResourceId(context, R.drawable.globe_favicon, iconDensity);
rValue = gValue = bValue = DEFAULT_RGB_VALUE;
}
final int smallestSide = iconSize;
if (favicon.getWidth() >= smallestSide / 2 && favicon.getHeight() >= smallestSide / 2) {
drawTouchIconToCanvas(context, favicon, canvas);
} else {
drawWidgetBackgroundToCanvas(context, canvas, iconDensity,
Color.rgb(rValue, gValue, bValue));
drawFaviconToCanvas(context, favicon, canvas);
}
canvas.setBitmap(null);
} catch (OutOfMemoryError e) {
Log.w(TAG, "OutOfMemoryError while trying to draw bitmap on canvas.");
}
return bitmap;
}
/**
* Creates an icon to be associated with this bookmark. If available, the touch icon
* will be used, else we draw our own.
* @param context Context used to create the intent.
* @param favicon Bookmark favicon bitmap.
* @param rValue Red component of the dominant favicon color.
* @param gValue Green component of the dominant favicon color.
* @param bValue Blue component of the dominant favicon color.
* @return Bitmap Either the touch-icon or the newly created favicon.
*/
private static Bitmap createIcon(Context context, Bitmap favicon, int rValue,
int gValue, int bValue) {
Bitmap bitmap = null;
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
final int iconSize = am.getLauncherLargeIconSize();
final int iconDensity = am.getLauncherLargeIconDensity();
try {
bitmap = Bitmap.createBitmap(iconSize, iconSize, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
if (favicon == null) {
favicon = getBitmapFromResourceId(context, R.drawable.globe_favicon, iconDensity);
rValue = gValue = bValue = DEFAULT_RGB_VALUE;
}
final int smallestSide = iconSize;
if (favicon.getWidth() >= smallestSide / 2 && favicon.getHeight() >= smallestSide / 2) {
drawTouchIconToCanvas(context, favicon, canvas);
} else {
drawWidgetBackgroundToCanvas(context, canvas, iconDensity,
Color.rgb(rValue, gValue, bValue));
drawFaviconToCanvas(context, favicon, canvas);
}
canvas.setBitmap(null);
} catch (OutOfMemoryError e) {
Log.w(TAG, "OutOfMemoryError while trying to draw bitmap on canvas.");
}
return bitmap;
}
protected void onCreate(Bundle savedInstanceState, Intent intent,
CharSequence title, Intent[] initialIntents, List<ResolveInfo> rList,
boolean alwaysUseOption,int userid) {
super.onCreate(savedInstanceState);
mLaunchedFromUid = userid;
mPm = getPackageManager();
mAlwaysUseOption = alwaysUseOption;
mMaxColumns = getResources().getInteger(R.integer.config_maxResolverActivityColumns);
mRegistered = true;
final ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
mIconDpi = am.getLauncherLargeIconDensity();
mIconSize = am.getLauncherLargeIconSize();
mAdapter = new ResolveListAdapter(this, intent, initialIntents, rList,
mLaunchedFromUid);
int count = mAdapter.getCount();
if (Build.VERSION.SDK_INT >= 17) {
if (mLaunchedFromUid < 0) {
// Gulp!
finish();
return;
}
}
if (count == 1) {
startSelected(0, false);
mRegistered = false;
finish();
return;
}
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(title);
if (count > 1) {
mListView = new ListView(this);
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(this);
mListView.setOnItemLongClickListener(new ItemLongClickListener());
builder.setView(mListView);
if (alwaysUseOption) {
mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
} else {
builder.setMessage(R.string.noApplications);
}
builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
finish();
}
});
dialog = builder.show();
//
// if (alwaysUseOption) {
// final ViewGroup buttonLayout = (ViewGroup) findViewById(R.id.button_bar);
// if (buttonLayout != null) {
// buttonLayout.setVisibility(View.VISIBLE);
// mAlwaysButton = (Button) buttonLayout.findViewById(R.id.button_always);
// mOnceButton = (Button) buttonLayout.findViewById(R.id.button_once);
// } else {
// mAlwaysUseOption = false;
// }
// // Set the initial highlight if there was a preferred or last used choice
// final int initialHighlight = mAdapter.getInitialHighlight();
// if (initialHighlight >= 0) {
// mListView.setItemChecked(initialHighlight, true);
// onItemClick(null, null, initialHighlight, 0); // Other entries are not used
// }
// }
}
public AppIconRequestHandler(Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
dpi = am.getLauncherLargeIconDensity();
pm = context.getPackageManager();
}