Android Live墙纸-为什么这么慢-如何提高速度?
我正在尝试创建我的第一张实时壁纸。它工作正常,除了速度问题,一切都会很好。它减慢了桌面的速度——小部件和图标的滚动速度明显低于使用pro live墙纸时的速度(我正在三星note上测试它,所以速度应该不会有问题)。我开始认为我的做法是错误的-请看一下我的代码:Android Live墙纸-为什么这么慢-如何提高速度?,android,live-wallpaper,slowdown,Android,Live Wallpaper,Slowdown,我正在尝试创建我的第一张实时壁纸。它工作正常,除了速度问题,一切都会很好。它减慢了桌面的速度——小部件和图标的滚动速度明显低于使用pro live墙纸时的速度(我正在三星note上测试它,所以速度应该不会有问题)。我开始认为我的做法是错误的-请看一下我的代码: public class DemoWallpaperService extends WallpaperService { @Override public Engine onCreateEngine() { return ne
public class DemoWallpaperService extends WallpaperService {
@Override
public Engine onCreateEngine() {
return new DemoWallpaperEngine();
}
private class DemoWallpaperEngine extends Engine {
private boolean mVisible = false;
private final Handler mHandler = new Handler();
int x=0,y=0,a=255,i=-1, a1=255, i1=-1;
float r=0,rs=1;
float rx1=10, rxs=-1;
private Matrix mMatrix = new Matrix();
private Matrix mMatrix1 = new Matrix();
private Matrix mMatrixRotate1 = new Matrix();
private Matrix mMatrixRotate2 = new Matrix();
public Bitmap spaceShip = BitmapFactory.decodeResource(getResources(), R.drawable.spaceship);
public Bitmap background= BitmapFactory.decodeResource(getResources(), R.drawable.back2short2j);
public Bitmap wyspa= BitmapFactory.decodeResource(getResources(), R.drawable.wyspa22g);
public Bitmap ksiezyc = BitmapFactory.decodeResource(getResources(), R.drawable.ksiezyc);
public Bitmap reflektorfront= BitmapFactory.decodeResource(getResources(), R.drawable.reflektorwyspa);
private float mPixels;
private float mPixels1;
private final Runnable mUpdateDisplay = new Runnable() {
@Override
public void run() {
draw();
}};
private void draw() {
SurfaceHolder holder = getSurfaceHolder();
Canvas c = null;
Display d = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int wx= d.getWidth();
int wy= d.getHeight();
try {
Runtime.getRuntime().gc();
c = holder.lockCanvas();
c.save();
if (c != null) {
Paint paintMoon = new Paint();
if(a1<=15){
i1=1;
}
else if(a1>=255){
i1=-1;
}
a1+=5*i1;
paintMoon.setAlpha(a1);
c.translate((float)mPixels, 0f);
c.drawBitmap(background, mMatrix, null);
c.drawBitmap(ksiezyc, 1027*wx/480,15*wy/800, paintMoon);
if(rx1<=-15){
rxs=1;
}
else if(rx1>=15){
rxs=-1;
}
rx1+=rxs*0.7;
c.translate((float)mPixels1, 0f);
//reflektor wyspa back
mMatrixRotate2.setTranslate(340*wx/480,300*wy/800);
mMatrixRotate2.preRotate(rx1,reflektorfront.getWidth()/2,20);
c.drawBitmap(reflektorfront, mMatrixRotate2, null);
c.drawBitmap(wyspa, mMatrix1, null);
if(r<=-15){
rs=1;
}
else if(r>=15){
rs=-1;
}
r+=rs*0.5;
mMatrixRotate1.setTranslate(160*wx/480,380*wy/800);
mMatrixRotate1.preRotate(r,reflektorfront.getWidth()/2,20);
c.drawBitmap(reflektorfront, mMatrixRotate1, null);
if(x<c.getWidth()){
x+=3;}
else{x=0;}
if(y<c.getHeight()){
y+=3;}
else{y=0;}
Paint paint = new Paint();
if(a<=5){
i=1;
}
else if(a>=255){
i=-1;
}
a+=10*i;
paint.setAlpha(a);
c.drawBitmap(spaceShip,x,y,paint);
c.restore();
}
} finally {
if (c != null)
holder.unlockCanvasAndPost(c);
}
mHandler.removeCallbacks(mUpdateDisplay);
if (mVisible) {
mHandler.postDelayed(mUpdateDisplay, 10);
}
}
@Override
public void onOffsetsChanged(float xOffset, float yOffset,
float xStep, float yStep, int xPixels, int yPixels){
super.onOffsetsChanged(xOffset, yOffset, xStep, yStep, xPixels, yPixels);
mPixels = xPixels*7/4;
mPixels1 = 500+xPixels;
draw();
}
@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) {
super.onSurfaceChanged(holder, format, width, height);
float w = background.getWidth();
float h = background.getHeight();
float s = height / (float)h;
float z = height / (float)h;
mMatrix.reset();
mMatrix1.reset();
mMatrixRotate1.reset();
mMatrixRotate2.reset();
mMatrix.setScale(s, s);
mMatrix1.setScale(z, z);
mMatrixRotate1.setScale(s, s);
draw();
}
@Override
public void onSurfaceDestroyed(SurfaceHolder holder) {
super.onSurfaceDestroyed(holder);
mVisible = false;
mHandler.removeCallbacks(mUpdateDisplay);
}
@Override
public void onCreate(SurfaceHolder surfaceHolder) {
super.onCreate(surfaceHolder);
setTouchEventsEnabled(false);
}
@Override
public void onDestroy() {
super.onDestroy();
mVisible = false;
mHandler.removeCallbacks(mUpdateDisplay);
}
}
公共类DemoWallperService扩展了WallperService{
@凌驾
公共引擎onCreateEngine(){
返回新引擎();
}
私有类引擎扩展引擎{
私有布尔mVisible=false;
private final Handler mHandler=新处理程序();
int x=0,y=0,a=255,i=-1,a1=255,i1=-1;
浮点数r=0,rs=1;
浮点数rx1=10,rxs=-1;
私有矩阵mMatrix=新矩阵();
私有矩阵mMatrix1=新矩阵();
私有矩阵mMatrixRotate1=新矩阵();
私有矩阵mMatrixRotate2=新矩阵();
公共位图spaceShip=BitmapFactory.decodeResource(getResources(),R.drawable.spaceShip);
公共位图背景=BitmapFactory.decodeResource(getResources(),R.drawable.back2short2j);
公共位图wyspa=BitmapFactory.decodeResource(getResources(),R.drawable.wyspa22g);
公共位图ksiezyc=BitmapFactory.decodeResource(getResources(),R.drawable.ksiezyc);
公共位图reflektorfront=BitmapFactory.decodeResource(getResources(),R.drawable.reflektorwyspa);
私有浮点像素;
私有浮动像素1;
私有最终可运行mUpdateDisplay=new Runnable(){
@凌驾
公开募捐{
draw();
}};
私人提款(){
SurfaceHolder holder=getSurfaceHolder();
c=null;
显示d=((WindowManager)getSystemService(Context.WINDOW_服务)).getDefaultDisplay();
int wx=d.getWidth();
int wy=d.getHeight();
试一试{
Runtime.getRuntime().gc();
c=holder.lockCanvas();
c、 save();
如果(c!=null){
油漆月亮=新油漆();
如果(a1=255){
i1=-1;
}
a1+=5*i1;
漆面月。setAlpha(a1);
c、 平移((浮点)mPixels,0f);
c、 drawBitmap(背景、mMatrix、null);
c、 drawBitmap(ksiezyc,1027*wx/480,15*wy/800,paintMoon);
如果(rx1=15){
rxs=-1;
}
rx1+=rxs*0.7;
c、 平移((浮点)mPixels1,0f);
//反射式wyspa背部
mMatrixRotate2.setTranslate(340*wx/480300*wy/800);
mMatrixRotate2.preRotate(rx1,reflektorfront.getWidth()/2,20);
c、 drawBitmap(reflektorfront,mMatrixRotate2,null);
c、 drawBitmap(wyspa、mMatrix1、null);
如果(r=15){
rs=-1;
}
r+=rs*0.5;
mMatrixRotate1.setTranslate(160*wx/480380*wy/800);
mMatrixRotate1.preRotate(r,reflektorfront.getWidth()/2,20);
c、 drawBitmap(reflektorfront,mMatrixRotate1,null);
如果(x我不确定这是否是完整的答案…但值得尝试。与其使用bitmap.decodeResources()方法引用位图,不如尝试以下方法:
pic1 = Bitmap.createScaledBitmap(background, (int)background.getWidth(), (int)background.getHeight(), true);
我制作视差墙纸已经有一段时间了,我发现速度慢通常是因为屏幕上绘制了多少位图