下面列出了android.view.View#getBackground ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void unbindDrawables(View view) {
if (null == view) return;
if (view.getBackground() != null) {
view.getBackground().setCallback(null);
view.setBackground(null);
}
if (view instanceof ViewGroup) {
ViewGroup container = (ViewGroup) view;
for (int i = 0, e = container.getChildCount(); i < e; i++) {
unbindDrawables(container.getChildAt(i));
}
container.removeAllViews();
} else if (view instanceof ImageView) {
ImageView image = (ImageView) view;
Drawable d = image.getDrawable();
if (d instanceof BitmapDrawable) {
Bitmap bmp = ((BitmapDrawable) d).getBitmap();
if (null != bmp) {
bmp.recycle();
}
}
image.setImageDrawable(null);
}
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
// test transition
View v = findViewById(R.id.test_transition);
TransitionDrawable drawable = (TransitionDrawable) v.getBackground();
drawable.startTransition(1000);
// test scale
View testScale = findViewById(R.id.test_scale);
ScaleDrawable testScaleDrawable = (ScaleDrawable) testScale.getBackground();
testScaleDrawable.setLevel(10);
// test clip
ImageView testClip = (ImageView) findViewById(R.id.test_clip);
ClipDrawable testClipDrawable = (ClipDrawable) testClip.getDrawable();
testClipDrawable.setLevel(8000);
// test custom drawable
View testCustomDrawable = findViewById(R.id.test_custom_drawable);
CustomDrawable customDrawable = new CustomDrawable(Color.parseColor("#0ac39e"));
testCustomDrawable.setBackgroundDrawable(customDrawable);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.com_facebook_usersettingsfragment, container, false);
loginButton = (LoginButton) view.findViewById(R.id.com_facebook_usersettingsfragment_login_button);
loginButton.setProperties(loginButtonProperties);
loginButton.setFragment(this);
loginButton.setLoginLogoutEventName(AnalyticsEvents.EVENT_USER_SETTINGS_USAGE);
Session session = getSession();
if (session != null && !session.equals(Session.getActiveSession())) {
loginButton.setSession(session);
}
connectedStateLabel = (TextView) view.findViewById(R.id.com_facebook_usersettingsfragment_profile_name);
// if no background is set for some reason, then default to Facebook blue
if (view.getBackground() == null) {
view.setBackgroundColor(getResources().getColor(R.color.com_facebook_blue));
} else {
view.getBackground().setDither(true);
}
return view;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.com_facebook_usersettingsfragment, container, false);
loginButton = (LoginButton) view.findViewById(R.id.com_facebook_usersettingsfragment_login_button);
loginButton.setProperties(loginButtonProperties);
loginButton.setFragment(this);
Session session = getSession();
if (session != null && !session.equals(Session.getActiveSession())) {
loginButton.setSession(session);
}
connectedStateLabel = (TextView) view.findViewById(R.id.com_facebook_usersettingsfragment_profile_name);
// if no background is set for some reason, then default to Facebook blue
if (view.getBackground() == null) {
view.setBackgroundColor(getResources().getColor(R.color.com_facebook_blue));
} else {
view.getBackground().setDither(true);
}
return view;
}
/**
* Due limitation in Android platform, the linear gradient in the following page will not be
* rounded if {@link Canvas#clipPath(Path)} of the parent view invoked when API level is lower
* than 21.
* http://dotwe.org/weex/963c9ade129f86757cecdd85651cd30e
* @param targetView
* @param borderDrawable
* @return
*/
private static boolean clipCanvasIfBackgroundImageExist(@NonNull View targetView,
@NonNull BorderDrawable borderDrawable) {
if (targetView instanceof ViewGroup) {
View child;
ViewGroup parent = ((ViewGroup) targetView);
int count = parent.getChildCount();
for (int i = 0; i < count; i++) {
child = parent.getChildAt(i);
if (child.getBackground() instanceof BorderDrawable &&
((BorderDrawable) child.getBackground()).hasImage() &&
Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
return false;
}
}
}
return true;
}
/**
* view转Bitmap
*
* @param view 视图
* @return bitmap
*/
public static Bitmap view2Bitmap(View view) {
if (view == null) return null;
Bitmap ret = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(ret);
Drawable bgDrawable = view.getBackground();
if (bgDrawable != null) {
bgDrawable.draw(canvas);
} else {
canvas.drawColor(Color.WHITE);
}
view.draw(canvas);
return ret;
}
@Override
public void onBindViewHolder(PreferenceViewHolder holder, int position) {
super.onBindViewHolder(holder, position);
if (position == mHighlightPosition) {
View v = holder.itemView;
if (v.getBackground() != null) {
final int centerX = v.getWidth() / 2;
final int centerY = v.getHeight() / 2;
v.getBackground().setHotspot(centerX, centerY);
}
v.setPressed(true);
v.setPressed(false);
mHighlightPosition = -1;
}
}
@Override
public Animator onAppear(ViewGroup sceneRoot, View view,
TransitionValues startValues, TransitionValues endValues) {
if (view == null || view.getBackground() == null) return null;
Drawable background = view.getBackground();
background.setAlpha(0);
return ObjectAnimator.ofInt(background, ViewUtils.DRAWABLE_ALPHA, 0, 255);
}
private static boolean hasOpaqueBackground(View v) {
final Drawable bg = v.getBackground();
if (bg != null) {
return bg.getOpacity() == PixelFormat.OPAQUE;
}
return false;
}
private static boolean viewIsOpaque(View v) {
if (ViewCompat.isOpaque(v)) return true;
// View#isOpaque didn't take all valid opaque scrollbar modes into account
// before API 18 (JB-MR2). On newer devices rely solely on isOpaque above and return false
// here. On older devices, check the view's background drawable directly as a fallback.
if (Build.VERSION.SDK_INT >= 18) return false;
final Drawable bg = v.getBackground();
if (bg != null) {
return bg.getOpacity() == PixelFormat.OPAQUE;
}
return false;
}
/**
*
* @param view
* @param info
*/
static void applySupportBackgroundTint(View view, TintInfo info) {
final Drawable background = view.getBackground();
if (background != null) {
if (info != null) {
EmTintManager.tintDrawable(background, info, view.getDrawableState());
}
}
}
private void unbindDrawables(View view) {
if (view == null)
return;
if (view.getBackground() != null) {
view.getBackground().setCallback(null);
}
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
unbindDrawables(((ViewGroup) view).getChildAt(i));
}
((ViewGroup) view).removeAllViews();
}
}
@Override
public Integer get(View view) {
Drawable background = view.getBackground();
if (background instanceof LayerDrawable
&& ((LayerDrawable) background).getNumberOfLayers() > 0) {
background = ((LayerDrawable) background).getDrawable(0);
}
return DrawableCompat.getAlpha(background);
}
/**
* view转Bitmap
*
* @param view 视图
* @return bitmap
*/
public static Bitmap view2Bitmap(View view) {
if (view == null) return null;
Bitmap ret = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(ret);
Drawable bgDrawable = view.getBackground();
if (bgDrawable != null) {
bgDrawable.draw(canvas);
} else {
canvas.drawColor(Color.WHITE);
}
view.draw(canvas);
return ret;
}
private static boolean hasOpaqueBackground(View v) {
final Drawable bg = v.getBackground();
if (bg != null) {
return bg.getOpacity() == PixelFormat.OPAQUE;
}
return false;
}
@Nullable
private static FrescoDrawable2 getDrawable(final View view) {
Drawable d =
view instanceof ImageView ? ((ImageView) view).getDrawable() : view.getBackground();
return d instanceof FrescoDrawable2 ? (FrescoDrawable2) d : null;
}
private static boolean hasOpaqueBackground(View v) {
final Drawable bg = v.getBackground();
return bg != null && bg.getOpacity() == PixelFormat.OPAQUE;
}
private void setSmallBackground(View v){
v.setBackgroundResource(R.drawable.background_small_indicator);
LayerDrawable layer = (LayerDrawable) v.getBackground();
GradientDrawable shape = (GradientDrawable) layer.findDrawableByLayerId(R.id.shape_bacground);
shape.setColor(colorIndicator);
}
private void setViewColor(View view, int color) {
if (view.getBackground() instanceof GradientDrawable) {
GradientDrawable drawable = (GradientDrawable) view.getBackground().mutate();
drawable.setColor(color);
}
}
private static boolean hasOpaqueBackground(View v) {
final Drawable bg = v.getBackground();
return bg != null && bg.getOpacity() == PixelFormat.OPAQUE;
}