下面列出了android.view.View#setBackground ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static void autoHandleSuccess(final Bitmap bitmap, final BlurConfig blurConfig) {
if (blurConfig.cache && !TextUtils.isEmpty(blurConfig.cacheKey)) {
addCacheBitmap(blurConfig.cacheKey, bitmap);
}
if (blurConfig.targetView != null && blurConfig.targetView.get() != null) {
View target = blurConfig.targetView.get();
if (target instanceof ImageView) {
((ImageView)target).setImageBitmap(bitmap);
} else {
Drawable drawable = new BitmapDrawable(target.getContext().getResources(), bitmap);
if (Build.VERSION.SDK_INT <= 16) {
target.setBackgroundDrawable(drawable);
} else {
target.setBackground(drawable);
}
}
if (blurConfig.animAlpha) {
animate(target, blurConfig.animDuration);
}
}
}
private void updateCheckStatus(View view, boolean checked) {
if (view == null) {
return;
}
// don't reuse drawable for different tag label
Drawable[] drawables = mDecorator.getBackgroundDrawable();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
view.setBackgroundDrawable(drawables[drawables.length > 1 ? (checked ? 1 : 0) : 0]);
} else {
view.setBackground(drawables[drawables.length > 1 ? (checked ? 1 : 0) : 0]);
}
final int[] color = mDecorator.getTextColor();
if (color != null && color.length > 0) {
((TextView) view).setTextColor((color[color.length > 1 ? (checked ? 1 : 0) : 0]));
}
}
@SuppressWarnings("deprecation")
public static void setBackGround(View view, Drawable drawable){
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN){
view.setBackgroundDrawable(drawable);
}
else{
view.setBackground(drawable);
}
}
/**
* Sets the background on a view to the provided drawable while retaining the padding
* of the original view (regardless of whether the provided drawable has its own padding)
*
* @param v The view whose background will be updated
* @param background A background drawable (can be null to clear the background)
*/
public static void setBackgroundRetainPadding(View v, Drawable background) {
//Need to transplant the padding due to background affecting it
int[] padding = {v.getPaddingLeft(), v.getPaddingTop(), v.getPaddingRight(), v.getPaddingBottom()};
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
v.setBackground(background);
} else {
v.setBackgroundDrawable(background);
}
v.setPadding(padding[0], padding[1], padding[2], padding[3]);
}
static void setRipple(View view, int pressedColor, Integer normalColor, int radius) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
view.setBackground(getRippleDrawable(pressedColor, normalColor, radius));
} else {
view.setBackgroundDrawable(getStateListDrawable(pressedColor, normalColor, radius));
}
}
private void tintViews(PreviewBar previewBar,
View morphView,
View frameView) {
int color = previewBar.getScrubberColor();
if (morphView.getBackgroundTintList() == null
|| morphView.getBackgroundTintList().getDefaultColor() != color) {
Drawable drawable = DrawableCompat.wrap(morphView.getBackground());
DrawableCompat.setTint(drawable, color);
morphView.setBackground(drawable);
frameView.setBackgroundColor(color);
}
}
static void setBackground(View view, Drawable drawable) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
view.setBackground(drawable);
} else {
view.setBackgroundDrawable(drawable);
}
}
@SuppressWarnings("deprecation")
private static void setBackground(View target, Drawable drawable) {
if (Build.VERSION.SDK_INT > 16) {
target.setBackground(drawable);
} else {
target.setBackgroundDrawable(drawable);
}
}
private void setBackground(View item, int drawableId) {
if (Build.VERSION.SDK_INT >= 21) {
item.setBackground(mParent.getResources().getDrawable(android.R.drawable.btn_default, mParent.getTheme()));
} else {
item.setBackground(mParent.getResources().getDrawable(android.R.drawable.btn_default));
}
}
protected static void setColor(View view, int color) {
if (color == 0) return;
Drawable drawable = view.getBackground();
if (drawable != null) {
drawable = drawable.mutate();
//drawable.setColorFilter(color, PorterDuff.Mode.MULTIPLY);
drawable.setColorFilter(getColorFilter(color));
if (Build.VERSION.SDK_INT >= 16) view.setBackground(drawable);
else view.setBackgroundDrawable(drawable);
} else {
view.setBackgroundColor(color);
}
}
@SuppressWarnings("deprecation")
public static void setBackgroundCompat(View v, Drawable drawable) {
if (hasJellyBean()) {
v.setBackground(drawable);
} else {
v.setBackgroundDrawable(drawable);
}
}
private static void setBackground(View target, Drawable drawable){
if(Build.VERSION.SDK_INT > 16){
target.setBackground(drawable);
}else{
target.setBackgroundDrawable(drawable);
}
}
@Override
protected void onBindView(View view) {
super.onBindView(view);
if (mDrawDivider) {
int left = view.getPaddingLeft();
int right = view.getPaddingRight();
int top = view.getPaddingTop();
int bottom = view.getPaddingBottom();
view.setBackground(DividerDrawable.create(getContext()));
view.setPadding(left, top, right, bottom);
}
SwitchCompat switchView = (SwitchCompat) view.findViewById(R.id.switch_widget);
// On BLU Life Play devices SwitchPreference.setWidgetLayoutResource() does nothing. As a
// result, the user will see a non-material Switch and switchView will be null, hence the
// null check below. http://crbug.com/451447
if (switchView != null) {
switchView.setChecked(isChecked());
}
TextView title = (TextView) view.findViewById(android.R.id.title);
title.setSingleLine(false);
if (!mDontUseSummaryAsTitle && TextUtils.isEmpty(getTitle())) {
TextView summary = (TextView) view.findViewById(android.R.id.summary);
title.setText(summary.getText());
title.setVisibility(View.VISIBLE);
summary.setVisibility(View.GONE);
}
if (mManagedPrefDelegate != null) mManagedPrefDelegate.onBindViewToPreference(this, view);
}
@SuppressLint("NewApi")
public static void setBackgroundCompat(View view, Drawable d) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
//noinspection deprecation
view.setBackgroundDrawable(d);
} else {
view.setBackground(d);
}
}
private View addViewToOverlay(ViewGroup sceneRoot, int width, int height, Drawable background) {
View view = new NoOverlapView(sceneRoot.getContext());
view.setBackground(background);
int widthSpec = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
int heightSpec = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY);
view.measure(widthSpec, heightSpec);
view.layout(0, 0, width, height);
sceneRoot.getOverlay().add(view);
return view;
}
private void changeColor(int pos, View change) {
Cursor csr = getCursor();
csr.moveToPosition(pos);
int color = csr.getInt(csr.getColumnIndexOrThrow("color"));
if(sdkVersion < android.os.Build.VERSION_CODES.JELLY_BEAN) {
change.setBackgroundDrawable(EnvelopesAdapter.getColorStateDrawable(color));
} else {
change.setBackground(EnvelopesAdapter.getColorStateDrawable(color));
}
}
void setBackgroundCompat(View view, Drawable d) {
view.setBackground(d);
}
public static final void setBackground(@NonNull View view, Drawable drawable) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
view.setBackground(drawable);
else
view.setBackgroundDrawable(drawable);
}
public void animation_change_color(final int Int) {
ImageView play_now_back_color = (ImageView) findViewById(R.id.play_now_back_color);
final RelativeLayout activity_now_play = (RelativeLayout) findViewById(R.id.activity_now_play);
if (cx == 0) {
FloatingActionButton play_or_pause = (FloatingActionButton) findViewById(R.id.play_or_pause);
RelativeLayout seekbar_layout = (RelativeLayout) findViewById(R.id.seekbar_layout);
RelativeLayout control_layout = (RelativeLayout) findViewById(R.id.control_layout);
cx = play_or_pause.getLeft() + control_layout.getLeft() + play_or_pause.getWidth() / 2;
cy = control_layout.getTop() - seekbar_layout.getTop() + play_or_pause.getTop() + play_or_pause.getHeight() / 2;
finalRadius = Math.max(play_now_back_color.getWidth(), play_now_back_color.getHeight());
}
if (cx != 0) {
Animator anim = ViewAnimationUtils.createCircularReveal(play_now_back_color, cx, cy, 0, finalRadius);
play_now_back_color.setBackgroundColor(Int);
anim.setDuration(500);
anim.start();
anim.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
activity_now_play.setBackgroundColor(Int);
}
});
} else {
activity_now_play.setBackgroundColor(Int);
}
TextView now_on_play_text = (TextView) findViewById(R.id.now_on_play_text);
now_on_play_text.setTextColor(Int);
//lrcview字体颜色
if (ColorUtil.isColorLight(Int)) {
otherLyricView.setNormalColor(Color.parseColor("#60000000"));
otherLyricView.setTimelineTextColor(Color.parseColor("#000000"));
otherLyricView.setCurrentColor(Color.parseColor("#000000"));
} else {
otherLyricView.setNormalColor(Color.parseColor("#60FFFFFF"));
otherLyricView.setTimelineTextColor(Color.parseColor("#FFFFFF"));
otherLyricView.setCurrentColor(Color.parseColor("#FFFFFF"));
}
//歌词背景颜色
if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
View bottom = findViewById(R.id.gradient_bottom);
View top = findViewById(R.id.gradient_top);
View gradient = findViewById(R.id.gradient);
top.setBackground(
ScrimUtil.makeCubicGradientScrimDrawable(Int, //颜色
8, //渐变层数
Gravity.TOP)); //起始方向
bottom.setBackground(
ScrimUtil.makeCubicGradientScrimDrawable(Int, //颜色
8, //渐变层数
Gravity.BOTTOM)); //起始方向
gradient.setBackground(
ScrimUtil.makeCubicGradientScrimDrawable(Int, //颜色
8, //渐变层数
Gravity.BOTTOM)); //起始方向
}
}
@TargetApi(21)
public static void afterOnPostCreateSDK21(XC_MethodHook.MethodHookParam mhparams) throws Throwable {
Class<?> internalStyleable = findClass("com.android.internal.R.styleable", null);
int[] internalTheme = $(getStaticObjectField(internalStyleable, "Theme"));
int internalColorPrimary = getStaticIntField(internalStyleable, "Theme_colorPrimaryDark");
SwipeBackActivityHelper helper = $(getAdditionalInstanceField(mhparams.thisObject, "helper"));
if (helper != null) {
final Activity activity = $(mhparams.thisObject);
String packageName = activity.getApplicationInfo().packageName;
String className = activity.getClass().getName();
mSettings.reload();
if (!mSettings.getBoolean(packageName, className, Settings.LOLLIPOP_HACK, false))
return;
ViewGroup root = $(helper.getSwipeBackLayout().getChildAt(0));
View content = root.getChildAt(0);
final WindowInsetsColorDrawable bkg = new WindowInsetsColorDrawable(content.getBackground());
content.setBackground(bkg);
TypedArray a = activity.getTheme().obtainStyledAttributes(internalTheme);
int primary = a.getColor(internalColorPrimary, 0);
a.recycle();
if (primary != 0) {
bkg.setTopDrawable(new ColorDrawable(primary));
} else {
content.setSystemUiVisibility(content.getSystemUiVisibility() | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
content.setFitsSystemWindows(true);
}
root.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
@Override
public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
bkg.setTopInset(insets.getSystemWindowInsetTop());
activity.getWindow().setStatusBarColor(0);
return insets;
}
});
}
}