我尝试创建一个自定义按钮作为圆圈并为圆圈设置背景图像。所以我扩展了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);
}