2

我很难把所有的部分放在一起。我有 3 个类:DefaultActivity扩展ActivityMyView扩展SurfaceViewResistorView扩展 a 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");
            }
        });
    }
}

我的视图.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    
    }
}

电阻视图.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它工作正常,则电阻器被绘制。我想要的是单击一个按钮并在表面上绘制电阻器,因此每次单击时我都希望将一个电阻器添加到表面视图中。

可能我正在以相反的方向解决这个问题。如果有人能指出我正确的方向,我将不胜感激。

4

1 回答 1

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);
}
于 2012-08-14T01:45:47.060 回答