Java 是否可以将不同的路径组合到视图或对象中?
我使用不同的路径创建了一个图形,但如何移动整个图形?如何选择并移动它?以下是我的onDraw方法的主要部分:Java 是否可以将不同的路径组合到视图或对象中?,java,android,Java,Android,我使用不同的路径创建了一个图形,但如何移动整个图形?如何选择并移动它?以下是我的onDraw方法的主要部分: Path theSymbol = new Path(); theSymbol.moveTo(0.0F, 0.0F); theSymbol.lineTo(0.0F, 50.0F); theSymbol.lineTo(16.666666F, 58.333332F); theSymbol.lineTo(-16.666666F, 75.0F); theSymbol.lineTo(16.6666
Path theSymbol = new Path();
theSymbol.moveTo(0.0F, 0.0F);
theSymbol.lineTo(0.0F, 50.0F);
theSymbol.lineTo(16.666666F, 58.333332F);
theSymbol.lineTo(-16.666666F, 75.0F);
theSymbol.lineTo(16.666666F, 91.666664F);
theSymbol.lineTo(-16.666666F, 108.33333F);
theSymbol.lineTo(16.666666F, 124.99999F);
theSymbol.lineTo(-16.666666F, 141.66666F);
theSymbol.lineTo(0.0F, 150.0F);
theSymbol.lineTo(0.0F, 200.0F);
theSymbol.offset(100.0F, 20.0F);
canvas.drawPath(theSymbol, paint);
这就是我在屏幕上画电阻的方式(它工作)。现在我想要一种方法,使所有这些路径成为一个对象,一个我可以选择和移动的对象
我一直在看一些项目,如,但我找不到他们是如何做他们的元素图纸
我也搜索过无数次,但我得到的只是“在一条路上移动某物”。Maibe我在寻找错误的东西,或者这不是做这种事情的方式
如果有人能给我指出正确的方向,我将不胜感激 将此绘图代码放入自定义
视图
子类的onDraw()
方法中。然后,可以像框架中的任何其他视图一样,随意使用布局、动画和其他变换在屏幕周围放置图形。比如:
public class ResistorView extends View {
private Path mSymbol;
private Paint mPaint;
//...Override Constructors...
public ResistorView(Context context) {
super(context);
init();
}
public ResistorView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
mSymbol = new Path();
mPaint = new Paint();
//...Your code here to set up the path,
//...allocate objects here, never in the drawing code.
}
//...Override onMeasure()...
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//Use this method to tell Android how big your view is
setMeasuredDimension(width, height);
}
//...Override onDraw()...
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawPath(mSymbol, mPaint);
}
}
有关创建自定义视图的详细信息
HTH天哪,我现在要试试这个!非常感谢你!好的,现在我有了这个类,每次我想在屏幕上创建/绘制一个电阻时,我应该从哪里调用它或其他什么?我有3个类:DefaultActivity.java MyView.java(它扩展了SurfaceView)和ResistorView.java如何实现ResistorView类?