how to draw path into the view in android
Hi i need to draw a path simple lines to the canvas it should write out red rectangle with white lines.but it do not draw ou开发者_开发知识库t nothing what m i missing. my code:
Canvas canvas = new Canvas();
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.RED);
canvas.drawPaint(paint);
Path path = new Path();
//canvas.drawColor(Color.CYAN);
for (int i = 5; i < 50; i++) {
path.moveTo(4, i-1);
path.lineTo(4, i);
}
path.close();
paint.setStrokeWidth(3);
paint.setPathEffect(null);
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.STROKE);
canvas.drawPath(path, paint);
for (int i = 0; i < 3; i++) {
View iview = inflater.inflate(R.layout.linear_layout, null);
if(i == 0){
iview.findViewById(R.id.imageView1).setBackgroundResource(R.drawable.distspeed);
}
if(i == 1){
iview.findViewById(R.id.imageView1).setBackgroundResource(R.drawable.hxmdist);
}
if(i == 2){
iview.findViewById(R.id.imageView1).setBackgroundResource(R.drawable.hxmspeeed);
}
iview.draw(canvas);
realViewSwitcher.addView(iview);
}
i only post the working code:
modified activity:
for (int i = 0; i < 2; i++) {
MyView mView = new MyView(this,i);
realViewSwitcher.addView(mView);
}
myview:
public class MyView extends View {
public MyView(Context context, int kolki) {
super(context);
if (kolki == 0){
this.setBackgroundResource(R.drawable.distspeed);
}
if (kolki == 1){
this.setBackgroundResource(R.drawable.hxmdist);
}
}
public void setBackgroundResource (int resid){
super.setBackgroundResource(resid);
}
public void onDraw(Canvas c){
super.onDraw(c);
Paint paint = new Paint();
Path path = new Path();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.TRANSPARENT);
c.drawPaint(paint);
for (int i = 50; i < 100; i++) {
path.moveTo(i, i-1);
path.lineTo(i, i);
}
path.close();
paint.setStrokeWidth(3);
paint.setPathEffect(null);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
c.drawPath(path, paint);
}
}
精彩评论