自定义按钮图像不适合shapeDrawable

时间:2015-06-06 05:03:34

标签: android android-custom-view android-button

我尝试创建一个自定义按钮作为圆圈并为圆圈设置背景图像。所以我扩展了Button类并使用OvalShape设置了ShapeDrawable。并创建了一个LayerDrawable并添加了这个形状并添加了一个背景图像。但是图像不适合第一层(圆形)。如何在圆形背景上拟合图像?

mOvalDrawable = new ShapeDrawable(new OvalShape());
                    mOvalDrawable.getPaint().setColor(drawableBackgroundColor);

Drawable drawable = getResources().getDrawable(R.drawable.facebook);
drawable.setBounds(mOvalDrawable.getBounds());

//ClipDrawable clipDrawable = new ClipDrawable(drawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);

LayerDrawable layerDrawable = new LayerDrawable(new Drawable[]{drawable,mOvalDrawable});

                    if(Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN)
                    {
                        //setBackground(mOvalDrawable);
                        setBackground(layerDrawable);
                    }
                    else
                    {
                        //setBackgroundDrawable(mOvalDrawable);
                        setBackgroundDrawable(layerDrawable);
                    }

enter image description here

1 个答案:

答案 0 :(得分:0)

我建议您使用CircleImage来实现目标。您可以参考此项目in github.