Draw Emoji on canvas using BitmapShader

470 views Asked by At

I'm trying to draw emoji, whenever I touch the screen, I'm using setShader method and BitmapShader to make it but the code below gives this :

enter image description here

public class MainActivity extends Activity implements View.OnTouchListener{

    float downx = 0;
    float downy = 0;
    float upx = 0;
    float upy = 0;
    Canvas canvas;
    Paint paint;
    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Point point = new Point();
        Display cureentDispaly = getWindowManager().getDefaultDisplay();
        cureentDispaly.getSize(point);
        int weidth = point.x;
        int height = point.y;
        Bitmap bitmap = Bitmap.createBitmap(weidth , height, Bitmap.Config.ARGB_8888);
        canvas = new Canvas(bitmap);
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStrokeWidth(50);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setShader(new BitmapShader(BitmapFactory.decodeResource(getResources(),R.drawable.heart), Shader.TileMode.REPEAT, Shader.TileMode.REPEAT));
        paint.setStyle(Paint.Style.STROKE);
        imageView = (ImageView)findViewById(R.id.imageView);
        imageView.setImageBitmap(bitmap);
        imageView.setOnTouchListener(this);
    }
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int action = event.getAction();
        switch (action){
            case MotionEvent.ACTION_DOWN:
                downx = event.getX();
                downy = event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                upx = event.getX();
                upy = event.getY();
                canvas.drawLine(downx, downy, upx, upy, paint);
                imageView.invalidate();
                downx = upx;
                downy = upy;
                break;
            case MotionEvent.ACTION_UP:
                upx = event.getX();
                upy = event.getY();
                canvas.drawLine(downx, downy, upx, upy, paint);
                imageView.invalidate();
                break;
            case MotionEvent.ACTION_CANCEL:
                break;
            default:
                break;

        }
        return true;
    }
}

what I want is something similar to this Draw With Emoji and this :

enter image description here

How I can draw a repetitive emoji without making it cropped

?

0

There are 0 answers