下面列出了android.widget.ImageView#setBackground ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void display(Bitmap bitmap, ImageAware imageaware, LoadedFrom loadedfrom)
{
if (!(imageaware instanceof ImageViewAware))
{
throw new IllegalArgumentException("ImageAware should wrap ImageView. ImageViewAware is expected.");
}
Bitmap bitmap1 = Util.clipToRoundBitmap(bitmap);
ImageView imageview = (ImageView)imageaware.getWrappedView();
if (bitmap1 == null || imageview == null)
{
return;
} else
{
imageview.setBackground(new BitmapDrawable(imageview.getResources(), bitmap1));
return;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView button = (ImageView) findViewById(R.id.image);
InputStream resource = getResources().openRawResource(R.drawable.eye);
Bitmap bitmap = BitmapFactory.decodeStream(resource);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
Bitmap b = Bitmap.createScaledBitmap(bitmap, width, height, false);
button.setBackground(new MyRoundCornerDrawable(b));
}
private void init() {
ImageView closeButton = new ImageView(getContext());
closeButton.setLayoutParams(new RelativeLayout.LayoutParams((int) (mCloseButtonSize), (int) (mCloseButtonSize)));
StateListDrawable drawable = new StateListDrawable();
ShapeDrawable shape = new ShapeDrawable(new OvalShape());
ShapeDrawable shapePressed = new ShapeDrawable(new OvalShape());
shape.setColorFilter(mCloseColor, PorterDuff.Mode.SRC_ATOP);
shapePressed.setColorFilter(mCloseColor - 0x444444, PorterDuff.Mode.SRC_ATOP);//a little bit darker
drawable.addState(new int[]{android.R.attr.state_pressed}, shapePressed);
drawable.addState(new int[]{}, shape);
closeButton.setImageResource(mCloseIcon);
closeButton.setBackground(drawable); //todo change this to support lower api
closeButton.setClickable(true);
closeButton.setId(R.id.closeId);
mImageView = new CustomImageView(getContext(), mCloseButtonSize, mCloseButtonMargin, mCornerRadius);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.setMargins(Math.round(mTopLeftMargin), Math.round(mTopLeftMargin), 0, 0);
mImageView.setLayoutParams(params);
mImageView.setAdjustViewBounds(true);
addView(mImageView);
addView(closeButton);
}
private void setPreviewColor() {
if (mView == null) return;
ImageView iView = new ImageView(getContext());
LinearLayout widgetFrameView = ((LinearLayout)mView.findViewById(android.R.id.widget_frame));
if (widgetFrameView == null) return;
widgetFrameView.setVisibility(View.VISIBLE);
widgetFrameView.setPadding(
widgetFrameView.getPaddingLeft(),
widgetFrameView.getPaddingTop(),
(int)(mDensity * 8),
widgetFrameView.getPaddingBottom()
);
// remove already create preview image
int count = widgetFrameView.getChildCount();
if (count > 0) {
widgetFrameView.removeViews(0, count);
}
widgetFrameView.addView(iView);
widgetFrameView.setMinimumWidth(0);
iView.setBackground(new AlphaPatternDrawable((int)(5 * mDensity)));
iView.setImageBitmap(getPreviewBitmap());
}
@Override
protected void initActionBar(ActionBar actionBar) {
if (actionBar == null)
return;
int padding = CompatResourceUtils.getDimensionPixelSize(this, R.dimen.space_12);
FrameLayout customView = new FrameLayout(this);
// customView.setPadding(padding, 0, padding, 0);
ActionBar.LayoutParams barParams = new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, WindowUtils.getActionBarSize(this));
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(customView, barParams);
//添加标题
tvTitle = new TextView(this);
tvTitle.setTextColor(Color.WHITE);
tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
tvTitle.setGravity(Gravity.CENTER);
customView.addView(tvTitle, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
//添加返回按钮
ivBack = new ImageView(this);
ivBack.setPadding(padding / 2, 0, padding / 2, 0);
ivBack.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
ivBack.setImageResource(R.drawable.ic_chevron_left_white_24dp);
ivBack.setBackground(WindowUtils.getSelectableItemBackgroundBorderless(this));
customView.addView(ivBack, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));
ivBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
//添加menu菜单
actionMenuView = new ActionMenuView(this);
FrameLayout.LayoutParams menuParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
menuParams.gravity = Gravity.END | Gravity.CENTER_VERTICAL;
customView.addView(actionMenuView, menuParams);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView==null)
convertView = inf.inflate(layout, null);
ImageView iv = (ImageView) convertView.findViewById(R.id.setting_image_pic);
KcaUtils.setFairyImageFromStorage(context, item.get(position), iv, IMAGE_SIZE);
if (position == prevactive) iv.setBackground(ContextCompat.getDrawable(context, R.drawable.imagebtn_on));
else iv.setBackground(ContextCompat.getDrawable(context, R.drawable.imagebtn_off));
if (rescale > 0) {
iv.getLayoutParams().width = rescale;
iv.getLayoutParams().height = rescale;
}
return convertView;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView==null)
convertView = inf.inflate(layout, null);
ImageView iv = (ImageView) convertView.findViewById(R.id.setting_image_pic);
iv.setImageResource(item.get(position));
if (selected.contains(position)) iv.setBackground(ContextCompat.getDrawable(context, R.drawable.imagebtn_on));
else iv.setBackground(ContextCompat.getDrawable(context, R.drawable.imagebtn_off));
if (rescale > 0) {
iv.getLayoutParams().width = rescale;
iv.getLayoutParams().height = rescale;
}
return convertView;
}
private void setEquipButton() {
String filter = getStringPreferences(getApplicationContext(), PREF_AKASHI_FILTERLIST);
for(int type: T3LIST_IMPROVABLE) {
int btnId = getId("akashi_filter_equip_btn_".concat(String.valueOf(type)), R.id.class);
ImageView btnView = (ImageView) findViewById(btnId);
if(checkFiltered(filter, type)) {
btnView.setBackground(ContextCompat.getDrawable(this, R.drawable.imagebtn_off));
} else {
btnView.setBackground(ContextCompat.getDrawable(this, R.drawable.imagebtn_on));
}
}
}
private ViewGroup buildDot(boolean stroke) {
ViewGroup dot = (ViewGroup) LayoutInflater.from(getContext()).inflate(R.layout.spring_dot_layout, this, false);
ImageView dotView = dot.findViewById(R.id.spring_dot);
dotView.setBackground(
AppCompatResources.getDrawable(getContext(), stroke ? R.drawable.spring_dot_stroke_background : R.drawable.spring_dot_background));
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) dotView.getLayoutParams();
params.width = params.height = stroke ? dotsStrokeSize : dotIndicatorSize;
params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
params.setMargins(dotsSpacing, 0, dotsSpacing, 0);
setUpDotBackground(stroke, dotView);
return dot;
}
@Override
protected void initActionBar(ActionBar actionBar) {
if (actionBar == null)
return;
int padding = CompatResourceUtils.getDimensionPixelSize(this, R.dimen.space_12);
FrameLayout customView = new FrameLayout(this);
// customView.setPadding(padding, 0, padding, 0);
ActionBar.LayoutParams barParams = new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, WindowUtils.getActionBarSize(this));
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(customView, barParams);
//添加标题
tvTitle = new TextView(this);
tvTitle.setTextColor(Color.WHITE);
tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
tvTitle.setGravity(Gravity.CENTER);
customView.addView(tvTitle, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
//添加返回按钮
ivBack = new ImageView(this);
ivBack.setPadding(padding / 2, 0, padding / 2, 0);
ivBack.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
ivBack.setImageResource(R.drawable.ic_chevron_left_white_24dp);
ivBack.setBackground(WindowUtils.getSelectableItemBackgroundBorderless(this));
customView.addView(ivBack, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));
ivBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
//添加menu菜单
actionMenuView = new ActionMenuView(this);
FrameLayout.LayoutParams menuParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
menuParams.gravity = Gravity.END | Gravity.CENTER_VERTICAL;
customView.addView(actionMenuView, menuParams);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView==null)
convertView = inf.inflate(layout, null);
ImageView iv = (ImageView) convertView.findViewById(R.id.setting_image_pic);
iv.setImageResource(item.get(position));
if (selected.contains(position)) iv.setBackground(ContextCompat.getDrawable(context, R.drawable.imagebtn_on));
else iv.setBackground(ContextCompat.getDrawable(context, R.drawable.imagebtn_off));
if (rescale > 0) {
iv.getLayoutParams().width = rescale;
iv.getLayoutParams().height = rescale;
}
return convertView;
}
/**
* 添加数据
* @param viewPager
*/
public void addPagerData(int count, ViewPager viewPager) {
/**
* 还原一些状态
*/
removeAllViews();
mMoveDistance = 0;
if (count == 0) {
return;
}
mViewPager = viewPager;
mCount = count;
GradientDrawable drawable = new GradientDrawable();
drawable.setShape(GradientDrawable.OVAL);
drawable.setSize(mSize, mSize);
drawable.setColor(mDefaultColor);
for (int i = 0; i < mCount; i++) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
if (i == mCount - 1) {
params.setMargins(mMargin, 0, mMargin, 0);
} else {
params.setMargins(mMargin, 0, 0, 0);
}
ImageView imageView = new ImageView(getContext());
imageView.setBackground(drawable);
imageView.setLayoutParams(params);
addView(imageView);
}
if (viewPager != null) {
viewPager.addOnPageChangeListener(new PagerListener());
}
}
public static void createCircularPreferenceBitmap(Boolean isImage, Preference preference, ImageView imageView, Context context, int color) {
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
int dimen = (int) context.getResources().getDimension(android.R.dimen.app_icon_size);
Bitmap bmp = Bitmap.createBitmap(dimen, dimen, conf);
if (isImage) {
imageView.setBackground(createRoundedBitmapDrawable(bmp, color, context.getResources()));
} else {
preference.setIcon(createRoundedBitmapDrawable(bmp, color, context.getResources()));
}
}
/**
* 建立添加按钮
*/
private View createAddTagView() {
final BackgroundView bgLayout = new BackgroundView(getContext());
ImageView iv = new ImageView(getContext());
LayoutParams params = new LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
params.setMargins(textLRMargin, textTBMargin, textLRMargin, textTBMargin);
iv.setLayoutParams(params);
iv.setImageResource(R.drawable.ic_svg_add);
iv.setScaleType(ImageView.ScaleType.CENTER);
iv.setBackgroundColor(Color.LTGRAY);
StateListDrawable pressedSelector = Utils.getPressedSelector(getContext(),
Color.LTGRAY, Color.LTGRAY, mCourseItemRadius);
iv.setBackground(pressedSelector);
iv.setClickable(true);
iv.setFocusable(true);
iv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mItemClickListener != null) {
mItemClickListener.onAdd(mAddTagCourse, mAddTagCourseView);
removeAddTagView();
}
}
});
bgLayout.addView(iv);
return bgLayout;
}
/**
* Sets a clicked cell on/off.
*
* @param image An image of a clicked cell.
* @param pos Position of a clicked cell.
* @return True, if cell is on and false otherwise.
*/
private boolean toggleLED(ImageView image, int pos) {
boolean isOn;
//Toast.makeText(this, "Pos :" + pos, Toast.LENGTH_SHORT).show();
int state = (Integer) image.getTag(R.id.ledState);
if(state != 1) {
DEVICE_CODE_ARRAY[pos] = 1;
image.setBackground(getApplication().getResources().getDrawable(R.drawable.red_white_led_btn));
image.setTag(R.id.ledState, 1);
isOn = true;
} else {
DEVICE_CODE_ARRAY[pos] = 0;
image.setBackground(getApplication().getResources().getDrawable(R.drawable.white_red_led_btn));
image.setTag(R.id.ledState, 0);
isOn = false;
// Update the code to consider the still ON LED below the toggled one
if(pos < 20) {
DEVICE_CODE_ARRAY[pos + 5] = 1;
}
}
image.setSelected(false);
int position = (Integer) image.getTag(R.id.position);
image.setContentDescription("" + position + getLEDStatus(pos));
return isOn;
}
public UndoButton(@NonNull Context context) {
super(context);
final int undoCancelColor = Theme.getColor(Theme.key_undo_cancelColor);
final ImageView undoImageView = new ImageView(getContext());
undoImageView.setOnClickListener(v -> undo());
undoImageView.setImageResource(R.drawable.chats_undo);
undoImageView.setColorFilter(new PorterDuffColorFilter(undoCancelColor, PorterDuff.Mode.MULTIPLY));
undoImageView.setBackground(Theme.createSelectorDrawable((undoCancelColor & 0x00ffffff) | 0x19000000));
ViewHelper.setPaddingRelative(undoImageView, 0, 12, 0, 12);
addView(undoImageView, LayoutHelper.createFrameRelatively(56, 48, Gravity.CENTER_VERTICAL));
}
/**
* 创建按钮并且设置对应功能
*
* @param line
* @param sc
*/
public void createBtnAndSetFunc(LinearLayout line, ShortCut sc) {
int iconScale = DataHook.iconScale;
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
p.weight = 1;
p.gravity = Gravity.CENTER;
Context context = line.getContext();
ImageView btn = new ImageView(context);
String iconPath = sc.getIconPath();
Bitmap iconBitmap = null;
if (iconPath != null) {
iconBitmap = ImageUtil.zoomBitmap(iconPath, iconScale);
}
if (iconBitmap == null) {
iconBitmap = ImageUtil.byte2Bitmap(mMapImgRes.get(sc.getCode()));
iconBitmap = ImageUtil.zommBitmap(iconBitmap, iconScale);
}
btn.setImageBitmap(iconBitmap);
ColorStateList colorStateList = createColorStateList(0xffffffff, 0xffcccccc, 0xff0000ff, 0xffff0000);
RippleDrawable ripple = new RippleDrawable(colorStateList, null, null);
btn.setBackground(ripple);
btn.setScaleType(ImageView.ScaleType.CENTER);
btn.setOnClickListener(getBtnFuncOfName(sc));
btn.setOnLongClickListener(getBtnLongFuncOfName(sc.getCode()));
line.addView(btn, p);
}
@Override
public void initActionBar(ActionBar actionBar) {
if (actionBar == null)
return;
//You can initialize custom action bar here.
int padding = CompatResourceUtils.getDimensionPixelSize(this, R.dimen.space_12);
FrameLayout customView = new FrameLayout(this);
// customView.setPadding(padding, 0, padding, 0);
ActionBar.LayoutParams barParams = new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, WindowUtils.getActionBarSize(this));
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(customView, barParams);
//添加标题
TextView tvTitle = new TextView(this);
tvTitle.setTextColor(Color.WHITE);
tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
tvTitle.setGravity(Gravity.CENTER);
tvTitle.setText(getClass().getSimpleName().replace("Activity", ""));
customView.addView(tvTitle, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
//添加返回按钮
ImageView ivBack = new ImageView(this);
ivBack.setPadding(padding / 2, 0, padding / 2, 0);
ivBack.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
ivBack.setImageResource(R.drawable.ic_chevron_left_white_24dp);
ivBack.setBackground(WindowUtils.getSelectableItemBackgroundBorderless(this));
customView.addView(ivBack, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));
ivBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
//添加menu菜单
ActionMenuView actionMenuView = new ActionMenuView(this);
FrameLayout.LayoutParams menuParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
menuParams.gravity = Gravity.END | Gravity.CENTER_VERTICAL;
customView.addView(actionMenuView, menuParams);
String title = getIntent().getStringExtra(EXTRA_TITLE);
tvTitle.setText(TextUtils.isEmpty(title) ? getClass().getSimpleName().replace("Activity", "") : title);
}
/**
* @param target
* @param drawable 设置帧动画
*/
public static void setAnimationDrawable(ImageView target, AnimationDrawable drawable) {
target.setBackground(drawable);
}
private void initView(Context context, AttributeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs,
R.styleable.SwipeTodoView);
Drawable centerIconBg = typedArray.getDrawable(R.styleable.SwipeTodoView_tic_centerBtnBg);
int leftIconResId = typedArray.getResourceId(R.styleable.SwipeTodoView_tic_leftBtnImage, 0);
int rightIconResId = typedArray.getResourceId(R.styleable
.SwipeTodoView_tic_rightBtnImage, 0);
mShowLeftButton = (0 != leftIconResId);
mShowRightButton = (0 != rightIconResId);
int centerIconResId = typedArray.getResourceId(R.styleable
.SwipeTodoView_tic_centerBtnImage, 0);
ColorStateList defaultColorList = ColorStateList.valueOf(Color.BLUE);
ColorStateList leftColorStateList = typedArray.getColorStateList(R.styleable
.SwipeTodoView_tic_leftBtnColor);
if (null == leftColorStateList) {
leftColorStateList = defaultColorList;
}
ColorStateList rightColorStateList = typedArray.getColorStateList(R.styleable
.SwipeTodoView_tic_rightBtnColor);
if (null == rightColorStateList) {
rightColorStateList = defaultColorList;
}
ColorStateList leftBgColor = typedArray.getColorStateList(R.styleable
.SwipeTodoView_tic_leftBtnBgColor);
ColorStateList rightBgColor = typedArray.getColorStateList(R.styleable
.SwipeTodoView_tic_rightBtnBgColor);
mLeftBgDrawable = new ArcDrawable(Color.WHITE);
mLeftBgDrawable.setTintList(leftBgColor);
mLeftBgDrawable.setGravity(Gravity.LEFT);
mLeftBgDrawable.setAlpha(0);
mRightBgDrawable = new ArcDrawable(Color.WHITE);
mRightBgDrawable.setGravity(Gravity.RIGHT);
mRightBgDrawable.setTintList(rightBgColor);
mRightBgDrawable.setAlpha(0);
String content = typedArray.getString(R.styleable.SwipeTodoView_tic_content);
String subContent = typedArray.getString(R.styleable.SwipeTodoView_tic_subContent);
typedArray.recycle();
mOuterCircleIv = (ImageView) findViewById(R.id.outer_circle_iv);
mMiddleCircleIv = (ImageView) findViewById(R.id.middle_circle_iv);
mInnerCircleIv = (ImageView) findViewById(R.id.inner_circle_iv);
mContentTv = (TextView) findViewById(R.id.content_tv);
mSubContentTv = (TextView) findViewById(R.id.sub_content_tv);
mTipTv = (TextView) findViewById(R.id.tip_tv);
mCenterIv = (ImageView) findViewById(R.id.center_iv);
mLeftIv = (ImageView) findViewById(R.id.left_iv);
mRightIv = (ImageView) findViewById(R.id.right_iv);
mContentTv.setText(content);
mSubContentTv.setText(subContent);
mCenterIv.setBackground(centerIconBg);
if (centerIconResId != 0) {
mHasCenterIcon = true;
mCenterIv.setImageResource(centerIconResId);
mCenterIv.getDrawable().setAlpha(255);
}
mLeftIv.setImageResource(leftIconResId);
mLeftIv.setImageTintList(leftColorStateList);
mLeftIv.setBackground(mLeftBgDrawable);
mRightIv.setImageResource(rightIconResId);
mRightIv.setImageTintList(rightColorStateList);
mRightIv.setBackground(mRightBgDrawable);
}