下面列出了android.graphics.drawable.ColorDrawable#draw() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public IcsColorDrawable(ColorDrawable drawable) {
Bitmap bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
drawable.draw(c);
this.color = bitmap.getPixel(0, 0);
bitmap.recycle();
}
@ColorInt
public static int getDrawableColor(ColorDrawable colorDrawable) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
return colorDrawable.getColor();
} else {
// http://stackoverflow.com/questions/15982044/get-activity-background-color-in-android-api-level-8
Bitmap bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_4444);
Canvas canvas = new Canvas(bitmap);
colorDrawable.draw(canvas);
final int color = bitmap.getPixel(0, 0);
bitmap.recycle();
return color;
}
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static int getColorDrawableColor(ColorDrawable drawable) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
Bitmap bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_4444);
Canvas canvas = new Canvas(bitmap);
drawable.draw(canvas);
int pix = bitmap.getPixel(0, 0);
bitmap.recycle();
return pix;
} else {
return drawable.getColor();
}
}
public IcsColorDrawable(ColorDrawable drawable) {
Bitmap bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
drawable.draw(c);
this.color = bitmap.getPixel(0, 0);
bitmap.recycle();
}
public IcsColorDrawable(ColorDrawable drawable) {
Bitmap bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
drawable.draw(c);
this.color = bitmap.getPixel(0, 0);
bitmap.recycle();
}
public IcsColorDrawable(ColorDrawable drawable) {
Bitmap bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
drawable.draw(c);
this.color = bitmap.getPixel(0, 0);
bitmap.recycle();
}
@Override
protected void onDraw(Canvas canvas) {
if (!mIsDragTarget) {
return;
}
// When we're large, we are either drawn in a "hover" state (ie when dragging an item to
// a neighboring page) or with just a normal background (if backgroundAlpha > 0.0f)
// When we're small, we are either drawn normally or in the "accepts drops" state (during
// a drag). However, we also drag the mini hover background *over* one of those two
// backgrounds
if (mBackgroundAlpha > 0.0f) {
mBackground.draw(canvas);
}
final Paint paint = mDragOutlinePaint;
for (int i = 0; i < mDragOutlines.length; i++) {
final float alpha = mDragOutlineAlphas[i];
if (alpha > 0) {
final Bitmap b = (Bitmap) mDragOutlineAnims[i].getTag();
paint.setAlpha((int)(alpha + .5f));
canvas.drawBitmap(b, null, mDragOutlines[i], paint);
}
}
if (DEBUG_VISUALIZE_OCCUPIED) {
int[] pt = new int[2];
ColorDrawable cd = new ColorDrawable(Color.RED);
cd.setBounds(0, 0, mCellWidth, mCellHeight);
for (int i = 0; i < mCountX; i++) {
for (int j = 0; j < mCountY; j++) {
if (mOccupied.cells[i][j]) {
cellToPoint(i, j, pt);
canvas.save();
canvas.translate(pt[0], pt[1]);
cd.draw(canvas);
canvas.restore();
}
}
}
}
for (int i = 0; i < mFolderBackgrounds.size(); i++) {
FolderIcon.PreviewBackground bg = mFolderBackgrounds.get(i);
cellToPoint(bg.delegateCellX, bg.delegateCellY, mTempLocation);
canvas.save();
canvas.translate(mTempLocation[0], mTempLocation[1]);
bg.drawBackground(mLauncher, canvas);
if (!bg.isClipping) {
bg.drawBackgroundStroke(mLauncher, canvas);
}
canvas.restore();
}
if (mFolderLeaveBehind.delegateCellX >= 0 && mFolderLeaveBehind.delegateCellY >= 0) {
cellToPoint(mFolderLeaveBehind.delegateCellX,
mFolderLeaveBehind.delegateCellY, mTempLocation);
canvas.save();
canvas.translate(mTempLocation[0], mTempLocation[1]);
mFolderLeaveBehind.drawLeaveBehind(canvas);
canvas.restore();
}
}