package com.besmart.components;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import androidx.annotation.Nullable;
public class TestView extends View {
private static final String TAG = "TestView";
private static final float IMAGE_WIDTH = Util.dp2px(200);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
Bitmap bitmap;
float downX;
float downY;
float offsetX;
float offsetY;
float orgOffsetX;
float orgOffsetY;
int statusBarHeight;
{
bitmap = Util.getImage(getResources(), (int) IMAGE_WIDTH, R.drawable.he);
}
public TestView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
statusBarHeight = Util.getStatusBarHeight(context);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int masked = event.getActionMasked();
int index = event.getActionIndex();
int id = event.getPointerId(index);
switch (masked){
case MotionEvent.ACTION_DOWN:
downX = event.getX();
downY = event.getY();
orgOffsetX = offsetX;
orgOffsetY = offsetY;
break;
case MotionEvent.ACTION_MOVE:
int count = event.getPointerCount();
Log.e(TAG, String.format("onTouchEvent: ACTION_MOVE %d-%d-%d", index, id, count));
offsetX = orgOffsetX + event.getX(count-1) - downX;
offsetY = orgOffsetY + event.getY(count-1) - downY;
invalidate();
break;
case MotionEvent.ACTION_POINTER_DOWN:
Log.e(TAG, String.format("onTouchEvent: ACTION_POINTER_DOWN %d-%d", index, id));
break;
case MotionEvent.ACTION_POINTER_UP:
Log.e(TAG, String.format("onTouchEvent: ACTION_POINTER_UP %d-%d", index, id));
break;
default:break;
}
return true;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(bitmap, offsetX, offsetY, paint);
}
}
评论