布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<RelativeLayout
android:id="@+id/camera_preview"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.example.drawingapp.CameraView
android:id="@+id/cameraView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="152dp"
android:contentDescription="@string/app_name"
android:src="@drawable/ic_launcher" />
<Button
android:id="@+id/button_capture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="@string/capture" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="152dp"
android:text="@string/app_name" />
</RelativeLayout>
自定义视图类
public class CameraView extends View {
private Paint p;
int width = 0;
int height = 0;
int pass = 0;
int xpos = 0;
int ypos = 0;
public CameraView(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 CameraView(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 CameraView(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();
xpos = width / 5;
ypos = height / 5;
for (int i = 0; i < 5; i++) {
p.setColor(Color.argb(100, 255, 255, 255));
canvas.drawLine(xpos + (xpos * i), 0, xpos + (xpos * i),
height, p);
// canvas.drawLine(startX, startY, stopX, stopY, paint)
}
p.setStyle(Style.STROKE);
for (int i = 0; i < 5; i++) {
p.setColor(Color.argb(100, 255, 255, 255));
canvas.drawLine(0, (ypos * pass) + 5, width, (ypos * pass) + 5,
p);
pass++;
}
}
public void hide(){
Visibility visibility = new Visibility();
visibility.
}
}
活动课
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}