2011-12-15 33 views
0

OK,多的畫布如何充滿

我試圖使具有刮開特徵的應用程序(如從樂透刮開門票)。

我一直在周圍挖,不能找到一種方法來告訴多少我的畫布是由我的父母/彩色覆膜。看到我想要的是,他們必須先揭開背景圖片的x%,然後才能點擊聲明。

我沒有找到答案原來的問題。但一個新的問題出現在處理同樣的事情上。

計算出的百分比時 「INT百分比=(trans_count/MAX_COUNT)* 100;」它總是返回0,在我的應用程序中,max_count始終爲90000(因爲圖片大小和我的機器人屏幕)。

這是我目前的代碼。

class TouchView extends ImageButton { 
    public Boolean Clickable = false; 


    private Bitmap bgr = null; 
    private Bitmap overlayDefault; 
    private Bitmap overlay; 
    private Paint pTouch; 
    private int X = -100; 
    private int Y = -100; 
    private Canvas c2; 

    public TouchView(Context context) { super(context); build();} 

    public TouchView(Context context, AttributeSet attrs) { super(context, attrs); build(attrs); } 

    public TouchView(Context context, AttributeSet attrs, Integer params) { super(context, attrs, params); build(attrs);} 

    private void build() 
    { 
     bgr = BitmapFactory.decodeResource(getResources(),R.drawable.loser); 
     overlayDefault = BitmapFactory.decodeResource(getResources(),R.drawable.over); 
     overlay = BitmapFactory.decodeResource(getResources(), R.drawable.over).copy(Config.ARGB_8888, true); 

     pTouch = new Paint(Paint.ANTI_ALIAS_FLAG); 
     pTouch.setXfermode(new PorterDuffXfermode(Mode.SRC_OUT)); 
     pTouch.setColor(Color.TRANSPARENT); 
     pTouch.setMaskFilter(new BlurMaskFilter(2, Blur.NORMAL)); 
     pTouch.setStrokeWidth(20); 
    } 

    private void build(AttributeSet attrs) 
    { 
     bgr = BitmapFactory.decodeResource(getResources(),R.drawable.loser); 
     overlayDefault = BitmapFactory.decodeResource(getResources(),R.drawable.over); 
     overlay = BitmapFactory.decodeResource(getResources(), R.drawable.over).copy(Config.ARGB_8888, true); 

     pTouch = new Paint(Paint.ANTI_ALIAS_FLAG); 
     pTouch.setXfermode(new PorterDuffXfermode(Mode.SRC_OUT)); 
     pTouch.setColor(Color.TRANSPARENT); 
     pTouch.setMaskFilter(new BlurMaskFilter(2, Blur.NORMAL)); 
     pTouch.setStrokeWidth(20); 
    } 

    public void setAsWinner() { 
     bgr = BitmapFactory.decodeResource(getResources(), R.drawable.winner); 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent ev) { 
     int sX = X; 
     int sY = Y; 
     switch (ev.getAction()) { 
      case MotionEvent.ACTION_DOWN: { 
       X = (int) ev.getX(); 
       Y = (int) ev.getY(); 
       invalidate(); 
       c2.drawCircle(X, Y, 5, pTouch); 
       break; 
      } 
      case MotionEvent.ACTION_MOVE: { 
       X = (int) ev.getX(); 
       Y = (int) ev.getY(); 
       invalidate(); 
       c2.drawLine(sX, sY, X, Y, pTouch); 
       break; 
      } 
      case MotionEvent.ACTION_UP: 
       X = (int) ev.getX(); 
       Y = (int) ev.getY(); 
       invalidate(); 
       c2.drawLine(sX, sY, X, Y, pTouch); 
       break; 
     } 

     try 
     { 
      int pixels[] = new int[(overlay.getWidth() * overlay.getHeight())]; 
      overlay.getPixels(pixels, 0, overlay.getWidth(), 0, 0, overlay.getWidth(), overlay.getHeight()); 
      int max_count = pixels.length; 
      int trans_count = 0; 

      for(int i = 0 ; i < max_count; i++) 
      { 
       if (pixels[i] == 0) 
        trans_count++; 
      } 

      int percent = (trans_count/max_count) * 100; 

      if (percent > 60) 
      { 
       Clickable = true; 
      } 
     } 
     catch (IllegalArgumentException e) 
     { 
      Log.i ("info", e.getMessage()); 
     } 
     catch (ArrayIndexOutOfBoundsException e) 
     { 
      Log.i ("info", e.getMessage()); 
     } 

     return true; 
    } 


    @Override 
    public void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 

     // draw background 
     if (bgr != null) 
      canvas.drawBitmap(bgr, 0, 0, null); 

     if (c2 == null) 
     { 
      int width = overlay.getWidth(); 
      int height = overlay.getHeight(); 

      RectF bounds = new RectF(canvas.getClipBounds()); 

      float scaleWidth = bounds.width()/width; 
      float scaleHeight = bounds.height()/height; 

      // create a matrix for the manipulation 
      Matrix matrix = new Matrix(); 
      // resize the bit map 
      matrix.postScale(scaleWidth, scaleHeight); 


      overlay = Bitmap.createBitmap(overlay,0, 0, width, height, matrix, true); 
      c2 = new Canvas(overlay); 
     } 

     canvas.drawBitmap(overlay, 0, 0, null); 
    } 

} 

回答

1

trans_countmax_count是整數,所以當你的java trans_count/max_count執行整數除法,結果是0,因爲trans_countmax_count小。你想要的是浮點除法什麼,你可以迫使它這樣的:

double percentage = 100.0 * trans_count/max_count; 

基本上會發生什麼是100.0 * trans_count將得到執行先給予一個浮點值(因爲其中一個值是浮點),然後結果將再除以max_count給出浮點值。

+0

你知道這其實奇怪了吧。我已經對多種語言進行了百分比計算,這是我第一次必須這樣做。 – Pyromanci 2011-12-15 22:04:23