android.graphics.drawable.ShapeDrawable#setColorFilter()源码实例Demo

下面列出了android.graphics.drawable.ShapeDrawable#setColorFilter() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: ImageWindow   文件: ImageWindow.java
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);
    }
 
源代码2 项目: SweetTips   文件: SweetToast.java
/**
 * 根据指定的背景色,获得当前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;
}
 
源代码3 项目: SweetTips   文件: SweetToast.java
/**
 * 根据指定的背景色,获得当前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;
}
 
源代码4 项目: Scoops   文件: FlavorRecyclerAdapter.java
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;
}
 
源代码5 项目: MaterialTabHost   文件: MaterialTabHost.java
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);
}