我正在尝试制作动态壁纸,但我遇到了一个我无法解决的错误。 即使我找不到问题的根源。 这是代码,然后我将解释这个问题:
的AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ir.cclever.fourmyword">
<uses-sdk android:minSdkVersion="7" />
<uses-feature android:name="android.software.live_wallpaper" />
<application android:icon="@drawable/icon"
android:label="@string/app_name"
android:permission="android.permission.BIND_WALLPAPER">
<service android:name=".LiveWallpaper"
android:label="@string/app_name"
android:icon="@drawable/icon">
<intent-filter>
<action android:name="android.service.wallpaper.WallpaperService" />
</intent-filter>
<meta-data android:name="android.service.wallpaper"
android:resource="@xml/livewallpaper" />
</service>
<activity android:label="@string/livewallpaper_settings"
android:name=".LiveWallpaperSettings"
android:theme="@android:style/Theme.Light.WallpaperSettings"
android:exported="true"
android:icon="@drawable/icon">
</activity>
</application>
</manifest>
LiveWallpaper.java
package ir.cclever.fourmyword;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.Handler;
import android.service.wallpaper.WallpaperService;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.WindowManager;
import android.widget.Toast;
import java.util.Random;
public class LiveWallpaper extends WallpaperService
{
public static final String SHARED_PREFS_NAME = "livewallpapersettings";
@Override
public void onCreate()
{
super.onCreate();
}
@Override
public void onDestroy()
{
super.onDestroy();
}
@Override
public Engine onCreateEngine()
{
return new TestPatternEngine();
}
class TestPatternEngine extends Engine implements
SharedPreferences.OnSharedPreferenceChangeListener {
private SharedPreferences mPreferences;
Random rand;
String firstSentence = null;
Integer firstSize = 50;
String secondSentence = null;
Integer secondSize = 50;
String thirdSentence = null;
Integer thirdSize = 50;
String forthSentence = null;
Integer forthSize = 50;
Point firstOriginal;
Point firstCurrent;
Point firstDes;
Point secondOriginal;
Point secondCurrent;
Point secondDes;
Point thirdOriginal;
Point thirdCurrent;
Point thirdDes;
Point forthOriginal;
Point forthCurrent;
Point forthDes;
Line firstLine;
Line secondLine;
Line thirdLine;
String firstColor;
String secondColor;
String thirdColor;
String forthColor;
Bitmap back = null;
Bitmap cover = null;
String whichTheme;
Boolean coverEnabled = true;
TestPatternEngine(){
mPreferences = LiveWallpaper.this.getSharedPreferences(SHARED_PREFS_NAME, 0);
mPreferences.registerOnSharedPreferenceChangeListener(this);
onSharedPreferenceChanged(mPreferences, null);
firstCurrent = new Point();
firstOriginal = new Point();
firstDes = new Point();
secondCurrent = new Point();
secondOriginal = new Point();
secondDes = new Point();
thirdCurrent = new Point();
thirdOriginal = new Point();
thirdDes = new Point();
forthCurrent = new Point();
forthOriginal = new Point();
forthDes = new Point();
firstLine = new Line();
secondLine = new Line();
thirdLine = new Line();
rand = new Random();
}
@Override
public void onCreate(SurfaceHolder surfaceHolder) {
super.onCreate(surfaceHolder);
setTouchEventsEnabled(true);
}
@Override
public void onDestroy() {
super.onDestroy();
mHandler.removeCallbacks(mDrawPattern);
}
Boolean isVisible = true;
@Override
public void onVisibilityChanged(boolean visible) {
isVisible = visible;
if (visible) {
drawFrame();
} else {
mHandler.removeCallbacks(mDrawPattern);
}
}
@Override
public void onSurfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
super.onSurfaceChanged(holder, format, width, height);
initFrameParams();
drawFrame();
//Toast.makeText(getApplicationContext(),"Surface Changed!!!",Toast.LENGTH_SHORT).show();
}
@Override
public void onSurfaceCreated(SurfaceHolder holder) {
super.onSurfaceCreated(holder);
Toast.makeText(getApplicationContext(),"surface created",Toast.LENGTH_SHORT).show();
}
@Override
public void onSurfaceDestroyed(SurfaceHolder holder) {
super.onSurfaceDestroyed(holder);
isVisible = false;
mHandler.removeCallbacks(mDrawPattern);
back = null;
}
@Override
public void onOffsetsChanged(float xOffset, float yOffset, float xStep,
float yStep, int xPixels, int yPixels) {
//drawFrame();
// Toast.makeText(getApplicationContext(),"Offset Changed!!! xOffset : " + xOffset + "yOffset : " + yOffset + "xStep : " + xStep + "yStep : " + yStep + "xPixels : " + xPixels + "yPixels : " + yPixels ,Toast.LENGTH_SHORT).show();
// System.out.println("Offset Changed!!! xOffset : " + xOffset + "yOffset : " + yOffset + "xStep : " + xStep + "yStep : " + yStep + "xPixels : " + xPixels + "yPixels : " + yPixels );
}
/*
* Store the position of the touch event so we can use it for drawing
* later
*/
float lastX = -1;
float lastY = -1;
@Override
public void onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
if (event.getAction() == MotionEvent.ACTION_UP) {
lastX = event.getX();
lastY = event.getY();
//Toast.makeText(getApplicationContext(),"Touched " + lastX + " " + lastY,Toast.LENGTH_SHORT).show();
}
// else {
// lastX = -1;
// lastY = -1;
// }
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
firstSentence = mPreferences.getString("text_one",getString(R.string.first_text_default));
secondSentence = mPreferences.getString("text_two",getString(R.string.second_text_default));
thirdSentence = mPreferences.getString("text_three",getString(R.string.third_text_default));
forthSentence = mPreferences.getString("text_four",getString(R.string.forth_text_default));
whichTheme = mPreferences.getString("theme_selector",getString(R.string.defTheme));
coverEnabled = Boolean.valueOf( mPreferences.getBoolean("cover_enabled",true));
//Toast.makeText(getApplicationContext(),((Integer)mPreferences.getInt("text_one_size",50)).toString(),Toast.LENGTH_SHORT).show();
firstSize = Integer.valueOf(mPreferences.getString("text_one_size","50"));
secondSize = Integer.valueOf(mPreferences.getString("text_two_size","50"));
thirdSize = Integer.valueOf(mPreferences.getString("text_three_size","50"));
forthSize = Integer.valueOf(mPreferences.getString("text_four_size","50"));
firstColor = mPreferences.getString("text_one_color","#ffffff");
secondColor = mPreferences.getString("text_two_color","#ffffff");
thirdColor = mPreferences.getString("text_three_color","#ffffff");
forthColor = mPreferences.getString("text_four_color","#ffffff");
back = null;
}
/*
Custom Codes
*/
private final Runnable mDrawPattern = new Runnable()
{
public void run()
{
drawFrame();
}
};
private final Handler mHandler = new Handler();
void drawFrame()
{
final SurfaceHolder holder = getSurfaceHolder();
Canvas c = null;
try
{
c = holder.lockCanvas();
if (c != null)
{
// draw something
// c.drawColor(0xff000000);
drawBack(c);
drawTextOne(c);
drawTextTwo(c);
drawTextThree(c);
drawTextFour(c);
// drawLineOne(c);
// drawLineTwo(c);
// drawLineThree(c);
}
}
finally
{
if (c != null)
holder.unlockCanvasAndPost(c);
}
mHandler.removeCallbacks(mDrawPattern);
if (isVisible)
{
mHandler.postDelayed(mDrawPattern, 1000 / 25);
}
}
class Line{
public Point leftTop;
public Point leftTopOrg;
public Point leftTopDes;
public Point rightButton;
public Point rightButtonOrg;
public Point rightButtonDes;
public Boolean dir;
public Line(){
leftTop = new Point();
leftTopOrg = new Point();
leftTopDes = new Point();
rightButton = new Point();
rightButtonDes = new Point();
rightButtonOrg = new Point();
}
}
private void drawLineOne(Canvas c) {
c.save();
Paint paint = new Paint();
paint.setColor(Color.WHITE);
c.drawRect(firstLine.leftTop.x,firstLine.leftTop.y,firstLine.rightButton.x,firstLine.rightButton.y,paint);
nextLocation(firstLine.leftTop, firstLine.leftTopOrg, firstLine.leftTopDes, firstLine.dir, 20);
nextLocation(firstLine.rightButton, firstLine.rightButtonOrg, firstLine.rightButtonDes, firstLine.dir, 20);
//Toast.makeText(getApplicationContext(),firstSentence,Toast.LENGTH_SHORT).show();
c.restore();
}
private void drawLineTwo(Canvas c) {
c.save();
Paint paint = new Paint();
paint.setColor(Color.WHITE);
c.drawRect(secondLine.leftTop.x,secondLine.leftTop.y,secondLine.rightButton.x,secondLine.rightButton.y,paint);
nextLocation(secondLine.leftTop, secondLine.leftTopOrg, secondLine.leftTopDes, secondLine.dir, 20);
nextLocation(secondLine.rightButton, secondLine.rightButtonOrg, secondLine.rightButtonDes, secondLine.dir, 20);
//Toast.makeText(getApplicationContext(),firstSentence,Toast.LENGTH_SHORT).show();
c.restore();
}
private void drawLineThree(Canvas c) {
c.save();
Paint paint = new Paint();
paint.setColor(Color.WHITE);
c.drawRect(thirdLine.leftTop.x,thirdLine.leftTop.y,thirdLine.rightButton.x,thirdLine.rightButton.y,paint);
nextLocation(thirdLine.leftTop, thirdLine.leftTopOrg, thirdLine.leftTopDes, thirdLine.dir, 20);
nextLocation(thirdLine.rightButton, thirdLine.rightButtonOrg, thirdLine.rightButtonDes, thirdLine.dir, 20);
//Toast.makeText(getApplicationContext(),firstSentence,Toast.LENGTH_SHORT).show();
c.restore();
}
private void drawBack(Canvas c) {
c.save();
if (back == null){
if (whichTheme.equals("t1")) {
back = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.p1), drawHere.width(), drawHere.height(), false);
}
else if(whichTheme.equals("t2")){
back = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.p2), drawHere.width(), drawHere.height(), false);
}
else{
back = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.p3), drawHere.width(), drawHere.height(), false);
}
}
if (cover == null){
cover = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.cover1), drawHere.width(), drawHere.height(), false);
}
c.drawBitmap(back,0,0,null);
if (coverEnabled) {
c.drawBitmap(cover, 0, 0, null);
}
c.restore();
}
private void drawTextOne(Canvas c) {
c.save();
Paint paint = new Paint();
paint.setColor(Color.parseColor(firstColor));
paint.setTextSize(firstSize);
paint.setTextAlign(Paint.Align.CENTER);
c.drawText(firstSentence,firstCurrent.x,firstCurrent.y,paint);
nextLocation(firstCurrent,firstOriginal,firstDes,firstDir, 100);
//Toast.makeText(getApplicationContext(),firstSentence,Toast.LENGTH_SHORT).show();
c.restore();
}
// from Org to Des
Boolean firstDir = true;
Boolean secondDir = true;
Boolean thirdDir = true;
Boolean forthDir = true;
private void nextLocation(Point f1, Point f2, Point f3, Boolean dir,Integer limit) { // f1 : current , f2 : Org , f3 : Destination
if (f1.x == f3.x && f1.y == f3.y && dir){
f3.x = f2.x-(limit/2)+rand.nextInt(limit);
f3.y = f2.y-(limit/2)+rand.nextInt(limit);
dir = false;
}
if (f1.x == f2.x && f1.y == f2.y && !dir){
dir = true;
}
else if (!dir){
if (f1.x > f2 .x)
f1.x--;
else if (f1.x < f2.x)
f1.x++;
if (f1.y > f2 .y)
f1.y--;
else if (f1.y < f2.y)
f1.y++;
}
else{
if (f1.x > f3 .x)
f1.x--;
else if (f1.x < f3.x)
f1.x++;
if (f1.y > f3 .y)
f1.y--;
else if (f1.y < f3.y)
f1.y++;
}
}
private void drawTextTwo(Canvas c) {
c.save();
Paint paint = new Paint();
paint.setColor(Color.parseColor(secondColor));
paint.setTextSize(secondSize);
paint.setTextAlign(Paint.Align.CENTER);
c.drawText(secondSentence,secondCurrent.x,secondCurrent.y,paint);
nextLocation(secondCurrent,secondOriginal,secondDes,secondDir, 100);
//Toast.makeText(getApplicationContext(),firstSentence,Toast.LENGTH_SHORT).show();
c.restore();
}
private void drawTextThree(Canvas c) {
c.save();
Paint paint = new Paint();
paint.setColor(Color.parseColor(thirdColor));
paint.setTextSize(thirdSize);
paint.setTextAlign(Paint.Align.CENTER);
c.drawText(thirdSentence,thirdCurrent.x,thirdCurrent.y,paint);
nextLocation(thirdCurrent,thirdOriginal,thirdDes,thirdDir, 100);
//Toast.makeText(getApplicationContext(),firstSentence,Toast.LENGTH_SHORT).show();
c.restore();
}
private void drawTextFour(Canvas c) {
c.save();
Paint paint = new Paint();
paint.setColor(Color.parseColor(forthColor));
paint.setTextSize(forthSize);
paint.setTextAlign(Paint.Align.CENTER);
c.drawText(forthSentence,forthCurrent.x,forthCurrent.y,paint);
nextLocation(forthCurrent,forthOriginal,forthDes,forthDir, 100);
//Toast.makeText(getApplicationContext(),firstSentence,Toast.LENGTH_SHORT).show();
c.restore();
}
float nextRight = 200 , nextButton = 200;
private void drawBlue(Canvas c) {
c.save();
c.drawColor(0xff000000);
Paint paint = new Paint();
paint.setColor(Color.BLUE);
c.drawRect(100, 100, nextRight, nextButton, paint);
if (nextRight > 200){
nextButton --;
}
if (nextButton > 200){
nextRight --;
}
if (lastX > 200 && lastY > 200){
nextRight = lastX;
lastX = -1;
nextButton = lastY;
lastY = -1;
}
//Toast.makeText(getApplicationContext(),"Refreshed",Toast.LENGTH_SHORT).show();
}
Rect drawHere;
Boolean isHorizontal;
void initFrameParams(){
DisplayMetrics metrics = new DisplayMetrics();
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
display.getMetrics(metrics);
drawHere = new Rect(0, 0, metrics.widthPixels, metrics.heightPixels);
int rotation = display.getOrientation();
if(rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180)
isHorizontal = false;
else
isHorizontal = true;
Toast.makeText(getApplicationContext(),"isHorizontal : " + isHorizontal.toString(),Toast.LENGTH_SHORT).show();
//System.out.println("mHorizontal "+isHorizontal);
//System.out.println("mShape "+mShape);
Integer heightPart = drawHere.height()/8;
firstOriginal.set(drawHere.width()/2,drawHere.height()/5);
firstCurrent.set(firstOriginal.x,firstOriginal.y);
firstDes.set(firstOriginal.x,firstOriginal.y);
firstLine.leftTop.set(drawHere.width()/10,firstOriginal.y+heightPart-10);
firstLine.leftTopOrg.set(firstLine.leftTop.x,firstLine.leftTop.y);
firstLine.leftTopDes.set(firstLine.leftTop.x,firstLine.leftTop.y);
firstLine.rightButton.set(9*drawHere.width()/10,firstOriginal.y+heightPart+10);
firstLine.rightButtonOrg.set(firstLine.rightButton.x,firstLine.rightButton.y);
firstLine.rightButtonDes.set(firstLine.rightButton.x,firstLine.rightButton.y);
firstLine.dir = true;
secondOriginal.set(drawHere.width()/2,2*drawHere.height()/5);
secondCurrent.set(secondOriginal.x,secondOriginal.y);
secondDes.set(secondOriginal.x,secondOriginal.y);
secondLine.leftTop.set(drawHere.width()/10,secondOriginal.y+heightPart-10);
secondLine.leftTopOrg.set(secondLine.leftTop.x,secondLine.leftTop.y);
secondLine.leftTopDes.set(secondLine.leftTop.x,secondLine.leftTop.y);
secondLine.rightButton.set(9*drawHere.width()/10,secondOriginal.y+heightPart+10);
secondLine.rightButtonOrg.set(secondLine.rightButton.x,secondLine.rightButton.y);
secondLine.rightButtonDes.set(secondLine.rightButton.x,secondLine.rightButton.y);
secondLine.dir = true;
thirdOriginal.set(drawHere.width()/2,3*drawHere.height()/5);
thirdCurrent.set(thirdOriginal.x,thirdOriginal.y);
thirdDes.set(thirdOriginal.x,thirdOriginal.y);
thirdLine.leftTop.set(drawHere.width()/10,thirdOriginal.y+heightPart-10);
thirdLine.leftTopOrg.set(thirdLine.leftTop.x,thirdLine.leftTop.y);
thirdLine.leftTopDes.set(thirdLine.leftTop.x,thirdLine.leftTop.y);
thirdLine.rightButton.set(9*drawHere.width()/10,thirdOriginal.y+heightPart+10);
thirdLine.rightButtonOrg.set(thirdLine.rightButton.x,thirdLine.rightButton.y);
thirdLine.rightButtonDes.set(thirdLine.rightButton.x,thirdLine.rightButton.y);
thirdLine.dir = true;
forthOriginal.set(drawHere.width()/2,4*drawHere.height()/5);
forthCurrent.set(forthOriginal.x,forthOriginal.y);
forthDes.set(forthOriginal.x,forthOriginal.y);
}
}
}
LiveWallpaperSettings.java
package ir.cclever.fourmyword;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceActivity;
public class LiveWallpaperSettings extends PreferenceActivity
implements SharedPreferences.OnSharedPreferenceChangeListener
{
@Override
protected void onCreate(Bundle icicle)
{
super.onCreate(icicle);
getPreferenceManager().setSharedPreferencesName(LiveWallpaper.SHARED_PREFS_NAME);
addPreferencesFromResource(R.xml.livewallpaper_settings);
getPreferenceManager().getSharedPreferences()
.registerOnSharedPreferenceChangeListener(this);
}
@Override
protected void onResume()
{
super.onResume();
}
@Override
protected void onDestroy()
{
getPreferenceManager().getSharedPreferences()
.unregisterOnSharedPreferenceChangeListener(this);
super.onDestroy();
}
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key)
{
}
}
故事如下: 我正在研究一个动态壁纸的例子,每件事都很好。当我决定发布我的程序时,我通过在android studio中重构更改了包名,然后重命名目录中的文件夹,然后根据需要编辑文件!我的意思是每个文件顶部的包定义。
现在壁纸预览还可以,但是当我想进入设置页面或设置壁纸时,我看到了一个错误。
我知道这件事对你来说不清楚,但如果你有任何想法,我想要它。 谢谢。