我使用SurfaceView
为未来的自定义视图绘制坐标网格和轮廓。当我完成绘制轮廓时,我隐藏了SurfaceView
(mySurfaceView.setVisibility(View.GONE)
)并且此时屏幕溅起了黑色。如何解决这个问题?
Sample project
MySurfaceView
public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback {
float[] gridLines;
Canvas canvas;
PointF oldPos = new PointF();
SurfaceHolder holder;
final int mStep = 15;
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
public MySurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
getHolder().addCallback(this);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(3);
paint.setColor(context.getResources().getColor(R.color.colorAccent));
}
@Override
public boolean onTouchEvent(MotionEvent event) {
PointF newPos = new PointF();
float top;
float left;
float bottom;
float right;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
oldPos.set(event.getX(), event.getY());
break;
case MotionEvent.ACTION_MOVE:
canvas = holder.lockCanvas();
drawGrid(canvas);
newPos.set(event.getX(), event.getY());
top = Math.min(oldPos.y, newPos.y);
left = Math.min(oldPos.x, newPos.x);
bottom = Math.max(oldPos.y, newPos.y);
right = Math.max(oldPos.x, newPos.x);
int l = (int) left / mStep;
l *= mStep;
int r = (int) right / mStep;
r *= mStep;
int t = (int) top / mStep;
t *= mStep;
int b = (int) bottom / mStep;
b *= mStep;
paint.setStrokeWidth(6);
canvas.drawRect(l, t, r, b, paint);
holder.unlockCanvasAndPost(canvas);
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
private void drawGrid(Canvas canvas) {
final Paint paint = new Paint();
canvas.drawColor(Color.WHITE);
paint.setStrokeWidth(2);
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.GRAY);
canvas.drawLines(gridLines, paint);
}
private float[] getLines(int step) {
final List<Float> floatList = new ArrayList<>();
int right = this.getRight();
int bottom = this.getBottom();
for (int x = 0; x < right; x += step) {
floatList.add((float) x);
floatList.add(0f);
floatList.add((float) x);
floatList.add((float) bottom);
}
for (int y = 0; y < bottom; y += step) {
floatList.add(0f);
floatList.add((float) y);
floatList.add((float) right);
floatList.add((float) y);
}
float[] rez = new float[floatList.size()];
for (int k = 0; k < floatList.size(); k++) {
rez[k] = floatList.get(k);
}
return rez;
}
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
holder = surfaceHolder;
gridLines = getLines(mStep);
canvas = surfaceHolder.lockCanvas();
drawGrid(canvas);
surfaceHolder.unlockCanvasAndPost(canvas);
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
canvas = surfaceHolder.lockCanvas();
drawGrid(canvas);
surfaceHolder.unlockCanvasAndPost(canvas);
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
}
}
MainFragment
public class MainFragment extends Fragment {
MySurfaceView mySurfaceView;
public MainFragment() {
// Required empty public constructor
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_main, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mySurfaceView = (MySurfaceView) view.findViewById(R.id.surface_view);
mySurfaceView.setVisibility(View.GONE);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.main_fragment_menu, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.vis:
if (mySurfaceView.getVisibility()==View.VISIBLE) {
item.setIcon(getResources().getDrawable(R.drawable.ic_action_show));
mySurfaceView.setVisibility(View.GONE);
} else {
item.setIcon(getResources().getDrawable(R.drawable.ic_action_hide));
mySurfaceView.setVisibility(View.VISIBLE);
}
break;
}
return super.onOptionsItemSelected(item);
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onStart() {
super.onStart();
FragmentManager fm = getSupportFragmentManager();
MainFragment fragment = new MainFragment();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.container, fragment).commit();
}
}
fragment_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.gvozditskiy.surfaceblinking.MainFragment">
<com.gvozditskiy.surfaceblinking.MySurfaceView
android:id="@+id/surface_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>