Android live墙纸缩放

Android live墙纸缩放,android,scaling,Android,Scaling,我正在学习android和java,并试图学习如何制作实时壁纸 我在使用水族馆教程,我把它放在一个文件中,而不是分散它,但我试图做的是为所有东西都得到一个比例数,我想得到屏幕高度除以背景图像高度(总是更大),这应该给我一个比例来缩放所有东西,我在surfaceView代码中尝试过这个,它工作得非常完美,我在LiveWallper和nada中尝试过。 在surfaceView测试中,我使用了onSizeChange,通过这种方式获得了高度,它没有任何问题。 这就是我为LiveWallper所做的

我正在学习android和java,并试图学习如何制作实时壁纸 我在使用水族馆教程,我把它放在一个文件中,而不是分散它,但我试图做的是为所有东西都得到一个比例数,我想得到屏幕高度除以背景图像高度(总是更大),这应该给我一个比例来缩放所有东西,我在surfaceView代码中尝试过这个,它工作得非常完美,我在LiveWallper和nada中尝试过。 在surfaceView测试中,我使用了onSizeChange,通过这种方式获得了高度,它没有任何问题。 这就是我为LiveWallper所做的 这是我为onSurfaceChange所做的更改

public int screenWidth;
public float rescaler;
public float totalHeight = theTemplate._backgroundImage.getHeight();

@Override
public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    //add rescale and width
    screenWidth = width;
    rescaler = (float) (height / totalHeight);
    super.onSurfaceChanged(holder, format, width, height);          
}
背景图像来自名为TheTemplate的内部类 这就是我在那里做的 变量

private SpriteAnimationActivityEngine theTest;
private Bitmap theBackgroundImage;
private float theScaler = theTest.rescaler;
然后我试着用标尺重新缩放它

public void initialize(Context context, SurfaceHolder surfaceHolder) {
    this.spriteThread = new SpriteThread(this); 
    this._surfaceHolder = surfaceHolder;        
    this._sprites = new ArrayList<Renderable>();
    this._context = context;
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPurgeable = true;
    this._backgroundImage = BitmapFactory.decodeResource(context.getResources(),ca.samsstuff.testonepagewallpaper.R.drawable.sky, options);
    this.theBackgroundImage = Bitmap.createScaledBitmap(_backgroundImage,  (int) (theScaler * _backgroundImage.getWidth()),  (int) (theScaler * _backgroundImage.getHeight()), true);
    this.addSprites();
}
哪一个发送到绘图

protected void onDraw(Canvas canvas) {
    this.renderBackGround(canvas);              
    for (Renderable renderable : this._sprites) {
        renderable.render(canvas);
    }
}
我不断地犯错误,不知道自己做错了什么。就像我说的,我正在学习android和java,但是这个方法在另一个测试中起了作用,我在onSizeChange上获得了屏幕高度。我可以使用onSurfaceChange获得高度吗

任何帮助都将不胜感激 提前谢谢 萨姆

编辑 我还尝试在theTemplate类中重新缩放,只是为了看看它是否可以在自己的类中处理所有内容,但仍然存在问题,这次我使用DisplayMetrics来获取屏幕高度。 如果我能让它正常运行,这可能会起作用。 这是一次尝试

public class TheTemplate {

        private SpriteThread spriteThread;  
        private SurfaceHolder _surfaceHolder;   
        private ArrayList<Renderable> _sprites; 
        public Bitmap _backgroundImage = BitmapFactory.decodeResource(getResources(), R.drawable.sky);;
        private Context _context;

        // add rescale stuff
        private Bitmap theBackgroundImage;
        private float theScaler = initFrameParams() / _backgroundImage.getHeight();
        private Bitmap oneBackImage = Bitmap.createScaledBitmap(_backgroundImage,  (int) (theScaler * _backgroundImage.getWidth()),  (int) (theScaler * _backgroundImage.getHeight()), true);

        int initFrameParams()
        {
                            //get the screen height to use to rescale everything
            DisplayMetrics metrics = new DisplayMetrics();
            Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
            display.getMetrics(metrics);

            int screenHeight = display.getHeight();
            return screenHeight;


        }


        public void render(){
            Canvas canvas = null;
            try{

                canvas = this._surfaceHolder.lockCanvas(null);
                synchronized (this._surfaceHolder) {
                    this.onDraw(canvas);
                }

            }finally{
                if(canvas != null){
                    this._surfaceHolder.unlockCanvasAndPost(canvas);
                }
            }   
        }

        protected void onDraw(Canvas canvas) {
            this.renderBackGround(canvas);              
            for (Renderable renderable : this._sprites) {
                renderable.render(canvas);
            }
        };

        public void start(){
            this.spriteThread.switchOn();
        }

        public void stop(){
            boolean retry = true;
            this.spriteThread.switchOff();
            while (retry) {
                try {
                 this.spriteThread.join();
                    retry = false;
                } catch (InterruptedException e) {
                    // we will try it again and again...
                }
            }
        }

        public int getLeft() {      
            return 0;
        }


        public int getRight() {
            return this.theBackgroundImage.getWidth();
        }

        public void initialize(Context context, SurfaceHolder surfaceHolder) {
            this.spriteThread = new SpriteThread(this); 
            this._surfaceHolder = surfaceHolder;        
            this._sprites = new ArrayList<Renderable>();
            this._context = context;
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPurgeable = true;
            this.theBackgroundImage = oneBackImage;
            this.addSprites();
        }


        private void addSprites() {     
            Point startPoint = new Point(100, 100);
            this._sprites.add(new SpriteOne(this._context, this, startPoint, 90));
            Point startPoint1 = new Point(100, 300);
            this._sprites.add(new SpriteOne(this._context, this, startPoint1, 50));

            Point startPoint2 = new Point(200, 200);
            this._sprites.add(new SpriteOne(this._context, this, startPoint2, 15));
        }

        private void renderBackGround(Canvas canvas)
        {
            canvas.drawBitmap(this.theBackgroundImage, 0, 0, null);
        }
    }
公共类模板{
私有spritehread spritehread;
私人表面股东(SurfaceHolder);;
私人ArrayList_sprites;
公共位图_backgroundImage=BitmapFactory.decodeResource(getResources(),R.drawable.sky);;
私人语境(private Context)(私人语境);;
//添加重新缩放的内容
背景图像的私有位图;
private float theScaler=initFrameParams()/_backgroundImage.getHeight();
私有位图oneBackImage=Bitmap.createScaledBitmap(_backgroundImage,(int)(标尺*_backgroundImage.getWidth()),(int)(标尺*_backgroundImage.getHeight()),true);
int initFrameParams()
{
//获取用于重新缩放所有内容的屏幕高度
DisplayMetrics=新的DisplayMetrics();
显示=((WindowManager)getSystemService(窗口服务)).getDefaultDisplay();
display.getMetrics(metrics);
int screenHeight=display.getHeight();
返回屏幕高度;
}
公共无效呈现(){
Canvas=null;
试一试{
canvas=this.\u surfaceHolder.lockCanvas(null);
已同步(此。\u surfaceHolder){
这个.onDraw(画布);
}
}最后{
if(canvas!=null){
此._surfaceHolder.unlockCanvasAndPost(画布);
}
}   
}
受保护的void onDraw(画布){
此.renderBackGround(画布);
for(可渲染可渲染:此.\u精灵){
渲染(画布);
}
};
公开作废开始(){
this.spriteThread.switch();
}
公共停车场(){
布尔重试=真;
这个.spriteRead.switchOff();
while(重试){
试一试{
this.spriteThread.join();
重试=错误;
}捕捉(中断异常e){
//我们会一次又一次的尝试。。。
}
}
}
public int getLeft(){
返回0;
}
public int getRight(){
返回这个.theBackgroundImage.getWidth();
}
公共void初始化(上下文上下文,SurfaceHolder SurfaceHolder){
this.spritehread=新spritehread(this);
这个。_surfaceHolder=surfaceHolder;
这是。_sprites=new ArrayList();
这._context=context;
BitmapFactory.Options=new-BitmapFactory.Options();
options.inpurgable=true;
this.theBackgroundImage=一个Backimage;
这个.addSprites();
}
私有void addSprites(){
点开始点=新点(100100);
这个._sprites.add(新的SpriteOne(这个._上下文,这个,startPoint,90));
点开始点1=新点(100300);
这个._sprites.add(新的SpriteOne(这个._上下文,这个,起始点1,50));
点起始点2=新点(200200);
这个._sprites.add(新的SpriteOne(这个._上下文,这个,起始点2,15));
}
私有void renderBackGround(画布)
{
drawBitmap(this.theBackgroundImage,0,0,null);
}
}
如我之前所述,任何帮助都将不胜感激。 再次表示感谢 萨姆

这里是我的答案,重缩放代码就是注释所在的位置 希望这能帮助别人

public class TheTemplate {  

            private SpriteThread spriteThread;    
            private SurfaceHolder _surfaceHolder;     
            private ArrayList<Renderable> _sprites;     
            public Bitmap _backgroundImage = BitmapFactory.decodeResource(getResources(), R.drawable.sky);;  
            private Context _context;  

            // add rescale stuff  
            //private SpriteAnimationActivityEngine theTest;  
            private Bitmap theBackgroundImage;  
            private float totalHeight = _backgroundImage.getHeight();  
            private int screenSized = initFrameParams();  
            private float theScaler = (float) (screenSized / totalHeight);  

            private Bitmap oneBackImage = Bitmap.createScaledBitmap(_backgroundImage,  (int) (theScaler * _backgroundImage.getWidth()),  (int) (theScaler * _backgroundImage.getHeight()), true);  


            int initFrameParams()  
            {  
                DisplayMetrics metrics = new DisplayMetrics();  
                Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();  
                display.getMetrics(metrics);  

                int screenHeight = display.getHeight();  
                return screenHeight;  
            }  


            public void render(){  
                Canvas canvas = null;  
                try{  

                    canvas = this._surfaceHolder.lockCanvas(null);  
                    synchronized (this._surfaceHolder) {  
                        this.onDraw(canvas);  
                    }  

                }finally{  
                    if(canvas != null){  
                        this._surfaceHolder.unlockCanvasAndPost(canvas);  
                    }  
                }     
            }  

            protected void onDraw(Canvas canvas) {  
                this.renderBackGround(canvas);                
                for (Renderable renderable : this._sprites) {  
                    renderable.render(canvas);  
                }  
            };  

            public void start(){  
                this.spriteThread.switchOn();  
            }  

            public void stop(){  
                boolean retry = true;  
                this.spriteThread.switchOff();  
                while (retry) {  
                    try {  
                     this.spriteThread.join();  
                        retry = false;  
                    } catch (InterruptedException e) {  
                        // we will try it again and again...  
                    }  
                }  
            }  

            public int getLeft() {        
                return 0;  
            }  


            public int getRight() {  
                return this.theBackgroundImage.getWidth();  
            }  

            public void initialize(Context context, SurfaceHolder surfaceHolder) {  
                this.spriteThread = new SpriteThread(this);   
                this._surfaceHolder = surfaceHolder;          
                this._sprites = new ArrayList<Renderable>();  
                this._context = context;  
                BitmapFactory.Options options = new BitmapFactory.Options();  
                options.inPurgeable = true;  
                this.theBackgroundImage = oneBackImage;  
                this.addSprites();  
            }  


            private void addSprites() {       
                Point startPoint = new Point(100, 100);  
                this._sprites.add(new SpriteOne(this._context, this, startPoint, 90));  
                Point startPoint1 = new Point(100, 300);  
                this._sprites.add(new SpriteOne(this._context, this, startPoint1, 50));  

                Point startPoint2 = new Point(200, 200);  
                this._sprites.add(new SpriteOne(this._context, this, startPoint2, 15));  
            }  

            private void renderBackGround(Canvas canvas)  
            {  
                canvas.drawBitmap(this.theBackgroundImage, 0, 0, null);  
            }  
        }  
公共类模板{
私有spritehread spritehread;
私人表面股东(SurfaceHolder);;
私人ArrayList_sprites;
公共位图_backgroundImage=BitmapFactory.decodeResource(getResources(),R.drawable.sky);;
私人语境(private Context)(私人语境);;
//添加重新缩放的内容
//private SpriteAnimation Activity引擎测试;
背景图像的私有位图;
private float totalHeight=_backgroundImage.getHeight();
private int screenSized=initFrameParams();
专用浮标标尺=(浮标)(屏幕大小/总高度);
私有位图oneBackImage=Bitmap.createScaledBitmap(_backgroundImage,(int)(标尺*_backgroundImage.getWidth(
public class TheTemplate {  

            private SpriteThread spriteThread;    
            private SurfaceHolder _surfaceHolder;     
            private ArrayList<Renderable> _sprites;     
            public Bitmap _backgroundImage = BitmapFactory.decodeResource(getResources(), R.drawable.sky);;  
            private Context _context;  

            // add rescale stuff  
            //private SpriteAnimationActivityEngine theTest;  
            private Bitmap theBackgroundImage;  
            private float totalHeight = _backgroundImage.getHeight();  
            private int screenSized = initFrameParams();  
            private float theScaler = (float) (screenSized / totalHeight);  

            private Bitmap oneBackImage = Bitmap.createScaledBitmap(_backgroundImage,  (int) (theScaler * _backgroundImage.getWidth()),  (int) (theScaler * _backgroundImage.getHeight()), true);  


            int initFrameParams()  
            {  
                DisplayMetrics metrics = new DisplayMetrics();  
                Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();  
                display.getMetrics(metrics);  

                int screenHeight = display.getHeight();  
                return screenHeight;  
            }  


            public void render(){  
                Canvas canvas = null;  
                try{  

                    canvas = this._surfaceHolder.lockCanvas(null);  
                    synchronized (this._surfaceHolder) {  
                        this.onDraw(canvas);  
                    }  

                }finally{  
                    if(canvas != null){  
                        this._surfaceHolder.unlockCanvasAndPost(canvas);  
                    }  
                }     
            }  

            protected void onDraw(Canvas canvas) {  
                this.renderBackGround(canvas);                
                for (Renderable renderable : this._sprites) {  
                    renderable.render(canvas);  
                }  
            };  

            public void start(){  
                this.spriteThread.switchOn();  
            }  

            public void stop(){  
                boolean retry = true;  
                this.spriteThread.switchOff();  
                while (retry) {  
                    try {  
                     this.spriteThread.join();  
                        retry = false;  
                    } catch (InterruptedException e) {  
                        // we will try it again and again...  
                    }  
                }  
            }  

            public int getLeft() {        
                return 0;  
            }  


            public int getRight() {  
                return this.theBackgroundImage.getWidth();  
            }  

            public void initialize(Context context, SurfaceHolder surfaceHolder) {  
                this.spriteThread = new SpriteThread(this);   
                this._surfaceHolder = surfaceHolder;          
                this._sprites = new ArrayList<Renderable>();  
                this._context = context;  
                BitmapFactory.Options options = new BitmapFactory.Options();  
                options.inPurgeable = true;  
                this.theBackgroundImage = oneBackImage;  
                this.addSprites();  
            }  


            private void addSprites() {       
                Point startPoint = new Point(100, 100);  
                this._sprites.add(new SpriteOne(this._context, this, startPoint, 90));  
                Point startPoint1 = new Point(100, 300);  
                this._sprites.add(new SpriteOne(this._context, this, startPoint1, 50));  

                Point startPoint2 = new Point(200, 200);  
                this._sprites.add(new SpriteOne(this._context, this, startPoint2, 15));  
            }  

            private void renderBackGround(Canvas canvas)  
            {  
                canvas.drawBitmap(this.theBackgroundImage, 0, 0, null);  
            }  
        }  
 public class TheTemplate {  

        private SpriteThread spriteThread;    
        private SurfaceHolder _surfaceHolder;     
        private ArrayList<Renderable> _sprites;     
        public Bitmap _backgroundImage = BitmapFactory.decodeResource(getResources(), R.drawable.sky);;  
        private Context _context;  

        // add rescale stuff  
        //private SpriteAnimationActivityEngine theTest;  
        private Bitmap theBackgroundImage;  
        private float totalHeight = _backgroundImage.getHeight();  
        private int screenSized = initFrameParams();  
        private float theScaler = (float) (screenSized / totalHeight);  

        private Bitmap oneBackImage = Bitmap.createScaledBitmap(_backgroundImage,  (int) (theScaler * _backgroundImage.getWidth()),  (int) (theScaler * _backgroundImage.getHeight()), true);  


        int initFrameParams()  
        {  
            DisplayMetrics metrics = new DisplayMetrics();  
            Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();  
            display.getMetrics(metrics);  

            int screenHeight = display.getHeight();  
            return screenHeight;  
        }  


        public void render(){  
            Canvas canvas = null;  
            try{  

                canvas = this._surfaceHolder.lockCanvas(null);  
                synchronized (this._surfaceHolder) {  
                    this.onDraw(canvas);  
                }  

            }finally{  
                if(canvas != null){  
                    this._surfaceHolder.unlockCanvasAndPost(canvas);  
                }  
            }     
        }  

        protected void onDraw(Canvas canvas) {  
            this.renderBackGround(canvas);                
            for (Renderable renderable : this._sprites) {  
                renderable.render(canvas);  
            }  
        };  

        public void start(){  
            this.spriteThread.switchOn();  
        }  

        public void stop(){  
            boolean retry = true;  
            this.spriteThread.switchOff();  
            while (retry) {  
                try {  
                 this.spriteThread.join();  
                    retry = false;  
                } catch (InterruptedException e) {  
                    // we will try it again and again...  
                }  
            }  
        }  

        public int getLeft() {        
            return 0;  
        }  


        public int getRight() {  
            return this.theBackgroundImage.getWidth();  
        }  

        public void initialize(Context context, SurfaceHolder surfaceHolder) {  
            this.spriteThread = new SpriteThread(this);   
            this._surfaceHolder = surfaceHolder;          
            this._sprites = new ArrayList<Renderable>();  
            this._context = context;  
            BitmapFactory.Options options = new BitmapFactory.Options();  
            options.inPurgeable = true;  
            this.theBackgroundImage = oneBackImage;  
            this.addSprites();  
        }  


        private void addSprites() {       
            Point startPoint = new Point(100, 100);  
            this._sprites.add(new SpriteOne(this._context, this, startPoint, 90));  
            Point startPoint1 = new Point(100, 300);  
            this._sprites.add(new SpriteOne(this._context, this, startPoint1, 50));  

            Point startPoint2 = new Point(200, 200);  
            this._sprites.add(new SpriteOne(this._context, this, startPoint2, 15));  
        }  

        private void renderBackGround(Canvas canvas)  
        {  
            canvas.drawBitmap(this.theBackgroundImage, 0, 0, null);  
        }  
    }