Java Code Examples for android.graphics.drawable.ShapeDrawable#setColorFilter()

The following examples show how to use android.graphics.drawable.ShapeDrawable#setColorFilter() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ImageWindow.java    From ImageWindow with Apache License 2.0 6 votes vote down vote up
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);
    }
 
Example 2
Source File: SweetToast.java    From SweetTips with Apache License 2.0 5 votes vote down vote up
/**
 * 根据指定的背景色,获得当前SweetToast实例中mContentView的背景drawable实例
 * @param backgroundColor
 * @return
 */
private static Drawable getBackgroundDrawable(SweetToast sweetToast, @ColorInt int backgroundColor){
    try {
        ShapeDrawable shapeDrawable = new ShapeDrawable();
        //获取当前设备的屏幕尺寸
        //实验发现不同的设备上面,Toast内容区域的padding值并不相同,根据屏幕的宽度分别进行处理,尽量接近设备原生Toast的体验
        int widthPixels = sweetToast.getContentView().getResources().getDisplayMetrics().widthPixels;
        int heightPixels = sweetToast.getContentView().getResources().getDisplayMetrics().heightPixels;
        float density = sweetToast.getContentView().getResources().getDisplayMetrics().density;
        if(widthPixels >= 1070){
            //例如小米5S:1920 x 1080
            shapeDrawable.setPadding((int)(density*13),(int)(density*12),(int)(density*13),(int)(density*12));
        }else {
            //例如红米2:1280x720
            shapeDrawable.setPadding((int)(density*14),(int)(density*13),(int)(density*14),(int)(density*13));
        }
        float radius = density*8;
        float[] outerRadii = new float[]{radius,radius,radius,radius,radius,radius,radius,radius};
        int width = sweetToast.getContentView().getWidth();
        int height = sweetToast.getContentView().getHeight();
        RectF rectF = new RectF(1,1,width-1,height-1);
        RoundRectShape roundRectShape = new RoundRectShape(outerRadii,rectF,null);
        shapeDrawable.setShape(roundRectShape);
        //在Android 5.0以下,直接设置 DrawableCompat.setTint 未变色:DrawableCompat.setTint(shapeDrawable,backgroundColor);

        //解决:不使用DrawableCompat,直接使用 Drawable.setColorFilter(backgroundColor, PorterDuff.Mode.SRC_ATOP),
        //经测试,在4.22(山寨机)和5.1(中兴)和6.0.1(小米5s)上颜色正常显示
        shapeDrawable.setColorFilter(backgroundColor, PorterDuff.Mode.SRC_ATOP);
        return shapeDrawable;
    }catch (Exception e){
        Log.e("幻海流心","e:"+e.getLocalizedMessage());
    }
    return null;
}
 
Example 3
Source File: SweetToast.java    From SweetTips with Apache License 2.0 5 votes vote down vote up
/**
 * 根据指定的背景色,获得当前SweetToast实例中mContentView的背景drawable实例
 * @param backgroundColor
 * @return
 */
private static Drawable getBackgroundDrawable(SweetToast sweetToast, @ColorInt int backgroundColor){
    try {
        ShapeDrawable shapeDrawable = new ShapeDrawable();
        //获取当前设备的屏幕尺寸
        //实验发现不同的设备上面,Toast内容区域的padding值并不相同,根据屏幕的宽度分别进行处理,尽量接近设备原生Toast的体验
        int widthPixels = sweetToast.getContentView().getResources().getDisplayMetrics().widthPixels;
        int heightPixels = sweetToast.getContentView().getResources().getDisplayMetrics().heightPixels;
        float density = sweetToast.getContentView().getResources().getDisplayMetrics().density;
        if(widthPixels >= 1070){
            //例如小米5S:1920 x 1080
            shapeDrawable.setPadding((int)(density*13),(int)(density*12),(int)(density*13),(int)(density*12));
        }else {
            //例如红米2:1280x720
            shapeDrawable.setPadding((int)(density*14),(int)(density*13),(int)(density*14),(int)(density*13));
        }
        float radius = density*8;
        float[] outerRadii = new float[]{radius,radius,radius,radius,radius,radius,radius,radius};
        int width = sweetToast.getContentView().getWidth();
        int height = sweetToast.getContentView().getHeight();
        RectF rectF = new RectF(1,1,width-1,height-1);
        RoundRectShape roundRectShape = new RoundRectShape(outerRadii,rectF,null);
        shapeDrawable.setShape(roundRectShape);
        //在Android 5.0以下,直接设置 DrawableCompat.setTint 未变色:DrawableCompat.setTint(shapeDrawable,backgroundColor);

        //解决:不使用DrawableCompat,直接使用 Drawable.setColorFilter(backgroundColor, PorterDuff.Mode.SRC_ATOP),
        //经测试,在4.22(山寨机)和5.1(中兴)和6.0.1(小米5s)上颜色正常显示
        shapeDrawable.setColorFilter(backgroundColor, PorterDuff.Mode.SRC_ATOP);
        return shapeDrawable;
    }catch (Exception e){
        Log.e("幻海流心","e:"+e.getLocalizedMessage());
    }
    return null;
}
 
Example 4
Source File: FlavorRecyclerAdapter.java    From Scoops with Apache License 2.0 5 votes vote down vote up
ShapeDrawable generateDrawable(@ColorInt int color){
    ShapeDrawable d = new ShapeDrawable(new OvalShape());
    d.setIntrinsicWidth(Utils.dipToPx(itemView.getContext(), 24));
    d.setIntrinsicHeight(Utils.dipToPx(itemView.getContext(), 24));
    d.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
    return d;
}
 
Example 5
Source File: MaterialTabHost.java    From MaterialTabHost with Apache License 2.0 4 votes vote down vote up
public MaterialTabHost(Context context, AttributeSet attrs) {
    super(context, attrs);

    inflater = LayoutInflater.from(context);

    TypedValue outValue = new TypedValue();
    Resources.Theme theme = context.getTheme();

    // use ?attr/colorPrimary as background color
    theme.resolveAttribute(R.attr.colorPrimary, outValue, true);
    setBackgroundColor(outValue.data);

    // use ?attr/colorControlActivated as default indicator color
    theme.resolveAttribute(R.attr.colorControlActivated, outValue, true);
    colorControlActivated = outValue.data;

    TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MaterialTabHost, 0, 0);
    int indicatorColor = a.getColor(R.styleable.MaterialTabHost_colorTabIndicator, colorControlActivated);
    a.recycle();

    // ColorDrawable on 2.x does not use getBounds() so use ShapeDrawable
    indicator = new ShapeDrawable();
    indicator.setColorFilter(indicatorColor, PorterDuff.Mode.SRC_ATOP);

    Resources res = context.getResources();
    indicatorHeight = res.getDimensionPixelSize(R.dimen.mth_tab_indicator_height);
    leftOffset = res.getDimensionPixelSize(R.dimen.mth_tab_left_offset);
    int tabHeight = res.getDimensionPixelSize(R.dimen.mth_tab_height);

    tabWidget = new TabWidget(context);
    tabWidget.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, tabHeight));
    tabWidget.setId(android.R.id.tabs);
    tabWidget.setStripEnabled(false);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        tabWidget.setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
    }
    addView(tabWidget);

    FrameLayout fl = new FrameLayout(context);
    fl.setLayoutParams(new LayoutParams(0, 0));
    fl.setId(android.R.id.tabcontent);
    addView(fl);

    setup();

    setOnTabChangedListener(new TabHost.OnTabChangeListener() {
        @Override
        public void onTabChanged(String tabId) {
            if (listener != null) {
                listener.onTabSelected(Integer.valueOf(tabId));
            }
        }
    });

    float density = getResources().getDisplayMetrics().density;

    // set elevation for App bar
    // http://www.google.com/design/spec/what-is-material/objects-in-3d-space.html#objects-in-3d-space-elevation
    ViewCompat.setElevation(this, APP_TAB_ELEVATION * density);
}