Skip to content

Instantly share code, notes, and snippets.

@Muraveiko
Last active March 21, 2024 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Muraveiko/33a30d5b6605783c3116b03af420c606 to your computer and use it in GitHub Desktop.
Save Muraveiko/33a30d5b6605783c3116b03af420c606 to your computer and use it in GitHub Desktop.
Custom size edit
import android.print.PrintAttributes;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.MutableLiveData;
import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textview.MaterialTextView;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
public class ImgSizeEdit {
public interface onChangeListener{
void onChange(PrintAttributes.MediaSize newValue);
}
public static LinkedHashMap<String,String> taxonomy;
static {
taxonomy = new LinkedHashMap<>();
taxonomy.put("mils","Mils");
taxonomy.put("in","Inches");
taxonomy.put("mm","mm");
taxonomy.put("cm","cm");
}
private int widthPx = 0;
private int heightPx = 0;
private int rotation = 0;
private int mode = 0;
private View view;
private TextInputEditText editWidth;
private TextInputEditText editHeight;
private String taxonomyValue = "cm";
private LinkedHashMapAdapter<String,String> adapter;
private MaterialTextView textView;
private final MutableLiveData<String> info = new MutableLiveData<>("");
private boolean isInit = false;
private onChangeListener callback = null;
public void setSourceSize(int w, int h){
if(isInit)return;
isInit = true;
_setSourceSize(w,h);
}
private void _showInfo(){
info.setValue(String.format("%s %s x %s px, %s°",
mode == 0 ?"Crop":"Fit",
widthPx,
heightPx,
rotation));
}
private void _setSourceSize(int w, int h){
widthPx = w;
heightPx = h;
_showInfo();
}
public void bind(View v, LifecycleOwner owner, onChangeListener callback){
view = v;
this.callback = callback;
Spinner spinner = v.findViewById(android.R.id.toggle);
textView = v.findViewById(android.R.id.summary);
editWidth = v.findViewById(android.R.id.text1);
editHeight = v.findViewById(android.R.id.text2);
adapter = new LinkedHashMapAdapter<>(view.getContext(),
android.R.layout.simple_spinner_item,
taxonomy
);
adapter.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Map.Entry<String,String> entity = adapter.getItem(position);
setTaxonomyValue(entity.getKey());
notifyCallback(true);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
spinner.setSelection(adapter.getPosition(taxonomyValue));
editHeight.setOnFocusChangeListener((v1, hasFocus) -> {
if(!hasFocus){
notifyCallback(false);
}
});
editWidth.setOnFocusChangeListener((v1, hasFocus) -> {
if(!hasFocus){
notifyCallback(false);
}
});
info.observe(owner, s -> textView.setText(s));
}
public void setTaxonomyValue(String v) {
taxonomyValue = v;
}
public void setRotation(int rotation) {
int oldRotation = this.rotation;
this.rotation = rotation;
int a = (rotation/90)%2;
int b = (oldRotation/90)%2;
if( a == b ) {
_showInfo();
return;
}
_setSourceSize(heightPx,widthPx);
}
public void setMode(int mode) {
this.mode = mode;
_showInfo();
}
public void setVisible(int visible){
view.setVisibility(visible);
}
public int getWidthMils(){
int w;
try {
Float v = Float.parseFloat(Objects.requireNonNull(editWidth.getText()).toString());
w = calcMils(v);
}catch (Exception e){
w = 0;
}
return Math.max(w, 500);
}
public int getHeightMils(){
int h;
try {
Float v = Float.parseFloat(Objects.requireNonNull(editHeight.getText()).toString());
h = calcMils(v);
}catch (Exception e){
h =0;
}
return Math.max(h,500);
}
public PrintAttributes.MediaSize getMediaSize(){
return new PrintAttributes.MediaSize("custom","Custom size",getWidthMils(),getHeightMils());
}
private int calcMils(Float v){
switch (taxonomyValue){
case "in":
return Math.round(v*1000);
case "cm":
return Math.round(v*(float)393.7);
case "mm":
return Math.round(v*(float)39.37);
}
return Math.round(v);
}
private int widthMils = 0;
private int heightMils = 0;
void notifyCallback(boolean force){
int w = getWidthMils();
int h = getHeightMils();
if(force || w!=widthMils || h!=heightMils){
widthMils = w;
heightMils = h;
callback.onChange(getMediaSize());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment