He usado el Cargando mapas de bits de manera eficiente de la Guía del desarrollador y siguió las instrucciones punto a punto de ese tutorial.
Como ya sabrás, este es el código que utilicé:
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth,int reqHeight){
//Raw height and width of the Image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height>reqHeight || width>reqWidth) {
final int heightratio = Math.round((float)height / (float)reqHeight);
final int widthRatio = Math.round((float)width / (float)reqWidth);
inSampleSize = heightratio < widthRatio ? heightratio : widthRatio;
}
return inSampleSize;
}
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,int reqWidth,int reqHeight){
//first decode with inJustdecodeBounds = true to check dimensions.
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
//Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
//Decode bitmap with inSampleSize
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
Y estoy llamando a este método en un caso de cambio como:
handler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
learn_full_iv.setVisibility(View.VISIBLE);
learn_full_iv.startAnimation(anim);
switch (id) {
case R.id.a:
//learn_full_iv.setImageResource(R.drawable.aeroplane);
learn_full_iv.setImageBitmap(decodeSampledBitmapFromResource(
getResources(), R.drawable.aeroplane, learn_full_iv.getWidth(), learn_full_iv.getWidth()));
Toast.makeText(getApplicationContext(), "Width : " + learn_full_iv.getWidth() + "Height : " + learn_full_iv.getHeight(), Toast.LENGTH_SHORT).show();
playSound(1, 2);
break;
case R.id.b:
//learn_full_iv.setImageResource(R.drawable.ball);
learn_full_iv.setImageBitmap(decodeSampledBitmapFromResource(
getResources(), R.drawable.ball, learn_full_iv.getWidth(), learn_full_iv.getWidth()));
Toast.makeText(getApplicationContext(), "Width : " + learn_full_iv.getWidth() + "Height : " + learn_full_iv.getHeight(), Toast.LENGTH_SHORT).show();
playSound(3, 4);
break;
case R.id.c:
//learn_full_iv.setImageResource(R.drawable.camel);
learn_full_iv.setImageBitmap(decodeSampledBitmapFromResource(
getResources(), R.drawable.camel, learn_full_iv.getWidth(), learn_full_iv.getWidth()));
Toast.makeText(getApplicationContext(), "Width : " + learn_full_iv.getWidth() + "Height : " + learn_full_iv.getHeight(), Toast.LENGTH_SHORT).show();
playSound(5, 6);
break;
case R.id.d:
//learn_full_iv.setImageResource(R.drawable.drum);
learn_full_iv.setImageBitmap(decodeSampledBitmapFromResource(
getResources(), R.drawable.drum, learn_full_iv.getWidth(), learn_full_iv.getWidth()));
Toast.makeText(getApplicationContext(), "Width : " + learn_full_iv.getWidth() + "Height : " + learn_full_iv.getHeight(), Toast.LENGTH_SHORT).show();
playSound(7, 8);
break;
default:
break;
}
Y ahora el problema es que cada vez que se carga el onCreate (), la imagen que se hace clic por primera vez mostrará solo el color sólido de la imagen y no toda la imagen, como se muestra a continuación:
Y desde el siguiente clic, ya sea en la misma imagen o en la siguiente, el código funciona bien como:
Y todo lo demás funciona igual de bien hasta que se reinicie la Actividad. Si reiniciamos o reanudamos esta Actividad, está sucediendo lo mismo.
¿Entonces, qué va mal?
Respuestas
2 para la respuesta № 1Simplemente lo resolví y lo descubrí yo mismo, en realidad es un pequeño vistazo para la carga de imágenes de Android:
@Override
public void run() {
// TODO Auto-generated method stub
learn_full_iv.setVisibility(View.VISIBLE);
learn_full_iv.startAnimation(anim);
switch (id) {
case R.id.a:
//learn_full_iv.setImageResource(R.drawable.aeroplane);
learn_full_iv.setImageBitmap(decodeSampledBitmapFromResource(
getResources(), R.drawable.aeroplane, learn_full_iv.getWidth(), learn_full_iv.getWidth()));
Toast.makeText(getApplicationContext(), "Width : " + learn_full_iv.getWidth() + "Height : " + learn_full_iv.getHeight(), Toast.LENGTH_SHORT).show();
playSound(1, 2);
break;
...........
El problema en el código anterior es que solo soyhabilitar ImageView "learn_full_iv" justo al comienzo del método de ejecución, que tendrá el ancho y la altura como "0" de forma predeterminada, ya que la imagen todavía no está configurada en ImageView
y si llamamos:
learn_full_iv.setImageBitmap(decodeSampledBitmapFromResource(
getResources(), R.drawable.aeroplane, learn_full_iv.getWidth(), learn_full_iv.getWidth()));
El ancho y la altura serán cero en este caso, por lo que solo se mostrará un color sólido de esa imagen. método para obtener el ancho y alto de la pantalla como valores enteros y pasarlos a las secciones de ancho y alto correspondientes en el método BODMAP DECODE anterior.
Eso es todo, ya que ahora tenemos el ancho y la altura como valores, el mapa de bits se mostrará ahora.
¡¡¡Simple!!!
0 para la respuesta № 2
Por favor aclarar:
¿por qué llamas al bacalao del interruptor en runnable?
¿Y a dónde llamas al controlador para que se ejecute?
Asegúrese de no llamar al método de inicio del controlador en el método onresume / onstart, también asegúrese de que no se vuelva a llamar a oncreate en caso de cambio de orientación