下面列出了android.graphics.PixelFormat#OPAQUE 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static Bitmap drawableToBitmap(Drawable drawable) {
// 取 drawable 的长宽
int w = drawable.getIntrinsicWidth();
int h = drawable.getIntrinsicHeight();
// 取 drawable 的颜色格式
Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565;
// 建立对应 bitmap
Bitmap bitmap = Bitmap.createBitmap(w, h, config);
// 建立对应 bitmap 的画布
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, w, h);
// 把 drawable 内容画到画布中
drawable.draw(canvas);
return bitmap;
}
private static Bitmap drawable2Bitmap(Drawable drawable) {
if (drawable == null) {
return null;
}
// 取 drawable 的长宽
int w = drawable.getIntrinsicWidth();
int h = drawable.getIntrinsicHeight();
// 取 drawable 的颜色格式
Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE
? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565;
// 建立对应 bitmap
Bitmap bitmap = Bitmap.createBitmap(w, h, config);
// 建立对应 bitmap 的画布
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, w, h);
// 把 drawable 内容画到画布中
drawable.draw(canvas);
return bitmap;
}
private static Bitmap drawable2Bitmap(Drawable drawable) {
if (drawable == null) {
return null;
}
// 取 drawable 的长宽
int w = drawable.getIntrinsicWidth();
int h = drawable.getIntrinsicHeight();
// 取 drawable 的颜色格式
Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565;
// 建立对应 bitmap
Bitmap bitmap = Bitmap.createBitmap(w, h, config);
// 建立对应 bitmap 的画布
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, w, h);
// 把 drawable 内容画到画布中
drawable.draw(canvas);
return bitmap;
}
/**
* Drawable → Bitmap
*/
public static Bitmap drawable2Bitmap(Drawable drawable) {
if (drawable == null) {
return null;
}
// 取 drawable 的长宽
int w = drawable.getIntrinsicWidth();
int h = drawable.getIntrinsicHeight();
// 取 drawable 的颜色格式
Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
// 建立对应 bitmap
Bitmap bitmap = Bitmap.createBitmap(w, h, config);
// 建立对应 bitmap 的画布
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, w, h);
// 把 drawable 内容画到画布中
drawable.draw(canvas);
return bitmap;
}
/**
* drawable转bitmap
*
* @param drawable drawable对象
* @return bitmap对象
*/
public static Bitmap drawable2Bitmap(Drawable drawable) {
// 取 drawable 的长宽
int w = drawable.getIntrinsicWidth();
int h = drawable.getIntrinsicHeight();
// 取 drawable 的颜色格式
Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565;
// 建立对应 bitmap
Bitmap bitmap = Bitmap.createBitmap(w, h, config);
// 建立对应 bitmap 的画布
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, w, h);
// 把 drawable 内容画到画布中
drawable.draw(canvas);
return bitmap;
}
public void setDivider(Drawable divider) {
if (divider != null) {
mDividerHeight = divider.getIntrinsicHeight();
} else {
mDividerHeight = 0;
}
mDivider = divider;
mDividerIsOpaque = divider == null || divider.getOpacity() == PixelFormat.OPAQUE;
requestLayout();
invalidate();
}
@Override
public int getOpacity() {
final int alpha = paint.getAlpha();
if (alpha == 0) return PixelFormat.TRANSPARENT;
if (alpha == 0xff) return PixelFormat.OPAQUE;
return PixelFormat.TRANSLUCENT;
}
@Override
public void setFormat(int format) {
// for backward compatibility reason, OPAQUE always
// means 565 for SurfaceView
if (format == PixelFormat.OPAQUE)
format = PixelFormat.RGB_565;
mRequestedFormat = format;
if (mSurfaceControl != null) {
updateSurface();
}
}
public static Bitmap drawableToBitmap(Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
} else {
int width = drawable.getIntrinsicWidth();// 取drawable的长宽
int height = drawable.getIntrinsicHeight();
Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;// 取drawable的颜色格式
Bitmap bitmap = Bitmap.createBitmap(width, height, config);// 建立对应bitmap
Canvas canvas = new Canvas(bitmap);// 建立对应bitmap的画布
drawable.setBounds(0, 0, width, height);
drawable.draw(canvas);// 把drawable内容画到画布中
return bitmap;
}
}
/**
* @param drawable
* @return
*/
public static Bitmap drawableToBitmap(Drawable drawable) {
int w = drawable.getIntrinsicWidth();
int h = drawable.getIntrinsicHeight();
Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565;
Bitmap bitmap = Bitmap.createBitmap(w, h, config);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, w, h);
drawable.draw(canvas);
return bitmap;
}
private static boolean hasOpaqueBackground(View v) {
final Drawable bg = v.getBackground();
if (bg != null) {
return bg.getOpacity() == PixelFormat.OPAQUE;
}
return false;
}
@Override
public boolean isOpaque() {
return super.isOpaque() || mDrawable != null && mXfermode == null
&& mDrawable.getOpacity() == PixelFormat.OPAQUE
&& mAlpha * mViewAlphaScale >> 8 == 255
&& isFilledByImage();
}
@Override
public int getOpacity() {
return PixelFormat.OPAQUE;
}
private static boolean hasOpaqueBackground(View v) {
final Drawable bg = v.getBackground();
return bg != null && bg.getOpacity() == PixelFormat.OPAQUE;
}
private static boolean hasOpaqueBackground(@NonNull View v) {
final Drawable bg = v.getBackground();
return bg != null && bg.getOpacity() == PixelFormat.OPAQUE;
}
private void setupViewsFromStyles(Activity activity, View headerView) {
final TypedArray styleAttrs = obtainStyledAttrsFromThemeAttr(activity,
R.attr.ptrHeaderStyle, R.styleable.PullToRefreshHeader);
// Retrieve the Action Bar size from the app theme or the Action Bar's style
if (mContentLayout != null) {
final int height = styleAttrs.getDimensionPixelSize(
R.styleable.PullToRefreshHeader_ptrHeaderHeight, getActionBarSize(activity));
mContentLayout.getLayoutParams().height = height;
mContentLayout.requestLayout();
}
// Retrieve the Action Bar background from the app theme or the Action Bar's style (see #93)
Drawable bg = styleAttrs.hasValue(R.styleable.PullToRefreshHeader_ptrHeaderBackground)
? styleAttrs.getDrawable(R.styleable.PullToRefreshHeader_ptrHeaderBackground)
: getActionBarBackground(activity);
if (bg != null) {
mHeaderTextView.setBackgroundDrawable(bg);
// If we have an opaque background we can remove the background from the content layout
if (mContentLayout != null && bg.getOpacity() == PixelFormat.OPAQUE) {
mContentLayout.setBackgroundResource(0);
}
}
// Retrieve the Action Bar Title Style from the app theme or the Action Bar's style
Context abContext = headerView.getContext();
final int titleTextStyle = styleAttrs
.getResourceId(R.styleable.PullToRefreshHeader_ptrHeaderTitleTextAppearance,
getActionBarTitleStyle(abContext));
if (titleTextStyle != 0) {
mHeaderTextView.setTextAppearance(abContext, titleTextStyle);
}
// Retrieve the Progress Bar Color the style
if (styleAttrs.hasValue(R.styleable.PullToRefreshHeader_ptrProgressBarColor)) {
mProgressDrawableColor = styleAttrs.getColor(
R.styleable.PullToRefreshHeader_ptrProgressBarColor, mProgressDrawableColor);
}
mProgressBarStyle = styleAttrs.getInt(
R.styleable.PullToRefreshHeader_ptrProgressBarStyle, PROGRESS_BAR_STYLE_OUTSIDE);
if (styleAttrs.hasValue(R.styleable.PullToRefreshHeader_ptrProgressBarHeight)) {
mProgressBarHeight = styleAttrs.getDimensionPixelSize(
R.styleable.PullToRefreshHeader_ptrProgressBarHeight, mProgressBarHeight);
}
// Retrieve the text strings from the style (if they're set)
if (styleAttrs.hasValue(R.styleable.PullToRefreshHeader_ptrPullText)) {
mPullRefreshLabel = styleAttrs.getString(R.styleable.PullToRefreshHeader_ptrPullText);
}
if (styleAttrs.hasValue(R.styleable.PullToRefreshHeader_ptrRefreshingText)) {
mRefreshingLabel = styleAttrs
.getString(R.styleable.PullToRefreshHeader_ptrRefreshingText);
}
if (styleAttrs.hasValue(R.styleable.PullToRefreshHeader_ptrReleaseText)) {
mReleaseLabel = styleAttrs.getString(R.styleable.PullToRefreshHeader_ptrReleaseText);
}
//SmoothProgressBar Style
if (styleAttrs.hasValue(R.styleable.PullToRefreshHeader_ptrSmoothProgressBarStyle)) {
int spbStyleRes = styleAttrs.getResourceId(R.styleable.PullToRefreshHeader_ptrSmoothProgressBarStyle, 0);
if (spbStyleRes != 0)
mHeaderProgressBar.applyStyle(spbStyleRes);
}
styleAttrs.recycle();
}
private void setupViewsFromStyles(Activity activity, View headerView) {
final TypedArray styleAttrs = obtainStyledAttrsFromThemeAttr(activity,
R.attr.ptrHeaderStyle, R.styleable.PullToRefreshHeader);
// Retrieve the Action Bar size from the app theme or the Action Bar's style
if (mContentLayout != null) {
final int height = styleAttrs
.getDimensionPixelSize(R.styleable.PullToRefreshHeader_ptrHeaderHeight,
getActionBarSize(activity));
mContentLayout.getLayoutParams().height = height;
mContentLayout.requestLayout();
}
// Retrieve the Action Bar background from the app theme or the Action Bar's style (see #93)
Drawable bg = styleAttrs.hasValue(R.styleable.PullToRefreshHeader_ptrHeaderBackground)
? styleAttrs.getDrawable(R.styleable.PullToRefreshHeader_ptrHeaderBackground)
: getActionBarBackground(activity);
if (bg != null) {
mHeaderTextView.setBackgroundDrawable(bg);
// If we have an opaque background we can remove the background from the content layout
if (mContentLayout != null && bg.getOpacity() == PixelFormat.OPAQUE) {
mContentLayout.setBackgroundResource(0);
}
}
// Retrieve the Action Bar Title Style from the app theme or the Action Bar's style
Context abContext = headerView.getContext();
final int titleTextStyle = styleAttrs
.getResourceId(R.styleable.PullToRefreshHeader_ptrHeaderTitleTextAppearance,
getActionBarTitleStyle(abContext));
if (titleTextStyle != 0) {
mHeaderTextView.setTextAppearance(abContext, titleTextStyle);
}
// Retrieve the Progress Bar Color the style
if (styleAttrs.hasValue(R.styleable.PullToRefreshHeader_ptrProgressBarColor)) {
mUseCustomProgressColor = true;
mProgressDrawableColor = styleAttrs
.getColor(R.styleable.PullToRefreshHeader_ptrProgressBarColor, 0);
}
// Retrieve the text strings from the style (if they're set)
if (styleAttrs.hasValue(R.styleable.PullToRefreshHeader_ptrPullText)) {
mPullRefreshLabel = styleAttrs.getString(R.styleable.PullToRefreshHeader_ptrPullText);
}
if (styleAttrs.hasValue(R.styleable.PullToRefreshHeader_ptrRefreshingText)) {
mRefreshingLabel = styleAttrs
.getString(R.styleable.PullToRefreshHeader_ptrRefreshingText);
}
if (styleAttrs.hasValue(R.styleable.PullToRefreshHeader_ptrReleaseText)) {
mReleaseLabel = styleAttrs.getString(R.styleable.PullToRefreshHeader_ptrReleaseText);
}
styleAttrs.recycle();
}
private static boolean hasOpaqueBackground(View v) {
final Drawable bg = v.getBackground();
return bg != null && bg.getOpacity() == PixelFormat.OPAQUE;
}
public static Bitmap drawableToBitmap(Drawable drawable) {
int w = drawable.getIntrinsicWidth();
int h = drawable.getIntrinsicHeight();
Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
Bitmap bitmap = Bitmap.createBitmap(w, h, config);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, w, h);
drawable.draw(canvas);
return bitmap;
}
@Override
public int getOpacity() {
return PixelFormat.OPAQUE;
}