Domanda

Could you someone show me how to create an if - else statements with two sets of R.drawable.images pirates?

I've tried, but I got an error code saying pirates is un-resolved.

Bitmap icon = BitmapFactory.decodeResource(getResources(),pirates[i]);

This is what I have in the preferences:

xmlns:android="http://schemas.android.com/apk/res/android"
android:key="wallpaper_settings"
android:title="@string/wallpaper_settings" >  <!--must be the same callout from 
values->strings-->

<ListPreference               
    android:key="speed"
    android:title="@string/speed_title"
    android:entries="@array/timelist"
    android:entryValues="@array/actualtime"
    android:summary="@string/speed_summary" >              
</ListPreference>

  <CheckBoxPreference
    android:key="checkbox"
    android:title="Nature Pictures"
    android:summary="For Nature Pictures Option"
    android:defaultValue="false" />    

Main Activity:

     public class main_activity extends WallpaperService {

     public static final String SHARED_PREFS_NAME="settings_menu";


   @Override
  public void onCreate() {
    super.onCreate();
}

   @Override
public void onDestroy() {
    super.onDestroy();
}

   @Override
public Engine onCreateEngine() {
    return new wallpaperEngine();   
}

class wallpaperEngine extends Engine implements SharedPreferences.OnSharedPreferenceChangeListener{


    private final Handler mhandler = new Handler();


    //defines variables


    private final Runnable drawrunnable = new Runnable() 
    {
        public void run() {
            drawFrame();
        }
    };
    private int i = 0;
    private boolean mVisible;
    private SharedPreferences mPrefs;
    private boolean mcheckbox = false;
    public int mSpeed = 10;

         int[] pirates = {
            R.drawable.image_1,
            R.drawable.image_2,
            R.drawable.image_3,
            R.drawable.image_4

    };

       //For Nature Pictures
         int[] pirates = {
            R.drawable.image_a1,
            R.drawable.image_a2,
            R.drawable.image_a3,
            R.drawable.image_a4

    };

  .......
  ......

   Bitmap icon = BitmapFactory.decodeResource(getResources(),pirates[i]);

        i++;

        if (i == 14) {
            i = 0;

Thank you very much

È stato utile?

Soluzione

Seems like you are trying to create two arrays of int, holding different set of images. But you are using the same variable name pirates. I would suggest you define two different variables int[] pirates and int[] piratesNatural and then switch between those two arrays when you need one and the other.

Bitmap icon; 
if(natural)  
  icon = BitmapFactory.decodeResource(getResources(),piratesNatural[i]);   
else
  icon = BitmapFactory.decodeResource(getResources(),pirates[i]);

Altri suggerimenti

you doing animation?

int frame=0;
int frametime=24;

Bitmap icon1 BitmapFactory.decodeResource(getResources(), R.drawable.image_1,);
Bitmap icon2 BitmapFactory.decodeResource(getResources(), R.drawable.image_2,);
Bitmap icon3 BitmapFactory.decodeResource(getResources(), R.drawable.image_3,);
Bitmap icon4 BitmapFactory.decodeResource(getResources(), R.drawable.image_4,);



//in update
frametime--;
if(frametime<=0){
frame++;
frametime=24;
}
if(frame>3){
frame=0;
}



//in onDraw

switch(frame){

case 0:
canvas.drawBitmap(icon1,x,y,null);
break;
case 1:
canvas.drawBitmap(icon2,x,y,null);
break;
case 2:
canvas.drawBitmap(icon3,x,y,null);
break;
case 3:
canvas.drawBitmap(icon4,x,y,null);
break;
default:
canvas.drawBitmap(icon1,x,y,null);
break;}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top