android 关于Canvas 画布和使用!!!!!!
我现在使用Canvas画好了一个使用的是继承view在view中的onDraw画的,现在我在另外一个activity的布局文件,引用了view,我在activity中怎么去实例他让他显示在我的布局中的LinearLayout里面,还有就是,在这个类里面我构造了一个方法,需要传5个参数才能画出图我的View如下:XML的代码如下:
private float p1;
private float p2;
private float p3;
private float p4;
private float p5;
public MyViewCanvas(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public void preview(Context context, float p1, float p2, float p3, float p4,
float p5) {
this.p1 = p1;//可玩性
this.p2 = p2;//难度
this.p3 = p3;//画面,音效
this.p4 = p4;//研发
this.p5 = p5;//操控
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//Resources res = getResources();
//Bitmap bitmap=BitmapFactory.decodeResource(res,R.drawable.aaa);
// canvas=new Canvas(bitmap); //创建画布
canvas.drawColor(Color.WHITE);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.FILL);
paint.setStrokeWidth(1);// 画笔的粗细
paint.setAntiAlias(true);// 去锯齿
Paint paint1 = new Paint();
paint1.setColor(Color.RED);
paint1.setStyle(Paint.Style.FILL);
paint1.setStrokeWidth(1);// 画笔的粗细
paint1.setAntiAlias(true);// 去锯齿
// 定义一个Path封闭一个五边形
//外围点
canvas.drawPoint(200, 200, paint);
canvas.drawPoint(150, 250, paint);
canvas.drawPoint(250, 250, paint);
canvas.drawPoint(170, 300, paint);
canvas.drawPoint(230, 300, paint);
//中心点线
canvas.drawLine(200, 250, 200, 200, paint);
canvas.drawLine(200, 250, 150, 250, paint);
canvas.drawLine(200, 250, 250, 250, paint);
canvas.drawLine(200, 250, 170, 300, paint);
canvas.drawLine(200, 250, 230, 300, paint);
//外围线
canvas.drawLine(200, 200, 150, 250, paint);
canvas.drawLine(150, 250, 170, 300, paint);
canvas.drawLine(170, 300, 230, 300, paint);
canvas.drawLine(230, 300, 250, 250, paint);
canvas.drawLine(250, 250, 200, 200, paint);
//填充点对点区域
Path path1=new Path();
float x1=(250-200)/p1;
if(p1==5){
x1=0;
}
if(p1==0){
x1=50;
}
float x2=(200-150)/p2;
if(p2==0){
x2=50;
}
if(p2==5){
x2=0;
}
float x3=(250-200)/p3;
if(p3==5){
x3=0;
}
if(p3==0){
x3=50;
}
float x4_y=(300-250)/p4;
float x4_x=(200-170)/p4;
if(p4==0){
x4_y=50;
x4_x=30;
}
if(p4==5){
x4_y=0;
x4_x=0;
}
float x5_y=(300-250)/p5;
float x5_x=(230-200)/p5;
if(p5==5){
x5_y=0;
x5_x=0;
}
if(p5==0){
x5_y=50;
x5_x=30;
}
path1.moveTo(200,250-(50-x1));
path1.lineTo(200-(50-x2), 250);
path1.lineTo(200-(30-x4_x), 250+(50-x4_y));
path1.lineTo(200+(30-x5_x), 250+(50-x5_y));
path1.lineTo(200+(50-x3), 250);
canvas.drawText("aa:"+p1, 200, 190, paint);
canvas.drawText("bb:"+p2, 100, 250, paint);
canvas.drawText("cc:"+p4, 170, 320, paint);
canvas.drawText("dd:"+p5, 240, 310, paint);
canvas.drawText("ee:"+p3, 260, 250, paint);
// 根据path进行绘制,绘制出五边形
canvas.drawPath(path1, paint1);
}
<LinearLayout
android:id="@+id/row3"
android:layout_width="fill_parent"
android:layout_height="100dip"
android:orientation="vertical" >
<com.jwd.myview.MyViewCanvas
android:id="@+id/views_s_s"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView4"
android:layout_alignParentRight="true"
android:src="@drawable/ic_launcher" />
</LinearLayout>
现在就是在这个Activity中根据参数在LinearLayout显示出来,求大神帮帮忙!!!!!
--------------------编程问答-------------------- 要不和主线程冲突的那样,自己顶下!!!!!
补充:移动开发 , Android