你能否告诉我如何使用两套R.drawable.images盗版创建一个if-else语句?
我已尝试过,但我收到错误代码,说 盗版 未解析。
Bitmap icon = BitmapFactory.decodeResource(getResources(), pirates [i]);
这就是我在首选项中的含义:
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" />
主要活动:
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;
非常感谢
答案 0 :(得分:1)
好像你正在尝试创建两个int数组,持有不同的图像集。但是您使用的是相同的变量名pirates
。我建议您定义两个不同的变量int[] pirates
和int[] piratesNatural
,然后在需要这两个数组时在这两个数组之间切换。
Bitmap icon;
if(natural)
icon = BitmapFactory.decodeResource(getResources(),piratesNatural[i]);
else
icon = BitmapFactory.decodeResource(getResources(),pirates[i]);
答案 1 :(得分:0)
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;}