i want to be able to rotate & zoom/scale my bitmap with fingers, i already look for all of the example project for this, but all of them aren't match with my code. This is my code for my onTouchListener.
public class MyView extends View {
private float mX, mY;
private float XText, YText;
private float Textpixel;
private static final float TOUCH_TOLERANCE = 2;
//image
float xImage = 200;
float yImage = 200;
public MyView(Context c) {
super(c);
mCanvas = new Canvas();
mPath = new Path();
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh );
}
@Override
protected void onDraw(Canvas canvas) {
if ( layouttype == "small"){
Textpixel = 16f;
}else if(layouttype == "normal"){
Textpixel = 16f;
}else if (layouttype == "large"){
Textpixel = 18f;
}else {
Textpixel = 20f;
}
if(bmImgAttach != null) {
canvas.drawBitmap(bmImgAttach, xImage, yImage, null);
}
}
public void touch_start(float x, float y, MotionEvent event) {
if (isImage) {
if(isTouchImage) {
xImage = event.getX();
yImage = event.getY();
}
}
}
private void touch_move(float x, float y, MotionEvent event) {
if(isImage) {
if(isTouchImage) {
xImage = event.getX();
yImage = event.getY();
}
}
}
private void touch_up(float x, float y, MotionEvent event) {
if(isImage) {
if(isTouchImage) {
xImage = event.getX();
yImage = event.getY();
}
}
}
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent event) {
final float x = event.getX();
final float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touch_start(x, y, event);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y, event);
invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up(x, y, event);
invalidate();
break;
}return true;
}
}
i created a different method for each event like:
- touch_start method for ACTION_DOWN event
- touch_move method for ACTION_MOVE event
- touch_up method for ACTION_UP event