如何在Android动态壁纸中添加布局

时间:2011-09-26 05:24:07

标签: java android android-layout canvas live-wallpaper

我已经使用画布创建了动态壁纸现在我想添加布局像(相对布局) 如果有可能吗?

在动态壁纸中,我们可以使用布局吗?

请指导我如何在这里添加布局? 如果可能的话意味着我如何在canvas类中调用该布局? 在这个画布中,我怎么能覆盖ondraw mathod?

我搜索了漏洞互联网,我无法对任何信息进行罚款。 我是新的android动态壁纸和jave plz指导我。 这是我的代码

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();
        }
}

这是我的画布类

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.getResources(), 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);
    }
}

1 个答案:

答案 0 :(得分:0)

你可以随时在WallpaperService中调用getTheme(),然后对其应用布局,但不确定它对你有什么用处。看起来你已经把一切都搞定了,如果我是你的话,不会真的打扰布局......