我的编码点击设置有什么问题会使应用程序崩溃。我无法从 prefs.xml 读取默认设置值
图像序列像我想要的那样工作,现在它从右向左移动,而图像继续变化(如视频)
哪位高手看看有什么问题吗?:)
AndroidManifest.xml
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mywallpaper"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="12" />
<uses-feature android:name="android.software.live_wallpaper" />
<uses-permission android:name="android.permission.SET_WALLPAPER" />
<uses-permission android:name="android.permission.BIND_WALLPAPER" />
<application
android:label="@string/app_name"
android:icon="@drawable/ic_launcher"
android:theme="@style/AppTheme">
<service
android:label="@string/app_name"
android:name=".MyWallpaperService"
android:enabled="true"
android:permission="android.permission.BIND_WALLPAPER">
<intent-filter android:priority="1">
<action
android:name="android.service.wallpaper.WallpaperService">
</action>
</intent-filter>
<meta-data
android:name="android.service.wallpaper"
android:resource="@xml/wallpaper" >
</meta-data>
</service>
<activity
android:label="myPrefs"
android:name="com.example.mywallpaper.Prefs"
android:permission="android.permission.BIND_WALLPAPER"
android:enabled="true"
android:exported="true">
<intent-filter>
<category android:name="android.intent.category.PREFERENCE" />
</intent-filter>
</activity>
</application>
</manifest>
壁纸.xml
<wallpaper
xmlns:android="http://schemas.android.com/apk/res/android"
android:name="something"
android:thumbnail="@drawable/ic_launcher"
android:description="@string/app_name"
android:settingsActivity="com.example.mywallpaper.Prefs">
</wallpaper>
首选项.xml
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:title="Title Preference"
android:key="myPrefs">
<CheckBoxPreference
android:title="Touch"
android:key="touch"
android:summary="Touch enable"
android:defaultValue="true"
/>
<EditTextPreference
android:title="Speed"
android:key="speed"
android:summary="Animation speed"
android:defaultValue="500"
/>
</PreferenceScreen>
首选项.java
package com.example.mywallpaper;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceActivity;
public class Prefs extends PreferenceActivity
implements
SharedPreferences.OnSharedPreferenceChangeListener {
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getPreferenceManager().setSharedPreferencesName(MyWallpaperService.SHARED_PREFS_NAME);
addPreferencesFromResource(R.xml.prefs);
getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
@Override
protected void onResume() {
super.onResume();
}
@SuppressWarnings("deprecation")
@Override
protected void onDestroy() {
getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
super.onDestroy();
}
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
// TODO Auto-generated method stub
}
}
MyWallpaperService.java
package com.example.mywallpaper;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Handler;
import android.service.wallpaper.WallpaperService;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
public class MyWallpaperService extends WallpaperService {
public static final String SHARED_PREFS_NAME = "myPrefs";
//drawable image array
int[] myImageList = new int[]{
R.drawable.bg,
R.drawable.bg1,
R.drawable.bg2,
R.drawable.bg3,
R.drawable.bg4,
R.drawable.bg5,
R.drawable.bg6,
R.drawable.bg7,
R.drawable.bg8,
R.drawable.bg9,
R.drawable.bg10,
R.drawable.bg11,
R.drawable.bg12,
R.drawable.bg13,
R.drawable.bg14,
R.drawable.bg15,
R.drawable.bg16
};
public boolean runUp = true;
public boolean touchEnabled = true;
public int mSpeed = 100;
public int bgNumber = 0;
public int bgFrame = 0;
public int bgFrameMax = 20;
@Override
public Engine onCreateEngine() {
return new WallpaperEngine();
}
private class WallpaperEngine extends Engine
implements SharedPreferences.OnSharedPreferenceChangeListener {
private SharedPreferences mPrefs;
private boolean mVisible = false;
private Bitmap backgroundImage;
private final Handler mHandler = new Handler();
private final Runnable mUpdateDisplay = new Runnable() {
public void run() {
draw();
}
};
private void draw() {
SurfaceHolder holder = getSurfaceHolder();
Canvas c = null;
try {
c = holder.lockCanvas();
if (c != null) {
//paint black
Paint p = new Paint();
p.setColor(Color.BLACK);
c.drawRect(0, 0, c.getWidth(), c.getHeight(), p);
drawBG(c);
}
} finally {
if (c != null)
holder.unlockCanvasAndPost(c);
}
mHandler.removeCallbacks(mUpdateDisplay);
if (mVisible) {
mHandler.postDelayed(mUpdateDisplay, mSpeed);
}
}
WallpaperEngine() {
mPrefs = MyWallpaperService.this.getSharedPreferences(SHARED_PREFS_NAME, 0);
mPrefs.registerOnSharedPreferenceChangeListener(this);
onSharedPreferenceChanged(mPrefs, null);
}
@Override
public void onTouchEvent(MotionEvent event) {
//super.onTouchEvent(event);
if(touchEnabled){
bgNumber = 0;
}
}
public void drawBG(Canvas c){
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPurgeable = true;
Resources res = getResources();
backgroundImage = BitmapFactory.decodeResource(res,myImageList[bgNumber], options);
//move looping imagas (video) right to left
int CH = c.getHeight();
int CW = c.getWidth();
double BH = backgroundImage.getHeight();
double BW = backgroundImage.getWidth();
double hScale = (BH/BW);
int bgSlideX = (CW/bgFrameMax);
int wallPH = (int) (CW*hScale);
int wLeft = 0+(CW-(bgSlideX*bgFrame));
int wTop = (int) ((CH/2)-(BH/2));
int wRight = CW+(CW-(bgSlideX*bgFrame));
int wBottom = wTop+wallPH;
Rect dest = new Rect(wLeft, wTop, wRight, wBottom);
Paint paint = new Paint();
paint.setFilterBitmap(true);
c.drawBitmap(backgroundImage, null, dest, paint);
bgFrame+=1;
if (bgFrame == bgFrameMax*2){
bgFrame = 0;
}
//Loop images up and down
if (runUp = true){
bgNumber += 1;
if (bgNumber == myImageList.length){
bgNumber=0;
runUp = false;
}
}
else if(runUp = false){
bgNumber -= 1;
if (bgNumber == 0){
bgNumber+=1;
runUp = true;
}
}
backgroundImage.recycle();
}
@Override
public void onVisibilityChanged(boolean visible) {
mVisible = visible;
if (visible) {
draw();
}
else {
mHandler.removeCallbacks(mUpdateDisplay);
}
}
@Override
public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
draw();
}
@Override
public void onSurfaceDestroyed(SurfaceHolder holder) {
super.onSurfaceDestroyed(holder);
mVisible = false;
mHandler.removeCallbacks(mUpdateDisplay);
}
@Override
public void onDestroy() {
super.onDestroy();
mVisible = false;
mHandler.removeCallbacks(mUpdateDisplay);
}
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
mSpeed = Integer.valueOf(sharedPreferences.getString("speed", "5000"));
//sharedPreferences.getInt("speed", 100);
touchEnabled = sharedPreferences.getBoolean("touch", true);
//mHandler.post(mUpdateDisplay);
}
}
public void onPause(){
super.onDestroy();
}
}