开发者

how to add layouts in android livewallpaper

开发者 https://www.devze.com 2023-04-08 21:18 出处:网络
i havecreated live wallpaper using canvas now i want to add the layout like(relative layout) if its possible?

i have created live wallpaper using canvas now i want to add the layout like(relative layout) if its possible?

in live wallpaper we can able to use layout?

please guide me how to add layout here if its possible? if possible means how can i called that layout in my canvas class? in this canvas how can i overwrite the ondraw mathod?

i have searched hole internet i can't able to fine any information. i'm new to android live wallpaper and jave plz guide me. here is my code

public class AquariumWallpaperService extends WallpaperService {
    private float mTouchX = -1;
    private float mTouchY = -1;
    int count = 1;
    public AquaticAnimal animal;
    public static final String SHARED_PREFS_NAME = "livewallpapertemplatesettings";
    @Override
    public Engine onCreateEngine() {
        return new AquariumWallpaperEngine();
    }

    class AquariumWallpaperEngine extends Engine {

        private Aquarium aquarium;

        public AquariumWallpaperEngine() {
            this.aquarium = new Aquarium();
            this.aquarium.initialize(getBaseContext(), getSurfaceHolder());
        }

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

            // By default we don't get touch events, so enable them.
            setTouchEventsEnabled(true);
        }

        @Override
        public void onVisibilityChanged(boolean visible) {
            if (visible) {
                this.aquarium.render();
            }
        }

        @Override
        public void onSurfaceChanged(SurfaceHolder holder, int format,
                int width, int height) {
            super.onSurfaceChanged(holder, format, width, height);
        }

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

        @Override
        public void onSurfaceDestroyed(SurfaceHolder holder) {
            super.onSurfaceDestroyed(holder);
            this.aquarium.stop();
        }
}

this is my canvas class

public class Aquarium {

    private AquariumThread aquariumThread;
    private SurfaceHolder surfaceHolder;
    private ArrayList<Renderable> fishes;
    private Bitmap backgroundImage, backgroundImage1;
    private Bitmap boble;
    public Boolean bgchange = false;
    private Context context;
    public int count = 1, x = 100, y = 500, x1 = 400, y1 = 500, x2 = 10,
            y2 = 250;
    public AquariumWallpaperEngine aqua;


    public void render() {
        Canvas canvas = null;
        try {
            count++;
            if (count > 5) {
                if (count % 8 == 0) {
                    x2++;
                    y--;
                    y1--;
                }
            }
            if (y == -20) {
                y = 600;
                y1 = 600;
            }
            if (x2 == 700) {
                x2 = -20;
            }
            if (count > 3000) {
                bgchange = true;
            }
            if (count > 6000) {
                bgchange = false;
                count = 0;
            }
            //System.out.println("count" + count);
            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.fishes) {
            renderable.render(canvas);
        }
    };

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

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

    public int getLeft() {
        return 0;
    }

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

    public int getRightbg() {
        return this.backgroundImage1.getWidth();
    }

    public void initialize(Context context, SurfaceHolder surfaceHolder) {
        this.aquariumThread = new AquariumThread(this);
        this.surfaceHolder = surfaceHolder;
        this.fishes = new ArrayList<Renderable>();
        this.context = context;
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPurgeable = true;
        this.backgroundImage = BitmapFactory.decodeResource(
                context.getResources(), com.thinkpal.live.R.drawable.aquarium,
                options);
        this.backgroundImage1 = BitmapFactory.decodeResource(
                context.getR开发者_如何转开发esources(), com.thinkpal.live.R.drawable.waquarium,
                options);
        this.boble = BitmapFactory.decodeResource(context.getResources(),
                com.thinkpal.live.R.drawable.bubble, options);
        this.addFishes();

    }

    private void addFishes() {
        Point startPoint = new Point(100, 100);
        this.fishes.add(new ClownFish(this.context, this, startPoint, 90));
        Point startPoint1 = new Point(100, 300);
        this.fishes.add(new ClownFish(this.context, this, startPoint1, 50));
        Point startPoint2 = new Point(200, 200);
        this.fishes.add(new ClownFish(this.context, this, startPoint2, 15));
    }

    private void renderBackGround(Canvas canvas) {
        Paint paint = new Paint();
        canvas.drawBitmap(this.backgroundImage, 0, 0, null);
        if (bgchange) {
            canvas.drawBitmap(this.backgroundImage1, 0, 0, null);
        }
        canvas.drawBitmap(this.boble, x, y, null);
        canvas.drawBitmap(this.boble, x1, y1, null);
        canvas.drawBitmap(this.boble, x2, y2, null);
        canvas.drawText("Think palm", x + 10, y + 45, paint);
        canvas.drawText("Cochin", x1 + 20, y1 + 45, paint);
        canvas.drawText("Welcome to", x2 + 10, y2 + 45, paint);
    }
}


YOu could always call getTheme() in WallpaperService and then apply layouts to that, not sure what use it will be to you though. It looks like you've already got everything working, wouldn't really bother with layouts if I were you...

0

精彩评论

暂无评论...
验证码 换一张
取 消