下面列出了android.graphics.drawable.Drawable#getOpacity() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
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;
}
public boolean k(View view)
{
Drawable drawable = view.getBackground();
boolean flag = false;
if (drawable != null)
{
int l = drawable.getOpacity();
flag = false;
if (l == -1)
{
flag = true;
}
}
return flag;
}
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 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 Bitmap drawableToBitmap(Drawable drawable) {
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565;
Bitmap bitmap = Bitmap.createBitmap(width, height, config);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, width, height);
drawable.draw(canvas);
return bitmap;
}
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;
}
}
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();
}
private static boolean hasOpaqueBackground(View v) {
final Drawable bg = v.getBackground();
if (bg != null) {
return bg.getOpacity() == PixelFormat.OPAQUE;
}
return false;
}
private Bitmap drawableToBitmap(Drawable drawable) {
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ?
Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
Bitmap bitmap = Bitmap.createBitmap(width, height, config);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, width, height);
drawable.draw(canvas);
return bitmap;
}
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;
}
public boolean isOpaque(View view) {
Drawable bg = view.getBackground();
if (bg == null || bg.getOpacity() != -1) {
return false;
}
return true;
}
private static boolean hasOpaqueBackground(View v) {
final Drawable bg = v.getBackground();
return bg != null && bg.getOpacity() == PixelFormat.OPAQUE;
}
@Override
public void onViewCreated(Activity activity, View headerView) {
// Get ProgressBar and TextView. Also set initial text on TextView
mHeaderProgressBar = (ProgressBar) headerView.findViewById(R.id.ptr_progress);
mHeaderTextView = (TextView) headerView.findViewById(R.id.ptr_text);
// Apply any custom ProgressBar colors
applyProgressBarColor();
// Labels to display
mPullRefreshLabel = activity.getString(R.string.pull_to_refresh_pull_label);
mRefreshingLabel = activity.getString(R.string.pull_to_refresh_refreshing_label);
mReleaseLabel = activity.getString(R.string.pull_to_refresh_release_label);
// Retrieve the Action Bar size from the Activity's theme
mContentLayout = (ViewGroup) headerView.findViewById(R.id.ptr_content);
if (mContentLayout != null) {
mContentLayout.getLayoutParams().height = getActionBarSize(activity);
mContentLayout.requestLayout();
}
// Retrieve the Action Bar background from the Activity's theme (see #93).
Drawable abBg = getActionBarBackground(activity);
if (abBg != null) {
// If we do not have a opaque background we just display a solid solid behind it
if (abBg.getOpacity() != PixelFormat.OPAQUE) {
View view = headerView.findViewById(R.id.ptr_text_opaque_bg);
if (view != null) {
view.setVisibility(View.VISIBLE);
}
}
mHeaderTextView.setBackgroundDrawable(abBg);
}
// Retrieve the Action Bar Title Style from the Action Bar's theme
Context abContext = headerView.getContext();
final int titleTextStyle = getActionBarTitleStyle(abContext);
if (titleTextStyle != 0) {
mHeaderTextView.setTextAppearance(abContext, titleTextStyle);
}
// Call onReset to make sure that the View is consistent
onReset();
}
private static boolean hasOpaqueBackground(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.pmHeaderStyle, R.styleable.PullMenuHeader);
// 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.PullMenuHeader_pmHeaderHeight, 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.PullMenuHeader_pmHeaderBackground)
? styleAttrs.getDrawable(R.styleable.PullMenuHeader_pmHeaderBackground)
: 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 Progress Bar Color the style
if (styleAttrs.hasValue(R.styleable.PullMenuHeader_pmProgressBarColor)) {
mProgressDrawableColor = styleAttrs.getColor(
R.styleable.PullMenuHeader_pmProgressBarColor, mProgressDrawableColor);
}
mProgressBarStyle = styleAttrs.getInt(
R.styleable.PullMenuHeader_pmProgressBarStyle, PROGRESS_BAR_STYLE_INSIDE);
if (styleAttrs.hasValue(R.styleable.PullMenuHeader_pmProgressBarHeight)) {
mProgressBarHeight = styleAttrs.getDimensionPixelSize(
R.styleable.PullMenuHeader_pmProgressBarHeight, mProgressBarHeight);
}
//SmoothProgressBar Style
if (styleAttrs.hasValue(R.styleable.PullMenuHeader_pmSmoothProgressBarStyle)) {
int spbStyleRes = styleAttrs.getResourceId(R.styleable.PullMenuHeader_pmSmoothProgressBarStyle, 0);
if (spbStyleRes != 0)
mHeaderProgressBar.applyStyle(spbStyleRes);
}
styleAttrs.recycle();
}
private static boolean hasOpaqueBackground(@NonNull View v) {
final Drawable bg = v.getBackground();
return bg != null && bg.getOpacity() == PixelFormat.OPAQUE;
}