No he trabajado en Widgets, pero tengo cierta experiencia en obtener el tamaño de ImageView.
Aquí hay un código que utilizo:
public class ViewSizes {
public int width;
public int height;
public boolean isEmpty() {
boolean result = false;
if (0 >= width || 0 >= height) {
result = true;
}
return result;
}
}
Eso es sólo una clase ficticia que contiene los parámetros de tamaño.
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;
}
Este método calcula el ancho que forzará un ciclo de medida si aún no sucedió.
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;
}
Probablemente este sea inútil para usted. Doy solo un ejemplo: tengo una ImageView y url de imagen, que deben ser parametrizados con la imagen y la altura.
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);
}
}
Esto es sólo un oyente diseño - Quiero procesar la carga de imágenes una vez que la fase de diseño ha terminado.
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));
}
}
}
}
Este es el método que realmente llamo. Le doy la vista y la URL. Los métodos se encargan de la carga: si puede determinar el tamaño de la vista, comienza a cargarse inmediatamente. De lo contrario, simplemente asigna un oyente de diseño e inicia el proceso de carga una vez que el diseño finaliza.
Podría quitar algunos parámetros: forceLoading/aspectRatio no debería ser relevante para usted. Después de eso, cambie el método PhotoUtils.loadPhoto
para crear el mapa de bits con el ancho/alto que ha calculado.
\ * facepalm \ *, un [* App Widget *] (http://developer.android.com/guide/topics/appwidgets/index.html). Dos palabras que, juntas, requieren una respuesta a esta pregunta que no tiene nada que ver con la respuesta que obtendrías en ninguna de ellas. –
Actualmente estoy teniendo el mismo problema. Si ha encontrado una solución al problema, sería genial si la publicara aquí. – flocki