Non ho lavorato su Widget, ma ho un po 'di esperienza con le dimensioni di ImageView.
Ecco il codice che uso:
public class ViewSizes {
public int width;
public int height;
public boolean isEmpty() {
boolean result = false;
if (0 >= width || 0 >= height) {
result = true;
}
return result;
}
}
Questo è solo una classe fittizia contenente i parametri di dimensione.
public static ViewSizes getSizes(View view) {
ViewSizes sizes = new ViewSizes();
sizes.width = view.getWidth();
sizes.height = view.getHeight();
if (sizes.isEmpty()) {
LayoutParams params = view.getLayoutParams();
if (null != params) {
int widthSpec = View.MeasureSpec.makeMeasureSpec(params.width, View.MeasureSpec.AT_MOST);
int heightSpec = View.MeasureSpec.makeMeasureSpec(params.height, View.MeasureSpec.AT_MOST);
view.measure(widthSpec, heightSpec);
}
sizes.width = view.getMeasuredWidth();
sizes.height = view.getMeasuredHeight();
}
return sizes;
}
Questo metodo calcola la larghezza forzando un ciclo di misura se tale non è già avvenuto.
public static boolean loadPhoto(ImageView view, String url, float aspectRatio) {
boolean processed = false;
ViewSizes sizes = ViewsUtils.getSizes(view);
if (!sizes.isEmpty()) {
int width = sizes.width - 2;
int height = sizes.height - 2;
if (ASPECT_RATIO_UNDEFINED != aspectRatio) {
if (height * aspectRatio > width) {
height = (int) (width/aspectRatio);
} else if (height * aspectRatio < width) {
width = (int) (height * aspectRatio);
}
}
// Do you bitmap processing here
processed = true;
}
return processed;
}
Questo è probabilmente inutile per voi. Do solo un esempio: ho un ImageView e un URL immagine, che dovrebbe essere parametrizzato con immagine e altezza.
public class PhotoLayoutListener implements OnGlobalLayoutListener {
private ImageView view;
private String url;
private float aspectRatio;
public PhotoLayoutListener(ImageView view, String url, float aspectRatio) {
this.view = view;
this.url = url;
this.aspectRatio = aspectRatio;
}
boolean handled = false;
@Override
public void onGlobalLayout() {
if (!handled) {
PhotoUtils.loadPhoto(view, url, aspectRatio);
handled = true;
}
ViewTreeObserver viewTreeObserver = view.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
removeLayoutListenerPre16(viewTreeObserver, this);
} else {
removeLayoutListenerPost16(viewTreeObserver, this);
}
}
}
@SuppressWarnings("deprecation")
private void removeLayoutListenerPre16(ViewTreeObserver observer, OnGlobalLayoutListener listener){
observer.removeGlobalOnLayoutListener(listener);
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void removeLayoutListenerPost16(ViewTreeObserver observer, OnGlobalLayoutListener listener){
observer.removeOnGlobalLayoutListener(listener);
}
}
Questo è solo un listener di layout: desidero elaborare il caricamento dell'immagine una volta terminata la fase di layout.
public static void setImage(ImageView view, String url, boolean forceLayoutLoading, float aspectRatio) {
if (null != view && null != url) {
final ViewTreeObserver viewTreeObserver = view.getViewTreeObserver();
if (forceLayoutLoading || !PhotoUtils.loadPhoto(view, url, aspectRatio)) {
if (viewTreeObserver.isAlive()) {
viewTreeObserver.addOnGlobalLayoutListener(new PhotoLayoutListener(view, url, aspectRatio));
}
}
}
}
Questo è il metodo che effettivamente chiamo. Gli do la vista e l'url. I metodi si occupano del caricamento: se è in grado di determinare le dimensioni della vista, inizia a caricarsi immediatamente. Altrimenti assegna semplicemente un listener di layout e avvia il processo di caricamento una volta terminato il layout.
È possibile rimuovere alcuni parametri: forceLoading/aspectRatio dovrebbe essere irrilevante. Successivamente, modificare il metodo PhotoUtils.loadPhoto
per creare la bitmap con la larghezza/altezza calcolata.
\ * facepalm \ *, un [* App Widget *] (http://developer.android.com/guide/topics/appwidgets/index.html). Due parole che, insieme, richiedono una risposta a questa domanda che non ha nulla a che fare con la risposta che otterresti a uno dei due. –
Attualmente sto avendo lo stesso problema. Se hai trovato una soluzione al problema, sarebbe fantastico se lo pubblicassi qui. – flocki