如何绘制路径到Android中的视图 [英] how to draw path into the view in android
本文介绍了如何绘制路径到Android中的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要绘制路径简洁的线条,应该写出来的红色长方形白色lines.but它不画了什么什么丢失英里的画布。 我的code:
画布油画=新的Canvas();
涂料粉刷=新的油漆();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.RED);
canvas.drawPaint(油漆);
路径path =新路径();
//canvas.drawColor(Color.CYAN);
的for(int i = 5; I< 50;我++){
path.moveTo(4,I-1);
path.lineTo(4,I);
}
path.close();
paint.setStrokeWidth(3);
paint.setPathEffect(空);
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.STROKE);
canvas.drawPath(路径,油漆);
的for(int i = 0;我3;;我++){
查看的iView = inflater.inflate(R.layout.linear_layout,NULL);
如果(我== 0){
iview.findViewById(R.id.imageView1).setBackgroundResource(R.drawable.distspeed);
}
如果(ⅰ== 1){
iview.findViewById(R.id.imageView1).setBackgroundResource(R.drawable.hxmdist);
}
如果(ⅰ== 2){
iview.findViewById(R.id.imageView1).setBackgroundResource(R.drawable.hxmspeeed);
}
iview.draw(画布);
realViewSwitcher.addView(iView中);
}
解决方案
我只张贴工作code:
修改活动:
的for(int i = 0;我2;我++){
MyView的MVIEW =新的MyView的(这一点,我);
realViewSwitcher.addView(MVIEW);
}
MyView的:
公共类MyView的扩展视图{
公共MyView的(上下文的背景下,INT kolki){
超(上下文);
如果(kolki == 0){
this.setBackgroundResource(R.drawable.distspeed);
}
如果(kolki == 1){
this.setBackgroundResource(R.drawable.hxmdist);
}
}
公共无效setBackgroundResource(INT渣油){
super.setBackgroundResource(渣油);
}
公共无效的OnDraw(帆布C){
super.onDraw(C);
涂料粉刷=新的油漆();
路径path =新路径();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.TRANSPARENT);
c.drawPaint(油漆);
的for(int i = 50; I< 100;我++){
path.moveTo(I,I-1);
path.lineTo(I,I);
}
path.close();
paint.setStrokeWidth(3);
paint.setPathEffect(空);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
c.drawPath(路径,油漆);
}
}
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 out 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);
}
}
这篇关于如何绘制路径到Android中的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文