2011-09-26 5 views
1

캔버스를 사용하여 라이브 벽지를 만들었습니다. (상대 레이아웃) 과 같은 레이아웃을 추가하고 싶습니다. 가능한 경우?안드로이드 라이브 월페이퍼에서 레이아웃을 추가하는 방법

라이브 벽지에서 레이아웃을 사용할 수 있습니까?

가능하면 레이아웃을 추가하는 방법을 안내해주세요. 가능하면 캔버스 클래스에서 레이아웃을 어떻게 호출 할 수 있습니까? 이 캔버스에 어떻게 ondraw mathod를 덮어 쓸 수 있습니까?

나는 홀 인터넷을 검색했다. 나는 어떤 정보도 잘 수 없다. 나는 안드로이드 라이브 벽지가 처음이다. 그리고 jave plz이 나를 인도한다. 다음은 항상 WallpaperService에 getTheme()를 호출하고 그에 레이아웃을 적용 할 수 있는지를 사용하지 않을 무엇 비록 당신에게 것이 내 캔버스 클래스

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

답변

0

내 코드

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

입니다 . 이미 당신이 모든 일을하고있는 것처럼 보입니다. 제가 당신이라면 레이아웃에 신경 쓸 필요가 없을 것입니다 ...

+0

이것에 대해 자세히 설명해 주시겠습니까? WallpaperService에서 getTheme()을 어디에서 호출하고 레이아웃을 추가 할 수 있습니까? FrameLayout을 라이브 배경 화면의 컨텐트로 사용하려고합니다. 감사! – Nick

+0

그것에 관해가는 길은 상황에 따라 달라질 수 있습니다. FrameLayout 소스를 어딘가에 게시 할 수 있습니까? 또한 레이아웃을 적용하여 레이아웃 레이아웃 속성 (layout_width, layout_gravity 등)을 언급했습니다. 혼란 스럽습니다. 손이 필요한지 알고 싶다면 라이브 배경 화면에 접근 할 수있는 방법은 다양하며 불가능한 것은 없습니다. –

관련 문제