在android中触摸浮雕位图

时间:2015-01-09 07:47:58

标签: android bimap emboss

我正在开发颜色启动应用。其中我使用手指画。

现在我想在触摸事件上压缩位图。我在这里有一个演示,当我应用浮雕然后它绘制红色的浮雕路径,但我想在触摸后压印位图。

private Path mPath;
private MaskFilter mEmboss;

public void init(){
    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setDither(true);
    mPaint.setColor(color.RED);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(20);
    mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 }, 0.4f, 6, 3.5f);
}



// on click event 
    switch (item.getItemId()) {
        case EMBOSS_MENU_ID:                        
            mPaint.setMaskFilter(mEmboss);
    }



// View Class method

@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);           
    canvas.drawPath(mPath, mPaint);
    canvas.drawPath(circlePath, circlePaint);
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
    float x = ev.getX();
    float y = ev.getY();
    invalidate();
    return true;
}

1 个答案:

答案 0 :(得分:2)

我最终找到了解决方案:

将BitmapShader与相同的位图一起使用

private Path mPath;
private MaskFilter mEmboss;

public void init(){
    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setDither(true);
    mPaint.setColor(color.RED);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(20);
BitmapShader fillBMPshader = new BitmapShader(bm_original, Shader.TileMode.MIRROR, Shader.TileMode.CLAMP);
mPaint.setShader(fillBMPshader);
    mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 }, 0.4f, 6, 3.5f);
}    

// onclick event 
    switch (item.getItemId()) {
        case EMBOSS_MENU_ID:                        
            mPaint.setMaskFilter(mEmboss);
    }

// View Class method

@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);           
    canvas.drawPath(mPath, mPaint);
    canvas.drawPath(circlePath, circlePaint);
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
    float x = ev.getX();
    float y = ev.getY();
    invalidate();
    return true;
}