我正在创建一个应用程序,其中显示了一个相机,并在屏幕上显示了一个选项来隐藏和显示屏幕上的网格。我通过 CustomView 创建了这个网格,它工作正常。但是当我隐藏网格并单击显示时,水平线没有显示...请帮助...
自定义视图
<com.***.GridLinesView
android:id="@+id/cameraView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
OnClick 隐藏/取消隐藏按钮
@Override
public void onClick(View v) {
// get an image from the camera
View grid = (View)findViewById(R.id.cameraView1);
if(grid.getVisibility() == View.VISIBLE)
grid.setVisibility(View.INVISIBLE);
else
grid.setVisibility(View.VISIBLE);
}
自定义视图
public class GridLinesView extends View {
private Paint p;
int width = 0;
int height = 0;
int pass = 0;
int xpos = 0;
int ypos = 0;
public GridLinesView(Context context) {
super(context);
// TODO Auto-generated constructor stub
p = new Paint();
p.setColor(Color.WHITE);
p.setStyle(Paint.Style.STROKE);
p.setStrokeWidth(1);
p.setAntiAlias(true);
}
public GridLinesView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
p = new Paint();
p.setColor(Color.WHITE);
p.setStyle(Paint.Style.STROKE);
p.setStrokeWidth(1);
p.setAntiAlias(true);
}
public GridLinesView(Context context, AttributeSet attrs) {
super(context, attrs);
p = new Paint();
p.setColor(Color.WHITE);
p.setStyle(Paint.Style.STROKE);
p.setStrokeWidth(1);
p.setAntiAlias(true);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
width = getWidth();
height = getHeight();
int ratio = 5;
if(width < 300 || height < 300){
ratio = 3;
}
xpos = width / ratio;
ypos = height / ratio;
p.setStyle(Style.STROKE);
for (int i = 0; i < ratio; i++) {
p.setColor(Color.argb(100, 255, 255, 255));
canvas.drawLine(0, (ypos * pass) + ratio, width, (ypos * pass) + ratio, p);
pass++;
}
for (int i = 0; i < ratio; i++) {
p.setColor(Color.argb(100, 255, 255, 255));
canvas.drawLine(xpos + (xpos * i), 0, xpos + (xpos * i), height, p);
}
}
}
任何帮助将是可观的......谢谢。