Java Code Examples for android.widget.ImageView.setBackground()

The following are Jave code examples for showing how to use setBackground() of the android.widget.ImageView class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: PaoMovie   File: GifViewUtils.java   View Source Code Vote up 7 votes
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@SuppressWarnings("deprecation")
static boolean setResource(ImageView view, boolean isSrc, int resId) {
	Resources res = view.getResources();
	if (res != null) {
		try {
			GifDrawable d = new GifDrawable(res, resId);
			if (isSrc) {
				view.setImageDrawable(d);
			} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
				view.setBackground(d);
			} else {
				view.setBackgroundDrawable(d);
			}
			return true;
		} catch (Exception ignored) {
			// ignored
		}
	}
	return false;
}
 
Example 2
Project: PaoMovie   File: GifViewUtils.java   View Source Code Vote up 7 votes
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@SuppressWarnings("deprecation")
static boolean setResource(ImageView view, boolean isSrc, int resId) {
	Resources res = view.getResources();
	if (res != null) {
		try {
			GifDrawable d = new GifDrawable(res, resId);
			if (isSrc) {
				view.setImageDrawable(d);
			} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
				view.setBackground(d);
			} else {
				view.setBackgroundDrawable(d);
			}
			return true;
		} catch (Exception ignored) {
			// ignored
		}
	}
	return false;
}
 
Example 3
Project: SuperNote   File: RvNoteFolderAdapter.java   View Source Code Vote up 7 votes
@Override
protected void convert(BaseViewHolder helper, NoteFolder item) {
    helper.setText(R.id.tv_folder_list_title, item.getFolderName())
            .setText(R.id.tv_folder_list_count, item.getNoteCount() + "");


    RelativeLayout rlItem=helper.getView(R.id.rl_folder_root);
    TextView tvTitle=helper.getView(R.id.tv_folder_list_title);
    TextView tvCount=helper.getView(R.id.tv_folder_list_count);
    ImageView ivIcon=helper.getView(R.id.iv_folder_list_ic);

    if(Constans.currentFolder == helper.getLayoutPosition()-getHeaderLayoutCount()){
        rlItem.setSelected(true);
        tvTitle.setTextColor(ThemeUtils.getColorPrimary(mContext));
        tvCount.setTextColor(ThemeUtils.getColorPrimary(mContext));
        ivIcon.setBackground(MyDrawable.getIcFolderSelectedDrawable( ThemeUtils.getColorPrimary(mContext)));
    } else {
        rlItem.setSelected(false);
        tvTitle.setTextColor(MainApplication.getContext().getResources().getColor(R.color.colorBlackAlpha87));
        tvCount.setTextColor(MainApplication.getContext().getResources().getColor(R.color.colorBlackAlpha54));
        ivIcon.setBackgroundResource(R.drawable.ic_folder_un_selected);
    }
}
 
Example 4
Project: GravityBox   File: ColorPickerPreference.java   View Source Code Vote up 7 votes
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());
}
 
Example 5
Project: LaunchEnr   File: ThemePreference.java   View Source Code Vote up 6 votes
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()));

        }
    }
 
Example 6
Project: SwipeBackController   File: SwipeBackController.java   View Source Code Vote up 6 votes
/**
 * 设置窗口根布局的背景图片,这张图将以子view的模式加到decorView中
 * <p/>
 * Sets the background image of the decorView, this drawable will be a child ImageView of decorView.
 *
 * @param decorBackgroundDrawable
 */
public SwipeBackController setDecorBackgroundDrawable(Drawable decorBackgroundDrawable) {
    mDecorBackgroundDrawable = decorBackgroundDrawable;
    final ImageView bgImageView = new ImageView(mActivity);
    ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    decorView.addView(bgImageView, 0, params);
    bgImageView.setBackground(mDecorBackgroundDrawable);
    return this;
}
 
Example 7
Project: home-assistant-android   File: Surface.java   View Source Code Vote up 6 votes
private void setHassEntities() {
    HassGroupEntity group = hassEntities.searchGroupEntity(hassGroup);

    if (group != null) {
        for (HassEntity entity : group.getEntities()) {

            View shortcutButton = getLayoutInflater().inflate(R.layout.shortcut_button, null);

            Drawable drawable = thisActivity.getDrawable(getHassIconResource(entity.getIcon()));
            drawable.setTint(getResources().getColor(R.color.white));

            ImageView childImage = (ImageView)shortcutButton.findViewById(R.id.buttonImage);
            childImage.setBackground(drawable);

            shortcutButtons.addView(shortcutButton);

            if (entity instanceof HassGroupEntity) {
                setHassButtonGroup((HassGroupEntity) entity, shortcutButton);
            }

            if (entity instanceof HassClimateEntity) {
                final View climateContainer = getLayoutInflater().inflate(R.layout.climate_big, null);
                scrollViewItems.addView(climateContainer);
                setClimateView(climateContainer, (ClimateControlInterface) entity);

                shortcutButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        scrollView.smoothScrollTo(climateContainer.getLeft(), 0);
                    }
                });
            }
        }
    } else {
        showSettings();
        setupError.setText("Group not found");
    }

}
 
Example 8
Project: Tribe   File: CountryDetailActivity.java   View Source Code Vote up 6 votes
/**
 * 初始化喜欢布局事件
 */
private void initLikeListener(){
    mLikell=(LinearLayout)findViewById(R.id.like_ll);
    mLikell.setOnClickListener(this);
    mLikeIv=(ImageView)findViewById(R.id.like_iv);
    if (mCountry.getGuan()==0){
        mLikeIv.setBackground(mDraLikeNo);
    }else {
        mLikeIv.setBackground(mDraLikePressed);
    }
}
 
Example 9
Project: CXJPadProject   File: VersionUtils.java   View Source Code Vote up 6 votes
@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
public static void setBackground(Drawable imagebakground,ImageView view){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        view.setBackground(imagebakground);
    } else {
        view.setBackgroundDrawable(imagebakground);
    }
}
 
Example 10
Project: style-transfer   File: MainActivity.java   View Source Code Vote up 6 votes
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.images, null);
    }
    image = (ImageView) convertView.findViewById(R.id.imagex);
    image.setBackground(items.get(position));
    return convertView;

}
 
Example 11
Project: microbit   File: PairingActivity.java   View Source Code Vote up 6 votes
/**
 * 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;
}
 
Example 12
Project: XposedNavigationBar   File: BtnFuncFactory.java   View Source Code Vote up 6 votes
/**
 * 创建按钮并且设置对应功能
 *
 * @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.WRAP_CONTENT);
    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, 0xffffff00, 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);
}
 
Example 13
Project: home-assistant-android   File: Surface.java   View Source Code Vote up 5 votes
private void showChildEntityView(ArrayList<LightControlInterface> lightControlInterfaces, final View childEntityContainer, View lightControlView, int color) {

        childEntityViews.add(childEntityContainer);
        childEntityContainer.setVisibility(View.VISIBLE);

        LinearLayout childEntityView = (LinearLayout)childEntityContainer.findViewById(R.id.childEntityView);
        childEntityView.removeAllViews();

        ImageButton closeButton = (ImageButton)childEntityContainer.findViewById(R.id.closeButton);
        closeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hideChildEntityView(childEntityContainer);
            }
        });
        childEntityView.removeAllViews();

        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        lp.addRule(RelativeLayout.CENTER_IN_PARENT,1);
        for (LightControlInterface lightControlInterface : lightControlInterfaces) {

            View child = getLayoutInflater().inflate(R.layout.lightcontrol_list_item_small, null);
            TextView childText = (TextView)child.findViewById(R.id.buttonText);
            childText.setText(lightControlInterface.getName());

            try {
                String icon = lightControlInterface.getIcon();
                if (icon.length() > 0 && icon.substring(0, 4).equals("mdi:")) {

                    ImageView childImage = (ImageView)child.findViewById(R.id.buttonImage);
                    Drawable iconDrawable = thisActivity.getDrawable(getHassIconResource(lightControlInterface.getIcon()));
                    iconDrawable.setTint(getResources().getColor(R.color.white));
                    childImage.setBackground(iconDrawable);
                }
            } catch (Exception e) {
                Log.e("ChildEntityView", e.getMessage());
            }

            childEntityView.addView(child);
            setLightControlButton(lightControlInterface, child, childEntityContainer, lightControlView);
        }
    }
 
Example 14
Project: home-assistant-android   File: Surface.java   View Source Code Vote up 5 votes
private void setHassButtonGroup(HassGroupEntity groupEntity, View shortcutButton) {

        final View groupContainer = getLayoutInflater().inflate(R.layout.group_list_vertical_scrollable, null);
        scrollViewItems.addView(groupContainer);
        LinearLayout groupsView = (LinearLayout)groupContainer.findViewById(R.id.groupsView);

        View childContainer = getLayoutInflater().inflate(R.layout.childentity_list_vertical_scrollable, null);
        scrollViewItems.addView(childContainer);
        childContainer.setVisibility(GONE);

        View lightControl = getLayoutInflater().inflate(R.layout.lightcontrol, null);
        scrollViewItems.addView(lightControl);
        lightControl.setVisibility(GONE);

        shortcutButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                scrollView.smoothScrollTo(groupContainer.getLeft(), 0);
            }
        });

        for (HassEntity entity : groupEntity.getEntities()) {

            View child = getLayoutInflater().inflate(R.layout.lightcontrol_list_item_medium, null);
            LinearLayout buttonLayout = (LinearLayout)child.findViewById(R.id.buttonLayout);
            Drawable background = buttonLayout.getBackground();
            background.setTint(entity.getColor());


            TextView childText = (TextView)child.findViewById(R.id.buttonText);
            childText.setText(entity.getName());

            try {
                String icon = entity.getIcon();

                Drawable drawable = thisActivity.getDrawable(getHassIconResource(icon));
                drawable.setTint(getResources().getColor(R.color.white));

                ImageView childImage = (ImageView)child.findViewById(R.id.buttonImage);
                childImage.setBackground(drawable);


            } catch (Exception e) {
                Log.e("LightControl", e.getMessage());
            }

            groupsView.addView(child);

            if (entity instanceof LightControlInterface) {
                setLightControlButton((LightControlInterface)entity, child, childContainer, lightControl);
            }

            if (entity instanceof SceneInterface) {
                setSceneButton((SceneInterface) entity, child);
            }

            if (entity instanceof SensorInterface) {
                setSensor((SensorInterface) entity, child);
            }
        }
    }
 
Example 15
Project: AstronomyTourPadova   File: ShowCategoriesActivity.java   View Source Code Vote up 5 votes
@Override
protected void onResume() {
    Log.d(TAG, "onResume()");
    super.onResume();
    
    atCategories = AtUtils.getAllCategories(getApplicationContext());
    Log.d(TAG, "CATEGORIES: " + atCategories.toString());
    
    SharedPreferences sharedpreferences = getSharedPreferences(Utils.AtSharedPreference, MODE_PRIVATE);
    
    // Populating the View
    for(Map.Entry<Integer, String> entry : dbCatConverter.entrySet()) {
        Integer categoryId = entry.getKey();
        String categoryName = entry.getValue();
        
        int rId = getResources().getIdentifier("textViewCategories" + categoryId.toString(), "id", getPackageName());
        TextView currCategory = (TextView) findViewById(rId);
        currCategory.setText(categoryName);

        // Maybe it's a selected one?
        Integer categoryIdSelected = sharedpreferences.getInt(Utils.keyCurrentCategoryId, 0); //0 is the default value.
        int rVcId = getResources().getIdentifier("imageViewCategories" + categoryId.toString(), "id", getPackageName());
        ImageView imageViewCategory = (ImageView) findViewById(rVcId);
        
        if( categoryIdSelected == categoryId ) {
            imageViewCategory.setBackground(getResources().getDrawable(R.drawable.layout_selected));
        } else {
            imageViewCategory.setBackground(getResources().getDrawable(R.drawable.layout_not_selected));
        }
    }
    
    // Constant SnackBar
    final Integer pointId = sharedpreferences.getInt(Utils.keyPointId, 0); //0 is the default value.
    final String snackText = sharedpreferences.getString(Utils.keyCurrentSnackbar, "");
    if (pointId != 0) {
        Log.d(TAG, "sharedpreferences keyPointId:" + pointId);
        localSnackbar = Utils.showSnackbar(findViewById(android.R.id.content), snackText, getString(R.string.snackbar_goto_point),
            new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    localSnackbar.dismiss();
                    Intent intent = new Intent(ShowCategoriesActivity.this, ShowPointActivity.class);
                    Bundle dataBundle = new Bundle();
                    dataBundle.putString("POINT_ID", pointId.toString());
                    intent.putExtras(dataBundle);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);
                }
            }
        );
    } else {
        if(localSnackbar != null) {
            localSnackbar.dismiss();
        }
    }
}
 
Example 16
Project: AstronomyTourPadova   File: ShowCategoriesActivity.java   View Source Code Vote up 5 votes
public void onClick(View view) {

    SharedPreferences sharedpreferences = getSharedPreferences(Utils.AtSharedPreference, MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedpreferences.edit();
    ImageView imageViewAll = (ImageView) findViewById(R.id.imageViewCategoriesAll);
    ImageView imageViewCategories4 = (ImageView) findViewById(R.id.imageViewCategories4);
    ImageView imageViewCategories5 = (ImageView) findViewById(R.id.imageViewCategories5);
    ImageView imageViewCategories6 = (ImageView) findViewById(R.id.imageViewCategories6);
    ImageView imageViewCategories7 = (ImageView) findViewById(R.id.imageViewCategories7);

    // FIXME: Ugly as hell, but hey, time's up :-(
    switch (view.getId()) {
    
        case R.id.categoriesAll:
            // All Categories
            editor.putInt(Utils.keyCurrentCategoryId, 0);
            editor.apply();
            // Graphic
            imageViewAll.setBackground(getResources().getDrawable(R.drawable.layout_selected));
            imageViewCategories4.setBackground(getResources().getDrawable(R.drawable.layout_not_selected));
            imageViewCategories5.setBackground(getResources().getDrawable(R.drawable.layout_not_selected));
            imageViewCategories6.setBackground(getResources().getDrawable(R.drawable.layout_not_selected));
            imageViewCategories7.setBackground(getResources().getDrawable(R.drawable.layout_not_selected));
            break;
    
        case R.id.categories4:
            // Cat 4
            editor.putInt(Utils.keyCurrentCategoryId, 4);
            editor.apply();
            // Graphic
            imageViewAll.setBackground(getResources().getDrawable(R.drawable.layout_not_selected));
            imageViewCategories4.setBackground(getResources().getDrawable(R.drawable.layout_selected));
            imageViewCategories5.setBackground(getResources().getDrawable(R.drawable.layout_not_selected));
            imageViewCategories6.setBackground(getResources().getDrawable(R.drawable.layout_not_selected));
            imageViewCategories7.setBackground(getResources().getDrawable(R.drawable.layout_not_selected));
            break;
    
        case R.id.categories5:
            // Cat 4
            editor.putInt(Utils.keyCurrentCategoryId, 5);
            editor.apply();
            // Graphic
            imageViewAll.setBackground(getResources().getDrawable(R.drawable.layout_not_selected));
            imageViewCategories4.setBackground(getResources().getDrawable(R.drawable.layout_not_selected));
            imageViewCategories5.setBackground(getResources().getDrawable(R.drawable.layout_selected));
            imageViewCategories6.setBackground(getResources().getDrawable(R.drawable.layout_not_selected));
            imageViewCategories7.setBackground(getResources().getDrawable(R.drawable.layout_not_selected));
            break;
    
        case R.id.categories6:
            // Cat 4
            editor.putInt(Utils.keyCurrentCategoryId, 6);
            editor.apply();
            // Graphic
            imageViewAll.setBackground(getResources().getDrawable(R.drawable.layout_not_selected));
            imageViewCategories4.setBackground(getResources().getDrawable(R.drawable.layout_not_selected));
            imageViewCategories5.setBackground(getResources().getDrawable(R.drawable.layout_not_selected));
            imageViewCategories6.setBackground(getResources().getDrawable(R.drawable.layout_selected));
            imageViewCategories7.setBackground(getResources().getDrawable(R.drawable.layout_not_selected));
            break;
    
        case R.id.categories7:
            // Cat 4
            editor.putInt(Utils.keyCurrentCategoryId, 7);
            editor.apply();
            // Graphic
            imageViewAll.setBackground(getResources().getDrawable(R.drawable.layout_not_selected));
            imageViewCategories4.setBackground(getResources().getDrawable(R.drawable.layout_not_selected));
            imageViewCategories5.setBackground(getResources().getDrawable(R.drawable.layout_not_selected));
            imageViewCategories6.setBackground(getResources().getDrawable(R.drawable.layout_not_selected));
            imageViewCategories7.setBackground(getResources().getDrawable(R.drawable.layout_selected));
            break;
    }
    
    // Send to List
    Intent intentList = new Intent(getApplicationContext(), ListPointsActivity.class);
    startActivity(intentList);
    finish();
}
 
Example 17
Project: LiveGiftLayout   File: GiftAnimationUtil.java   View Source Code Vote up 5 votes
/**
 * @param target
 * @param drawable 设置帧动画
 */
public static void setAnimationDrawable(ImageView target, AnimationDrawable drawable) {

    target.setBackground(drawable);
}
 
Example 18
Project: ViewPagerHelper   File: TransIndicator.java   View Source Code Vote up 5 votes
public void addPagerData(PageBean bean, ViewPager viewPager){
    if (bean != null){
        mCount = bean.datas.size();
        if (mCount == 0){
            return;
        }

        mOpenView = bean.openview;
        LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        params.setMargins(mLeftMargin,0,0,0);
        GradientDrawable drawable = new GradientDrawable();
        if (mTransType == TRANS_ROUND) {
            drawable.setShape(GradientDrawable.RECTANGLE);
            drawable.setSize(mTransWidht, mTransHeight);
            drawable.setCornerRadius(mRoundRadius);
        }else{
            drawable.setShape(GradientDrawable.OVAL);
            drawable.setSize(mTransWidht *2, mTransHeight*2);
        }
        drawable.setColor(mDefaultcolor);

        for (int i = 0; i < mCount; i++) {
            ImageView imageView = new ImageView(mContext);
            imageView.setBackground(drawable);
            imageView.setLayoutParams(params);
            if (i == 0){

                mFirstView = imageView;
            }
            if (i == 1){
                mSecondView = imageView;
            }

            addView(imageView);
        }
        if (viewPager != null) {
            viewPager.addOnPageChangeListener(this);
        }
    }
}