28 Şubat 2018 Çarşamba

Android - LightingColorFilter example and BitmapShader example, how to change BitmapShader color

Paint drawPaint;
drawPaint = new Paint(); 

drawPaint.setColor(paintColor);

drawPaint.setAntiAlias(true);
drawPaint.setStrokeWidth(brushSize);
drawPaint.setStyle(Paint.Style.STROKE);
drawPaint.setStrokeJoin(Paint.Join.ROUND);
drawPaint.setStrokeCap(Paint.Cap.ROUND);


drawPaint.setShader(new BitmapShader(shaderBitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT));
ColorFilter filter = new LightingColorFilter(paintColor , 0x00000000 );
drawPaint.setColorFilter(filter);
Output :

--------------------------------------------------------------------------------------
https://code.tutsplus.com/series/create-a-drawing-app-on-android--cms-704
https://github.com/SueSmith/android-drawing-app
I started from the above codes.
I enhanced the above codes and used LightingColorFilter and BitmapShader. Here is my custom View class :

public class DrawingView extends View
{
    //drawing path    private Path drawPath;
    //drawing and canvas paint    private Paint drawPaint, canvasPaint;
    //initial color    private int paintColor = 0xFF660000;
    //canvas    private Canvas drawCanvas;
    //canvas bitmap    private Bitmap canvasBitmap, shaderBitmap;



    private float brushSize, lastBrushSize; 

    private boolean erase=false;

    public DrawingView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setupDrawing();
        shaderBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pattern);
    }


    private void setupDrawing()
    {
        brushSize = getResources().getInteger(R.integer.medium_size);
        lastBrushSize = brushSize;

        //get drawing area setup for interaction        drawPath = new Path(); 
        drawPaint = new Paint(); 

        drawPaint.setColor(paintColor);

        drawPaint.setAntiAlias(true);
        drawPaint.setStrokeWidth(brushSize);
        drawPaint.setStyle(Paint.Style.STROKE);
        drawPaint.setStrokeJoin(Paint.Join.ROUND);
        drawPaint.setStrokeCap(Paint.Cap.ROUND);

        canvasPaint = new Paint(Paint.DITHER_FLAG);
    }

    // Bitmap ve Canvas objects are created here    @Override    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        canvasBitmap = Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_8888);
        drawCanvas =  new Canvas(canvasBitmap);
    }

    // invalidate() invokes onDraw()    @Override    protected void onDraw(Canvas canvas) {
        canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
        canvas.drawPath(drawPath, drawPaint);
    }

    @Override    public boolean onTouchEvent(MotionEvent event) {
        float touchX = event.getX();
        float touchY = event.getY();

        switch(event.getAction())
        {
            // When the user touches the View, we move to that position to start drawing.            case MotionEvent.ACTION_DOWN:
                drawPath.moveTo(touchX,touchY);
                break;
            // When they move their finger on the View, we draw the path along with their touch.            case MotionEvent.ACTION_MOVE:
                drawPath.lineTo(touchX, touchY);
                break;
            // When they lift their finger up off the View, we draw the Path and reset it for the next drawing operation.            case MotionEvent.ACTION_UP:
                drawCanvas.drawPath(drawPath, drawPaint);
                drawPath.reset();
                break;
            default:
                return false;
        }

        invalidate(); // Calling invalidate will cause the onDraw method to execute.        return true;
    }

    public void setColor(String newColor){
        paintColor = Color.parseColor(newColor);
        drawPaint.setColor(paintColor);
        ColorFilter filter = new LightingColorFilter(paintColor , 0x00000000 );
        drawPaint.setColorFilter(filter);
        invalidate();
    }

    public void setBrushSize(float newSize){
        //update size        float pixelAmount = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                newSize, getResources().getDisplayMetrics()); 
        brushSize = pixelAmount;
        drawPaint.setStrokeWidth(brushSize);
    }

    public void setLastBrushSize(float lastSize){
        lastBrushSize=lastSize;
    }
    public float getLastBrushSize(){
        return lastBrushSize;
    }

    public void setErase(boolean isErase)
    {
        //set erase true or false        erase=isErase;
        if(erase)
            drawPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        else            drawPaint.setXfermode(null);
    }

    public void startNew(){
        drawCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
        invalidate();
    }

    public void setBitmapShader(){
        drawPaint.setShader(new BitmapShader(shaderBitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT));
        ColorFilter filter = new LightingColorFilter(paintColor , 0x00000000 );
        drawPaint.setColorFilter(filter);
    }
}


Hiç yorum yok:

Yorum Gönder