我正在开发 Android 项目。我有具有片段 PainterFragment 的 ViewPage(活动)。在 PainterFragment 方法 onCreateView 我返回 Painter(getActivity());
在我的 Painter 类中,我绘制对象 (onDraw)。现在我需要刷新视图。我有静态方法刷新(我从 ViewPage 调用),但它不能使非静态函数失效/运行,所以我的视图只有在我点击它时才会刷新,因为 onScale / onTouch。
我应该怎么做才能从 Painter 类刷新视图?当我想刷新它时,也许以编程方式制作 onTouch 事件?
public class Painter extends View {
private static final int INVALID_POINTER_ID = -1;
...
static Context baseContext;
public Painter(Context context, AttributeSet attrs) {
this(context, attrs, 0);
baseContext = getContext();
}
public Painter(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
baseContext = getContext();
mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
}
private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
mScaleFactor *= detector.getScaleFactor();
// Zabezpieczenie przed nieskończonym zoomowaniem
mScaleFactor = Math.max(0.5f, Math.min(mScaleFactor, 10.0f));
invalidate();
return true;
}
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
// Let the ScaleGestureDetector inspect all events.
mScaleDetector.onTouchEvent(ev);
final int action = ev.getAction();
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN: {
final float x = ev.getX();
final float y = ev.getY();
float px = ev.getX() / mScaleFactor + myRect.left;
float py = ev.getY() / mScaleFactor + myRect.top;
mLastTouchX = x;
mLastTouchY = y;
mActivePointerId = ev.getPointerId(0);
for(int i = 0; i<listPointer.size(); i++) {
...
}
break;
}
case MotionEvent.ACTION_MOVE: {
final int pointerIndex = ev.findPointerIndex(mActivePointerId);
final float x = ev.getX(pointerIndex);
final float y = ev.getY(pointerIndex);
// Only move if the ScaleGestureDetector isn't processing a gesture.
if (!mScaleDetector.isInProgress()) {
final float dx = x - mLastTouchX;
final float dy = y - mLastTouchY;
mPosX += dx;
mPosY += dy;
invalidate();
}
mLastTouchX = x;
mLastTouchY = y;
break;
}
case MotionEvent.ACTION_UP: {
mActivePointerId = INVALID_POINTER_ID;
break;
}
case MotionEvent.ACTION_CANCEL: {
mActivePointerId = INVALID_POINTER_ID;
break;
}
case MotionEvent.ACTION_POINTER_UP: {
final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK)
>> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
final int pointerId = ev.getPointerId(pointerIndex);
if (pointerId == mActivePointerId) {
final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
mLastTouchX = ev.getX(newPointerIndex);
mLastTouchY = ev.getY(newPointerIndex);
mActivePointerId = ev.getPointerId(newPointerIndex);
}
break;
}
}
return true;
}
public Painter(Context context) {
this(context, null, 0);
baseContext = getContext();
paint.setColor(Color.BLACK);
refresher();
}
public void refresher() {
if(printTable != null) printTable.clear();
if(listOrders!=null & baseContext!=null) listOrders = Bridge.getOrders(baseContext);
if(listTable!=null) {
for(Table table: listTable) {
printTable.add(table);
}
}
invalidate();
}
public static void refresh() {
if(printTable != null) printTable.clear();
if(listOrders!=null & baseContext!=null) listOrders = Bridge.getOrders(baseContext);
if(listTable!=null) {
for(Table table: listTable) {
printTable.add(table);
}
}
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
...
}
public void Draw(Table item, Canvas canvas)
{
...
}
}
}