Java Code Examples for android.graphics.Canvas#saveLayerAlpha()

The following examples show how to use android.graphics.Canvas#saveLayerAlpha() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VisualizerView.java    From zom-android-matrix with Apache License 2.0 6 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (mBytes == null) {
        return;
    }

    // Calc width
    int n = Math.min(numBars, (mBytes == null) ? 0 : mBytes.length);
    int w = n * barWidth + Math.max(0, n - 1) * gapWidth;
    float x = getPlayFraction() * w;

    canvas.saveLayerAlpha(0, 0, x, getHeight(), alphaPlayed,
            Canvas.ALL_SAVE_FLAG);
    drawBars(canvas, paint);
    canvas.restore();
    canvas.saveLayerAlpha(x, 0, getWidth(), getHeight(), alphaUnplayed,
            Canvas.ALL_SAVE_FLAG);
    drawBars(canvas, paint);
    canvas.restore();
}
 
Example 2
Source File: SwitchButton.java    From BaseProject with Apache License 2.0 6 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    canvas.saveLayerAlpha(mSaveLayerRectF, mAlpha, Canvas.MATRIX_SAVE_FLAG
            | Canvas.CLIP_SAVE_FLAG | Canvas.HAS_ALPHA_LAYER_SAVE_FLAG
            | Canvas.FULL_COLOR_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
    // 绘制蒙板
    canvas.drawBitmap(mMask, 0, mExtendOffsetY, mPaint);
    mPaint.setXfermode(mXfermode);

    // 绘制底部图片
    canvas.drawBitmap(mBottom, mRealPos, mExtendOffsetY, mPaint);
    mPaint.setXfermode(null);
    // 绘制边框
    canvas.drawBitmap(mFrame, 0, mExtendOffsetY, mPaint);

    // 绘制按钮
    canvas.drawBitmap(mCurBtnPic, mRealPos, mExtendOffsetY, mPaint);
    canvas.restore();
}
 
Example 3
Source File: SwitchButton.java    From NewXmPluginSDK with Apache License 2.0 6 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    int alpha = isEnabled() ? FULL_ALPHA : FULL_ALPHA / 2;
    canvas.saveLayerAlpha(0, 0, mMask.getWidth(), mMask.getHeight(), alpha, Canvas.ALL_SAVE_FLAG);
    // draw mask
    canvas.drawBitmap(mMask, 0, 0, null);
    // draw the background (on or off)
    drawBar(canvas);
    // draw the frame
    mFrame.draw(canvas);
    // draw the slider
    if (mSliderOnAlpha <= FULL_ALPHA) {
        mSliderOff.setBounds(mSliderOffset, 0, mSliderWidth + mSliderOffset, mHeight);
        mSliderOff.draw(canvas);
    }

    mSliderOn.setAlpha(mSliderOnAlpha);
    mSliderOn.setBounds(mSliderOffset, 0, mSliderWidth + mSliderOffset, mHeight);
    mSliderOn.draw(canvas);
    canvas.restore();
}
 
Example 4
Source File: Canvas1.java    From zone-sdk with MIT License 6 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
	super.onDraw(canvas);
	System.out.println("¿ªÊ¼£º"+canvas.getSaveCount());
	paint.setColor(Color.RED);
	paint.setDither(true);
	paint.setAntiAlias(true);
	canvas.drawCircle(100, 100, 100, paint);
	canvas.save();
	System.out.println("save£º"+canvas.getSaveCount());
	canvas.saveLayerAlpha(0, 0, getWidth(), getHeight(), 100, Canvas.ALL_SAVE_FLAG);
	System.out.println("saveLayerAlpha£º"+canvas.getSaveCount());
	canvas.translate(100, 100);
	paint.setColor(Color.BLUE);
	canvas.drawCircle(100, 100, 100, paint);
	canvas.restoreToCount(1);
	System.out.println("restore£º"+canvas.getSaveCount());
}
 
Example 5
Source File: SwitchButton.java    From BlunoAccessoryShieldDemo with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    canvas.saveLayerAlpha(mSaveLayerRectF, mAlpha, Canvas.MATRIX_SAVE_FLAG
            | Canvas.CLIP_SAVE_FLAG | Canvas.HAS_ALPHA_LAYER_SAVE_FLAG
            | Canvas.FULL_COLOR_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
    // 绘制蒙板
    canvas.drawBitmap(mMask, 0, mExtendOffsetY, mPaint);
    mPaint.setXfermode(mXfermode);

    // 绘制底部图片
    canvas.drawBitmap(mBottom, mRealPos, mExtendOffsetY, mPaint);
    mPaint.setXfermode(null);
    // 绘制边框
    canvas.drawBitmap(mFrame, 0, mExtendOffsetY, mPaint);

    // 绘制按钮
    canvas.drawBitmap(mCurBtnPic, mRealPos, mExtendOffsetY, mPaint);
    canvas.restore();
}
 
Example 6
Source File: SwitchButton.java    From BigApp_Discuz_Android with Apache License 2.0 6 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
	canvas.saveLayerAlpha(mSaveLayerRectF, mAlpha, Canvas.MATRIX_SAVE_FLAG
			| Canvas.CLIP_SAVE_FLAG | Canvas.HAS_ALPHA_LAYER_SAVE_FLAG
			| Canvas.FULL_COLOR_LAYER_SAVE_FLAG
			| Canvas.CLIP_TO_LAYER_SAVE_FLAG);
	// 绘制蒙板
	canvas.drawBitmap(mMask, 0, mExtendOffsetY, mPaint);
	mPaint.setXfermode(mXfermode);

	// 绘制底部图片
	canvas.drawBitmap(mBottom, mRealPos, mExtendOffsetY, mPaint);
	mPaint.setXfermode(null);
	// 绘制边框
	canvas.drawBitmap(mFrame, 0, mExtendOffsetY, mPaint);

	// 绘制按钮
	canvas.drawBitmap(mCurBtnPic, mRealPos, mExtendOffsetY, mPaint);
	canvas.restore();
}
 
Example 7
Source File: CanvasCompat.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
/**
 * Convenience for {@link Canvas#saveLayer(RectF, Paint)} but instead of taking a entire Paint
 * object it takes only the {@code alpha} parameter.
 */
public static int saveLayerAlpha(@NonNull Canvas canvas, @Nullable RectF bounds, int alpha) {
  if (VERSION.SDK_INT > VERSION_CODES.LOLLIPOP) {
    return canvas.saveLayerAlpha(bounds, alpha);
  } else {
    return canvas.saveLayerAlpha(bounds, alpha, Canvas.ALL_SAVE_FLAG);
  }
}
 
Example 8
Source File: FloatingActionButton.java    From android-chat-ui with Apache License 2.0 5 votes vote down vote up
@Override
public void draw(Canvas canvas) {
  Rect bounds = getBounds();
  canvas.saveLayerAlpha(bounds.left, bounds.top, bounds.right, bounds.bottom, mAlpha, Canvas.ALL_SAVE_FLAG);
  super.draw(canvas);
  canvas.restore();
}
 
Example 9
Source File: FloatingActionButton.java    From FloatingActionButton with Apache License 2.0 5 votes vote down vote up
@Override
public void draw(Canvas canvas) {
  Rect bounds = getBounds();
  canvas.saveLayerAlpha(bounds.left, bounds.top, bounds.right, bounds.bottom, mAlpha, Canvas.ALL_SAVE_FLAG);
  super.draw(canvas);
  canvas.restore();
}
 
Example 10
Source File: HorizontalHeaderItemDecoration.java    From aptoide-client-v8 with GNU General Public License v3.0 5 votes vote down vote up
@Override public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
  super.onDraw(c, parent, state);

  boolean foundFirstChild = false;
  float viewVerticalCenter = 0f;
  float left = 0f;
  float limit = -(layout.getMeasuredWidth() / 2f);
  float initialPosition =
      ((headerSize / 2f) - (layout.getMeasuredWidth() / 2f) - (margin / 2f)) * PARALLAX_RATIO;

  for (int i = 0; i < parent.getChildCount(); i++) {
    View view = parent.getChildAt(i);
    if (viewVerticalCenter == 0f) {
      viewVerticalCenter =
          view.getTop() + (view.getMeasuredHeight() / 2f) - (layout.getMeasuredWidth() / 2f);
    }
    if (parent.getChildAdapterPosition(view) == 0) {
      left =
          view.getLeft() - (headerSize / 2f) - (layout.getMeasuredWidth() / 2f) - (margin / 2f);
      left *= PARALLAX_RATIO;
      if (left < limit) {
        left = limit;
      }
      foundFirstChild = true;
      break;
    }
  }
  if (!foundFirstChild) {
    left = limit;
  }
  // With API <= 23, there's a bug where if alpha is set to 0, TextViews' alpha is not set correctly
  int movementPercentage = Math.max((int) (getPercentage(limit, initialPosition, left) * 255), 1);
  c.save();
  c.saveLayerAlpha(new RectF(0, 0, headerSize - 2, c.getHeight()), movementPercentage,
      Canvas.ALL_SAVE_FLAG);
  c.translate(left, viewVerticalCenter - VERTICAL_OFFSET);
  layout.draw(c);
  c.restore();
}
 
Example 11
Source File: GesturePadView.java    From SoloPi with Apache License 2.0 5 votes vote down vote up
@Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = getWidth();

        if (width != oldWidth) {
            reloadWidth(width);
        }

        if (targetImg == null && sourceImg != null) {
            loadTargetImg(width);
        }

        backgroundRes.draw(canvas);
//        canvas.save();

        if (targetImg != null) {
            targetImg.draw(canvas);
        }

        canvas.saveLayerAlpha(0, 0, width, width, 125, Canvas.ALL_SAVE_FLAG);
//        canvas.save();
        canvas.drawRect(clearBtnRect, backgroundPaint);
        clearBtnRes.draw(canvas);
        canvas.restore();

        drawPoints(canvas);
    }
 
Example 12
Source File: ImageWindow.java    From ImageWindow with Apache License 2.0 5 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    canvas.saveLayerAlpha(0, 0, canvas.getWidth(), canvas.getHeight(), 255, Canvas.HAS_ALPHA_LAYER_SAVE_FLAG);
    super.onDraw(canvas);
    canvas.drawPath(mRectanglePath, mEraser);
    //5 is the margin of the close circle
    canvas.drawCircle((int) ((mCloseSize * 0.5) - ((LayoutParams) getLayoutParams()).leftMargin),
            (int) ((mCloseSize * 0.5) - ((LayoutParams) getLayoutParams()).topMargin),
            (int) (((mCloseSize * 0.5) + mCloseButtonMargin)), mEraser);
}
 
Example 13
Source File: FloatingActionButton.java    From TestChat with Apache License 2.0 5 votes vote down vote up
@Override
public void draw(Canvas canvas) {
        Rect bounds = getBounds();
        canvas.saveLayerAlpha(bounds.left, bounds.top, bounds.right, bounds.bottom, mAlpha, Canvas.ALL_SAVE_FLAG);
        super.draw(canvas);
        canvas.restore();
}
 
Example 14
Source File: SwitchButton.java    From SettingView with Apache License 2.0 5 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
	canvas.saveLayerAlpha(mSaveLayerRectF, mAlpha, Canvas.MATRIX_SAVE_FLAG | Canvas.CLIP_SAVE_FLAG | Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG
			| Canvas.CLIP_TO_LAYER_SAVE_FLAG);

	// 绘制底部图片
	canvas.drawBitmap(bmCurBgPic, 0, 0, mPaint);

	// 绘制按钮
	canvas.drawBitmap(bmCurBtnPic, curBtnPos, 0, mPaint);

	canvas.restore();
}
 
Example 15
Source File: CanvasCompat.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
/**
 * Convenience for {@link #saveLayerAlpha(Canvas, RectF, int)} that takes the four float
 * coordinates of the bounds rectangle.
 */
public static int saveLayerAlpha(
    @NonNull Canvas canvas, float left, float top, float right, float bottom, int alpha) {
  if (VERSION.SDK_INT > VERSION_CODES.LOLLIPOP) {
    return canvas.saveLayerAlpha(left, top, right, bottom, alpha);
  } else {
    return canvas.saveLayerAlpha(left, top, right, bottom, alpha, Canvas.ALL_SAVE_FLAG);
  }
}
 
Example 16
Source File: DragSortListView.java    From biermacht with Apache License 2.0 4 votes vote down vote up
@Override
protected void dispatchDraw(Canvas canvas) {
  super.dispatchDraw(canvas);

  if (mDragState != IDLE) {
    // draw the divider over the expanded item
    if (mFirstExpPos != mSrcPos) {
      drawDivider(mFirstExpPos, canvas);
    }
    if (mSecondExpPos != mFirstExpPos && mSecondExpPos != mSrcPos) {
      drawDivider(mSecondExpPos, canvas);
    }
  }

  if (mFloatView != null) {
    // draw the float view over everything
    final int w = mFloatView.getWidth();
    final int h = mFloatView.getHeight();

    int x = mFloatLoc.x;

    int width = getWidth();
    if (x < 0) {
      x = - x;
    }
    float alphaMod;
    if (x < width) {
      alphaMod = ((float) (width - x)) / ((float) width);
      alphaMod *= alphaMod;
    }
    else {
      alphaMod = 0;
    }

    final int alpha = (int) (255f * mCurrFloatAlpha * alphaMod);

    canvas.save();
    // Log.d("mobeta", "clip rect bounds: " + canvas.getClipBounds());
    canvas.translate(mFloatLoc.x, mFloatLoc.y);
    canvas.clipRect(0, 0, w, h);

    // Log.d("mobeta", "clip rect bounds: " + canvas.getClipBounds());
    canvas.saveLayerAlpha(0, 0, w, h, alpha, Canvas.ALL_SAVE_FLAG);
    mFloatView.draw(canvas);
    canvas.restore();
    canvas.restore();
  }
}
 
Example 17
Source File: DragSortListView.java    From simpletask-android with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected void dispatchDraw(@NonNull Canvas canvas) {
    super.dispatchDraw(canvas);

    if (mDragState != IDLE) {
        // draw the divider over the expanded item
        if (mFirstExpPos != mSrcPos) {
            drawDivider(mFirstExpPos, canvas);
        }
        if (mSecondExpPos != mFirstExpPos && mSecondExpPos != mSrcPos) {
            drawDivider(mSecondExpPos, canvas);
        }
    }

    if (mFloatView != null) {
        // draw the float view over everything
        final int w = mFloatView.getWidth();
        final int h = mFloatView.getHeight();

        int x = mFloatLoc.x;

        int width = getWidth();
        if (x < 0)
            x = -x;
        float alphaMod;
        if (x < width) {
            alphaMod = ((float) (width - x)) / ((float) width);
            alphaMod *= alphaMod;
        } else {
            alphaMod = 0;
        }

        final int alpha = (int) (255f * mCurrFloatAlpha * alphaMod);

        canvas.save();
        // Log.d(tag, "clip rect bounds: " + canvas.getClipBounds());
        canvas.translate(mFloatLoc.x, mFloatLoc.y);
        canvas.clipRect(0, 0, w, h);

        // Log.d(tag, "clip rect bounds: " + canvas.getClipBounds());
        canvas.saveLayerAlpha(0, 0, w, h, alpha, Canvas.ALL_SAVE_FLAG);
        mFloatView.draw(canvas);
        canvas.restore();
        canvas.restore();
    }
}
 
Example 18
Source File: DragSortListView.java    From mobile-manager-tool with MIT License 4 votes vote down vote up
@Override
protected void dispatchDraw(Canvas canvas) {
    super.dispatchDraw(canvas);

    if (mDragState != IDLE) {
        // draw the divider over the expanded item
        if (mFirstExpPos != mSrcPos) {
            drawDivider(mFirstExpPos, canvas);
        }
        if (mSecondExpPos != mFirstExpPos && mSecondExpPos != mSrcPos) {
            drawDivider(mSecondExpPos, canvas);
        }
    }

    if (mFloatView != null) {
        // draw the float view over everything
        final int w = mFloatView.getWidth();
        final int h = mFloatView.getHeight();

        int x = mFloatLoc.x;

        int width = getWidth();
        if (x < 0)
            x = -x;
        float alphaMod;
        if (x < width) {
            alphaMod = ((float) (width - x)) / ((float) width);
            alphaMod *= alphaMod;
        } else {
            alphaMod = 0;
        }

        final int alpha = (int) (255f * mCurrFloatAlpha * alphaMod);

        canvas.save();
        // Log.d("mobeta", "clip rect bounds: " + canvas.getClipBounds());
        canvas.translate(mFloatLoc.x, mFloatLoc.y);
        canvas.clipRect(0, 0, w, h);

        // Log.d("mobeta", "clip rect bounds: " + canvas.getClipBounds());
        canvas.saveLayerAlpha(0, 0, w, h, alpha, Canvas.ALL_SAVE_FLAG);
        mFloatView.draw(canvas);
        canvas.restore();
        canvas.restore();
    }
}
 
Example 19
Source File: FancyIndexer.java    From FancyListIndexer with Apache License 2.0 4 votes vote down vote up
@Override
	protected void onDraw(Canvas canvas) {
		
		// 控件宽高
	    int height = getHeight();
	    int width = getWidth();

	    // 单个字母高度
	    float singleHeight = height / (float)ConstChar.length;
	    
	    int workHeight = 0;
	    
	    if( mAlpha == 0 )
	    	return;
	    
	    mPaint.reset();
	    
	    int saveCount = 0;
	    
	    if( mHideAnimation )
	    {
	    	saveCount = canvas.save();
	    	canvas.saveLayerAlpha( 0, 0, width, height, mAlpha, Canvas.ALL_SAVE_FLAG );
	    }
	    
	    for(int i=0;i<ConstChar.length;i++) {
	    	
	       mPaint.setColor(mFontColor);
	       mPaint.setAntiAlias(true);
	       
	       float xPos = width - mWidthOffset;
	       float yPos = workHeight + singleHeight/2;
	       
	       //float adjustX = adjustXPos( yPos, i == mChooseIndex );
	       // 根据当前字母y的位置计算得到字体大小
	       int fontSize = adjustFontSize(i, yPos ); 
	       mPaint.setTextSize(fontSize);
    	   
	       // 添加一个字母的高度
	       workHeight += singleHeight;
	       
	       // 绘制字母
	       drawTextInCenter(canvas, ConstChar[i], xPos + ajustXPosAnimation(i,  yPos )  , yPos );
	       
	       // 绘制的字母和当前触摸到的一致, 绘制红色被选中字母
	       if(i == mChooseIndex) {
	    	   mPaint.setColor( mTipFontColor );
	    	   mPaint.setFakeBoldText(true);
	    	   mPaint.setTextSize( mTipFontSize );
	    	   yPos = mTouch.y;
	    	   
	    	   float pos = 0;
	    	   
	    	   if( mAnimating || mHideAnimation ) {
	    		   pos = mLastFucusPostion.x;
	    		   yPos = mLastFucusPostion.y;
	    	   } else {
	    		   pos = xPos + ajustXPosAnimation(i, yPos ) - mAdditionalTipOffset;
	    		   mLastFucusPostion.x = pos;
	    		   mLastFucusPostion.y = yPos;
	    	   }
	    		   
	    	   drawTextInCenter(canvas, ConstChar[i], pos, yPos );
//	    	   mPaint.setStrokeWidth(5);
//	    	   canvas.drawLine(0, yPos, width, yPos, mPaint);
	       }
	       mPaint.reset();
	    }
	    
	    if( mHideAnimation ) 
	    {
	    	canvas.restoreToCount(saveCount);
	    }
	   
	}
 
Example 20
Source File: PXShape.java    From pixate-freestyle-android with Apache License 2.0 4 votes vote down vote up
/**
 * Inner render the canvas.
 * 
 * @param canvas
 */
private void innerRender(Canvas canvas) {
    if (!transform.isIdentity()) {
        // apply transform
        canvas.concat(transform);
    }

    // apply clipping path, if we have one
    if (clippingPath != null) {
        canvas.clipPath(clippingPath.getPath());
    }

    // setup transparency layer
    int alphaSaveCount = -1;
    if (opacity < 1.0F) {
        int alpha = (int) (opacity * 255);
        alphaSaveCount = canvas.saveLayerAlpha(new RectF(canvas.getClipBounds()), alpha,
                Canvas.HAS_ALPHA_LAYER_SAVE_FLAG);
    }

    // render content
    if (getPath() != null) {
        // Apply shadow outset
        if (shadow != null) {
            shadow.applyOutsetToPath(path, canvas);
        }
        // Set fill
        if (fillColor != null) {
            Paint fillPaint = ObjectPool.paintPool.checkOut();
            fillPaint.setStyle(Style.FILL);
            fillColor.applyFillToPath(path, fillPaint, canvas);
            // Check the paint back into the pool
            ObjectPool.paintPool.checkIn(fillPaint);
        }
        // Apply shadow insets
        if (shadow != null) {
            shadow.applyInsetToPath(path, canvas);
        }
        // Set stroke
        if (stroke != null) {
            // Passing a null paint. This will eventually be rendered as new
            // Paint with Paint.ANTI_ALIAS_FLAG.
            stroke.applyStrokeToPath(path, null, canvas);
        }
    }

    // Draw the children
    renderChildren(canvas);

    if (opacity < 1.0F) {
        canvas.restoreToCount(alphaSaveCount);
    }
    // Release the path after the rendering. Usually, this method will
    // render into a Picture, so no new Path will be created, unless a
    // complete redraw is performed after a Picture disposal.
    ObjectPool.pathPool.checkIn(path);
    path = null;
}