2011-01-05 3 views
1

Est-il possible de dessiner une image lors de contact et un BG transparent, donc je vois mon homesceen avec ceci:Développement Android: 2d Graphic Transparent?

android:theme="@android:style/Theme.Translucent.NoTitleBar" 

Et voici mon projet jusqu'à présent:

import java.util.ArrayList; 



import android.app.Activity; 
import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Bitmap.Config; 
import android.os.Bundle; 
import android.view.Display; 
import android.view.MotionEvent; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 
import android.view.Window; 
import android.view.WindowManager; 
import android.os.Bundle; 


public class main extends Activity { 
    private Panel mG; 


@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     // setContentView(new Panel(this)); 
     setContentView(R.layout.main); 

     mG = new Panel(this); 
     setContentView(mG); 
    } 

    class Panel extends SurfaceView implements SurfaceHolder.Callback { 
     private TutorialThread _thread; 
     private ArrayList<GraphicObject> _graphics = new ArrayList<GraphicObject>(); 

     public Panel(Context context) { 
      super(context); 
      getHolder().addCallback(this); 
      _thread = new TutorialThread(getHolder(), this); 
      setFocusable(true); 
     } 

     @Override 
     public boolean onTouchEvent(MotionEvent event) { 
      synchronized (_thread.getSurfaceHolder()) { 
       if (event.getAction() == MotionEvent.ACTION_DOWN) { 
        GraphicObject graphic = new GraphicObject(BitmapFactory.decodeResource(getResources(), R.drawable.myicon)); 
        graphic.getCoordinates().setX((int) event.getX() - graphic.getGraphic().getWidth()/2); 
        graphic.getCoordinates().setY((int) event.getY() - graphic.getGraphic().getHeight()/2); 
        _graphics.add(graphic); 
       } 
       return true; 
      } 
     } 

     @Override 
     public void onDraw(Canvas canvas) { 
     Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay(); 
     int height = display.getHeight(); // not exactly correct 
      int width = display.getWidth(); // not exactly correct 

     //Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ALPHA_8);//Config.RGB_565); 



      //canvas.drawColor(Color.Transparent); 
      Bitmap bitmap; 
      GraphicObject.Coordinates coords; 
      for (GraphicObject graphic : _graphics) { 
       bitmap = graphic.getGraphic(); 
       coords = graphic.getCoordinates(); 
       canvas.drawBitmap(bitmap, coords.getX(), coords.getY(), null); 
      } 
     } 

     @Override 
     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 
      // TODO Auto-generated method stub 
     } 

     @Override 
     public void surfaceCreated(SurfaceHolder holder) { 
      _thread.setRunning(true); 
      _thread.start(); 
     } 

     @Override 
     public void surfaceDestroyed(SurfaceHolder holder) { 
      // simply copied from sample application LunarLander: 
      // we have to tell thread to shut down & wait for it to finish, or else 
      // it might touch the Surface after we return and explode 
      boolean retry = true; 
      _thread.setRunning(false); 
      while (retry) { 
       try { 
        _thread.join(); 
        retry = false; 
       } catch (InterruptedException e) { 
        // we will try it again and again... 
       } 
      } 
     } 
    } 

    class TutorialThread extends Thread { 
     private SurfaceHolder _surfaceHolder; 
     private Panel _panel; 
     private boolean _run = false; 

     public TutorialThread(SurfaceHolder surfaceHolder, Panel panel) { 
      _surfaceHolder = surfaceHolder; 
      _panel = panel; 
     } 

     public void setRunning(boolean run) { 
      _run = run; 
     } 

     public SurfaceHolder getSurfaceHolder() { 
      return _surfaceHolder; 
     } 

     @Override 
     public void run() { 
      Canvas c; 
      while (_run) { 
       c = null; 
       try { 
        c = _surfaceHolder.lockCanvas(null); 
        synchronized (_surfaceHolder) { 
         _panel.onDraw(c); 
        } 
       } finally { 
        // do this in a finally so that if an exception is thrown 
        // during the above, we don't leave the Surface in an 
        // inconsistent state 
        if (c != null) { 
         _surfaceHolder.unlockCanvasAndPost(c); 
        } 
       } 
      } 
     } 
    } 

    class GraphicObject { 
     /** 
     * Contains the coordinates of the graphic. 
     */ 
     public class Coordinates { 
      private int _x = 100; 
      private int _y = 0; 

      public int getX() { 
       return _x + _bitmap.getWidth()/2; 
      } 

      public void setX(int value) { 
       _x = value - _bitmap.getWidth()/2; 
      } 

      public int getY() { 
       return _y + _bitmap.getHeight()/2; 
      } 

      public void setY(int value) { 
       _y = value - _bitmap.getHeight()/2; 
      } 

      public String toString() { 
       return "Coordinates: (" + _x + "/" + _y + ")"; 
      } 
     } 

     private Bitmap _bitmap; 
     private Coordinates _coordinates; 

     public GraphicObject(Bitmap bitmap) { 
      _bitmap = bitmap; 
      _coordinates = new Coordinates(); 
     } 

     public Bitmap getGraphic() { 
      return _bitmap; 
     } 

     public Coordinates getCoordinates() { 
      return _coordinates; 
     } 
    } 
} 

Cependant l'arrière-plan est noir et je ne vois pas mon écran d'accueil (ou mon activité de fond)

Qu'est-ce que je fais mal?

// Votre ami!

Répondre

0

Bon à voir quelqu'un est en train de lire mes tutoriels :)

Je ne sais pas s'il est possible de voir l'écran d'accueil à travers l'arrière-plan, mais peut-être vous jeter un oeil à ceci: How do I create a transparent Activity on Android?

+0

Ce qui tutoriel C'est le tien? Et vous avez essayé, mais je ne peux écrire qu'une seule chose dans setContentView. Donc, si je mets main.xml cela fonctionne mais pas de toile ou bitmap. Ou je prends un nouveau panneau et il ne sera pas transparent :(Cela doit fonctionner, j'ai vu des applications comme le crack de l'écran faux – user564612

+0

Votre code est basé sur un tutoriel de mon blog ... http://www.droidnova.com .. Vous devez spécifier le Panel dans le fichier main.xml et là vous pouvez ajouter quelques attributs comme dans le lien mentionné – WarrenFaith

+0

Yeye Droidnova est niccee: P Pourriez-vous expliquer un peu plus comment spécifier le panneau dans main.xml? – user564612