我无法将所有部分组合在一起。我有3个类:DefaultActivity
扩展Activity
,MyView
扩展SurfaceView
,ResistorView
扩展View
。
我想知道的是每次点击bAddResistor
课程按钮DefaultActivity
时如何画一个电阻?以下是3个班级的部分内容:
DefaultActivity.java
public class DefaultActivity extends Activity {
//MyView myView;
TextView myText;
ResistorView myResistor;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//myView = (MyView) findViewById(R.id.my_view);
//myView = new MyView(this, null);
//myResistor = new ResistorView(this);
//myView.setBackgroundColor(Color.WHITE);
//myView.setScrollContainer(true);
setContentView(R.layout.main);
final Button bAddResistor = (Button) findViewById(R.id.bAdd);
bAddResistor.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
Global.numberOfResistors++;
Log.d("ButtonADD", "Button Add has been clicked");
}
});
}
}
MyView.java
public class MyView extends SurfaceView implements SurfaceHolder.Callback {
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
getHolder().addCallback(this);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
Canvas c = holder.lockCanvas();
onDraw(c);
holder.unlockCanvasAndPost(c);
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
}
ResistorView.java
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();
mPaint.setAntiAlias(true);
mPaint.setStrokeWidth(2);
mPaint.setColor(-7829368);
mPaint.setStyle(Paint.Style.STROKE);
//...Your code here to set up the path,
//...allocate objects here, never in the drawing code.
mSymbol.moveTo(0.0F, 0.0F);
mSymbol.lineTo(0.0F, 50.0F);
mSymbol.lineTo(16.666666F, 58.333332F);
mSymbol.lineTo(-16.666666F, 75.0F);
mSymbol.lineTo(16.666666F, 91.666664F);
mSymbol.lineTo(-16.666666F, 108.33333F);
mSymbol.lineTo(16.666666F, 124.99999F);
mSymbol.lineTo(-16.666666F, 141.66666F);
mSymbol.lineTo(0.0F, 150.0F);
mSymbol.lineTo(0.0F, 200.0F);
mSymbol.offset(100.0F, 20.0F);
}
//...Override onMeasure()...
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//Use this method to tell Android how big your view is
setMeasuredDimension(50, 50);
}
//...Override onDraw()...
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawPath(mSymbol, mPaint);
}
}
和main.xml布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:ignore="HardcodedText" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RightLayout"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:background="#00000000"
android:orientation="vertical" >
<Button
android:id="@+id/bAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add RES" />
<Button
android:id="@+id/bDeleate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Del RES" />
</LinearLayout>
<com.defaul.android.MyView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_toLeftOf="@id/RightLayout"
android:background="#ff000000" />
</RelativeLayout>
如果我将绘图代码放在MyView.onDraw
中它工作正常,则会绘制电阻。我想要的是单击一个按钮并在表面上绘制电阻,所以每次我点击我想要一个电阻添加到表面视图。
可能我正朝着相反的方向解决这个问题。如果有人能指出我正确的方向,我会非常感激。
答案 0 :(得分:2)
由于我找不到用SurfaceView
做我想做的事的方法,我刚创建了一个扩展View的类,然后将该视图添加到我的主布局中。
如果它可能对某人有帮助,下面是我的代码的简化版本:
public class CircuitSolverActivity extends Activity {
/** Called when the activity is first created. */
RelativeLayout mLayout;
MyView mView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mLayout = (RelativeLayout)findViewById(R.id.elementContainer);
}
public void bAddView(View view){
mView = new MyView(context);
mView.setBackgroundColor(Color.TRANSPARENT);
mLayout.addView(mView);
}
MyView
上课:
public class ResistorView extends View {
public ResistorView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public ResistorView(Context context){
super(context);
init();
}
private void init() {
mSymbol = new Path();
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setStrokeWidth(2);
mPaint.setColor(-7829368);
mPaint.setStyle(Paint.Style.STROKE);
mSymbol.moveTo(0.0F, 0.0F);
mSymbol.lineTo(0.0F, 50.0F);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawPath(mSymbol, mPaint);
}