动态壁纸中心或滚动

时间:2012-07-09 22:42:41

标签: android image live-wallpaper centering horizontal-scrolling

我正在开发我的第一个Android项目,这是一个动态壁纸,我设法通过教程,示例,甚至是在这里找到的代码来完成工作,但我仍然有一个问题是不能完成它。当用户切换主屏幕时,我希望我的动态壁纸使用图像进行视差滚动。

这是我的代码::

  package com.livewallpaper.mw3lwp;


  import android.content.res.Configuration;
  import android.content.res.Resources;
  import android.graphics.Bitmap;
  import android.graphics.BitmapFactory;
  import android.graphics.Canvas;
  import android.graphics.Matrix;
  import android.graphics.Paint;
  import android.os.Handler;
  import android.service.wallpaper.WallpaperService;


  import android.view.Display;
  import android.view.MotionEvent;
  import android.view.SurfaceHolder;
  import android.view.WindowManager;

  public class ModernWarfare3LiveWallpaper extends WallpaperService {



  private final Handler mHandler = new Handler();


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

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

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

class CubeEngine extends Engine {

    private final Paint mPaint = new Paint();
    private float mPosY;
    private float mPosX;
    //private float mPosYBackup;



    private boolean mAnime = true;
    private Matrix mMatrix = new Matrix();
    public int bgcycle = 0;
    public Bitmap myBg;
    public int idx = 0;
    private float mPixels;




    private final Runnable mDrawAnim = new Runnable() {
        public void run() {
            drawFrame();
        }
    };

    private boolean mVisible;

    private static final int NUM_RES = 50;
    //private final Bitmap[] mPics = new Bitmap[NUM_RES];

    public int getScreenOrientation() {



        Display screen= ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
        int orientation = getResources().getConfiguration().orientation;


          // UNDEFINED

          if(orientation==Configuration.ORIENTATION_UNDEFINED){

              //Configuration config = getResources().getConfiguration();


              if(orientation==Configuration.ORIENTATION_UNDEFINED){
              //if height and widht of screen are equal then
              // it is square orientation
              if(screen.getWidth()==screen.getHeight()){
              orientation = Configuration.ORIENTATION_SQUARE;
              }else{ //if widht is less than height than it is portrait
              if(screen.getWidth() < screen.getHeight()){
              orientation = Configuration.ORIENTATION_PORTRAIT;
              }else{ // if it is not any of the above it will defineitly be landscape
              orientation = Configuration.ORIENTATION_LANDSCAPE;
              }
              }
              }
              }

          //

        // Query what the orientation currently really is.
          if (orientation == Configuration.ORIENTATION_PORTRAIT)                {
              // The following message is only displayed once.
               return orientation/*= 1*/; // Portrait Mode

          }else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
              // The following message is only displayed once.
               return orientation/*= 2*/;   // Landscape mode
          }


          return orientation;
         }



    public void updateBG() {

        idx += 1;
        if (idx == NUM_RES) {idx = 0;}


        Resources res = getResources();
        int id = res.getIdentifier("n" + (idx + 1), "drawable", "com.livewallpaper.mw3lwp");
        myBg = BitmapFactory.decodeResource(res, id);

    }


    CubeEngine() {


        Resources res = getResources();
        //for (int i = 0; i< NUM_RES; i++) {
            int id = res.getIdentifier("n" + (idx + 1), "drawable", "com.livewallpaper.mw3lwp");
            myBg = BitmapFactory.decodeResource(res, id);
           // if (i==NUM_RES) i=0;

       // }





    }

    @Override
    public void onCreate(SurfaceHolder surfaceHolder) {
        super.onCreate(surfaceHolder);

        setTouchEventsEnabled(false);
    }

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

    @Override
    public void onVisibilityChanged(boolean visible) {
        mVisible = visible;
        if (visible) {
            drawFrame();
        } else {
            mHandler.removeCallbacks(mDrawAnim);
        }
    }

    @Override
    public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {





        //if landscape
        if (getScreenOrientation() == 2){
        super.onSurfaceChanged(holder, format, width, height);

        float w = myBg.getWidth();
        float h = myBg.getHeight();
        float s = width / (float)w;
        mMatrix.reset();
        mMatrix.setScale(s, s);

        mPosY = (height - (h * s)) / 2f;
        //mPixels= 0;
        //mPosYBackup= mPosY;

        drawFrame();
        }
        //


        //if portrait
        else {
        super.onSurfaceChanged(holder, format, width, height);

        float w = myBg.getWidth();
        float h = myBg.getHeight();
        float s = height / (float)h;
        mMatrix.reset();
        mMatrix.setScale(s, s);
        //mMatrix.postScale(s, s, 0, 0);

       // mPosY = 0f;


        mPosX= (width - (w * s)) / 2f;


        drawFrame();
        }
        //


    }

    @Override
    public void onSurfaceCreated(SurfaceHolder holder) {
        super.onSurfaceCreated(holder);
    }

    @Override
    public void onSurfaceDestroyed(SurfaceHolder holder) {
        super.onSurfaceDestroyed(holder);
        mVisible = false;
        mHandler.removeCallbacks(mDrawAnim);
    }

    @Override
    public void onOffsetsChanged(float xOffset, float yOffset,
            float xStep, float yStep, int xPixels, int yPixels) {

        // Agregado recien          
        //if landscape
        if (getScreenOrientation() == 2){
        super.onOffsetsChanged(xOffset, yOffset, xStep, yStep, xPixels, yPixels);


        //mPosY= mPosYBackup;

        drawFrame();
        }

        //if portrait
        else{

        super.onOffsetsChanged(xOffset, yOffset, xStep, yStep, xPixels, yPixels);                   
        mPixels = xPixels;


        //mPosY=0f;

        drawFrame();
        }


        // Fin Agregado


    }


    @Override
    public void onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_MOVE) {
            mAnime = !mAnime;
        }
        super.onTouchEvent(event);
    }


    void drawFrame() {
        final SurfaceHolder holder = getSurfaceHolder();

        Canvas c = null;
        try {
            c = holder.lockCanvas();
            if (c != null) {
                // draw something
                drawAnim(c);
                //drawTouchPoint(c);
            }
        } finally {
            if (c != null) holder.unlockCanvasAndPost(c);
        }

        // Reschedule the next redraw
        mHandler.removeCallbacks(mDrawAnim);


        if (mVisible && mAnime) {
            mHandler.postDelayed(mDrawAnim, 0);
        } 
    }



    void drawAnim(Canvas c) {



        // if portrait
        if(getScreenOrientation() == 1){

        c.save();


        //if (this.isPreview()) {
        //c.translate(/*(float)*/mPosX, 0f);
        //}

         // MY PROBLEM HEREEEEE!!!! IM NOT USING BOTH

        c.translate(/*(float)*/mPosX, 0f);  

        c.translate((float)mPixels, 0f);


        updateBG();
        c.drawBitmap(myBg, mMatrix, mPaint);



        //c.drawBitmap(myBg, 0, 0, mPaint);
        if (mAnime) ++idx;
        if (idx == NUM_RES) idx = 0;

        c.restore();

        }
        //end if portrait


        // if landscape

        if(getScreenOrientation() == 2){
        c.save();

        c.translate(0, mPosY);
        updateBG();
        c.drawBitmap(myBg, mMatrix, mPaint);
        if (mAnime) ++idx;
        if (idx == NUM_RES) idx = 0;

        c.restore();
        }

        // end if landscape



        //c.drawBitmap(mPics[idx], mMatrix, mPaint);


    }



   }
  }

  }

我指出我认为错误是“我的问题在于我的问题!”在代码中。事情在canvas.translate();

如果我使用c.translate(mPosX,0f); mPosX来自onSurfacedChanged,壁纸中的图像的中心部分以我想要的方式显示,但它不会滚动整个背景。

如果我使用c.translate((float)mPixels,0f);其中mPixels来自onOffSetChanged,它只显示壁纸的左侧部分/区域。

最后,如果我将mPosX名称更改为mPixels,则c.translate((float)mPixels,0f);从fromSurfacedChanged和onOffSetChanged获取值。它不适用于第一次执行。您可以先看到壁纸居中,然后再返回仅显示壁纸的左侧部分。如果我在eclipse上再次运行它并再次在我的手机上设置它,那么它将按照我想要的方式工作,中间主屏幕上的图像的中心部分,并在切换主屏幕时滚动背景。但问题是它在第一次执行时不起作用,导致在导出为apk时无效。

所以任何人都可以帮助我在切换主屏幕时让我的动态壁纸图像显示居中和滚动。提前谢谢。

1 个答案:

答案 0 :(得分:0)

好吧,我仍然不知道为什么有时来自onOffsetChanged的xPixelOffset值(我的代码上的xPixel)返回0,但我设法为可能发生此异常的设备获得替代解决方案。 如果发生在其他人身上,我意识到xPixelOffset也可以通过获取设备屏幕宽度并将xOffset与onOffsetChanged相乘来获得。可以获取宽度并将其存储在OnSurfaceChanged的新变量中,其中可以获得设备屏幕的宽度和高度。

EX:

mPixels = (0- screenWidth)*xOffset;

mPixels应该从xPixelOffset(我的代码上是xPixel)获取值,而screenWidth是从OnSurfaceChanged获得的:

super.onSurfaceChanged(holder, format, width, height);
screenWidth= width;