1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
| package fr.yanasoft.axipush;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.support.v7.widget.AppCompatImageView;
/**
* Created by Florian on 08/02/2018.
*/
public class ImageViewPaint extends AppCompatImageView implements View.OnTouchListener {
private Context context;
private Paint paint;
private Canvas canvas;
float dwx, dwy, upx, upy;
float xr, yr;
public ImageViewPaint(Context context) {
super(context);
init(context);
}
public ImageViewPaint(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context);
}
public ImageViewPaint(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
@Override
public void setImageBitmap(Bitmap bm) {
super.setImageBitmap(bm);
xr = (bm.getWidth() - getWidth()) * 0.5f;
yr = (bm.getHeight() - getHeight()) * 0.5f;
Matrix matrix = new Matrix();
matrix.setTranslate(xr, yr);
canvas.setMatrix(matrix);
canvas.setBitmap(bm);
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
int action = motionEvent.getAction();
switch (action)
{
case MotionEvent.ACTION_DOWN:
{
pointerPressed(motionEvent);
break;
}
case MotionEvent.ACTION_MOVE:
{
pointerDragged(motionEvent);
break;
}
case MotionEvent.ACTION_UP:
{
pointerReleased(motionEvent);
break;
}
}
return true;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
@Override
public boolean performClick() {
return super.performClick();
}
private void init(Context c) {
context = c;
paint = new Paint();
canvas = new Canvas();
paint.setAntiAlias(true);
paint.setDither(true);
paint.setFilterBitmap(true);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeWidth(10);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setColor(context.getColor(R.color.ax_green));
this.setOnTouchListener(this);
}
private void pointerPressed(MotionEvent motionEvent)
{
dwx = motionEvent.getX();
dwy = motionEvent.getY();
}
private void pointerDragged(MotionEvent motionEvent)
{
upx = motionEvent.getX();
upy = motionEvent.getY();
this.repaint();
dwx = upx;
dwy = upy;
}
private void pointerReleased(MotionEvent motionEvent)
{
upx = motionEvent.getX();
upy = motionEvent.getY();
this.repaint();
}
private void repaint()
{
this.canvas.drawLine(dwx, dwy, upx, upy, paint);
this.invalidate();
}
} |
Partager