我正在做一个项目,所有的按钮都有类似纸张的背景。此外,按钮需要圆角和特定的边框。为了制作角落和边框,我使用了九个补丁。问题来了!以纹理为背景,排除9个贴片,9个贴片不能正确拉伸纹理。
有没有办法把九个补丁和纹理结合起来?我试图使用layer-list,但失败了。
更新。这是我的9个补丁(缩放)

这是纹理

发布于 2015-03-24 11:29:27
经过一些研究,我创建了扩展Button的自定义类。此类可以将纹理应用于相应的9修补程序按钮掩码。
public class TextureButton extends Button {
private final static String TAG = TextureButton.class.getSimpleName();
private BitmapDrawable texture;
private NinePatchDrawable ninePatchDrawable;
private Paint paint;
public TextureButton(Context context) {
super(context);
init();
}
public TextureButton(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public TextureButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pattern300);
texture = new BitmapDrawable(getResources(), bitmap);
texture.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
Bitmap bitmapMask = BitmapFactory.decodeResource(getResources(), R.drawable.btn_mask);
ninePatchDrawable = new NinePatchDrawable(getResources(), bitmapMask, bitmapMask.getNinePatchChunk(), new Rect(0, 0, 0, 0), null);
paint = new Paint();
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));
paint.setAntiAlias(true);
}
@Override
protected void onDraw(@NonNull Canvas canvas) {
super.onDraw(canvas);
canvas.saveLayer(0, 0, getWidth(), getHeight(), paint, Canvas.ALL_SAVE_FLAG);
texture.setBounds(0, 0, getWidth(), getHeight());
texture.setAlpha(255);
texture.draw(canvas);
ninePatchDrawable.setBounds(0, 0, getWidth(), getHeight());
ninePatchDrawable.draw(canvas);
canvas.restore();
}
}作为掩码,我使用带有白色和透明像素的9修补程序。
https://stackoverflow.com/questions/29126246
复制相似问题