FairEmail/app/src/main/java/eu/faircode/email/FixedImageButton.java

60 lines
1.9 KiB
Java

package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FairEmail is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018-2022 by Marcel Bokhorst (M66B)
*/
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageButton;
public class FixedImageButton extends AppCompatImageButton {
public FixedImageButton(@NonNull Context context) {
super(context);
}
public FixedImageButton(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public FixedImageButton(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
try {
super.onDraw(canvas);
} catch (RuntimeException ex) {
Log.e(new Throwable(Helper.getViewName(this), ex));
Drawable d = getContext().getDrawable(R.drawable.twotone_broken_image_24);
Rect b = getDrawable().getBounds();
int px = Math.min(b.width(), b.height());
d.setBounds(0, 0, px, px);
d.draw(canvas);
}
}
}