SeniorUI0506_Xfermode实现书柜图书高亮

高级UI汇总​​​​​​​
SeniorUI05_Paint_Xfermode模式
##1 Effect Picture
这里写图片描述
##2 Demo
Senior05_LightBookActivity
##3 Requirement
书柜中某个位置的数据高亮显示
##4 Theory

  • 自定义View
  • Xfermode合成两张图片(一张显示书柜内容,一张高亮效果图片)
  • 注意此处为了做高亮效果,位置用死的图片,实际运用中,可以用创建一张和书柜一样大小的Bitmap,然后在Bitmap中画高亮区域加上本文案例内容即可

##5 Core Code

public class LightBookView_LIGHTEN extends View {
    private Paint mBitPaint;
    private Bitmap BmpDST,BmpSRC;

    public LightBookView_LIGHTEN(Context context, AttributeSet attrs) {
        super(context, attrs);
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        mBitPaint = new Paint();
        BmpDST = BitmapFactory.decodeResource(getResources(), R.drawable.book_bg,null);
        BmpSRC = BitmapFactory.decodeResource(getResources(),R.drawable.book_light,null);
    }

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

        int layerId = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG);

        canvas.drawBitmap(BmpDST,0,0,mBitPaint);
        mBitPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.LIGHTEN));
        canvas.drawBitmap(BmpSRC,0,0,mBitPaint);

        mBitPaint.setXfermode(null);
        canvas.restoreToCount(layerId);

    }
}

猜你喜欢

转载自blog.csdn.net/baopengjian/article/details/80814845
今日推荐