简体   繁体   中英

Why won't my canvas show up (Android)?

I'm writing a program that is supposed to draw a line in a canvas at the bottom of the screen below a button, etc. My layout shows up, but the line doesn't. Anyone know why my canvas doesn't show up?

Here is the code:

public class Vectors extends Activity{

VectorsView vectorsView;
LinearLayout l;

public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.vectors);        
    vectorsView = new VectorsView(this);
    l = (LinearLayout) findViewById(R.id.llCanvasV);
    l.addView(vectorsView); 

    .......
}

public class VectorsView extends View{

    public VectorsView(Context context) {
        super(context);
    }
    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);       

        Paint paint = new Paint();
        paint.setColor(Color.WHITE);

        canvas.drawLine(0, 0, 100, 100, paint);
        vectorsView.draw(canvas);
        vectorsView.setLayoutParams(new LayoutParams(25, 25)); 
    }
}

Here is the xml:

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"
     android:background="@drawable/background" 
     android:orientation="vertical">

     <ImageView android:layout_height="wrap_content" 
        android:src="@drawable/vectors" 
        android:layout_width="wrap_content"
        android:layout_gravity="center"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:adjustViewBounds="true">
     </ImageView>
     <LinearLayout android:layout_height="wrap_content" 
        android:layout_width="match_parent"
        android:orientation="horizontal">
        <Button android:text="Choose Program" 
            android:id="@+id/bChsProgV" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="97dp"
            android:adjustViewBounds="true">
        </Button>
        <ImageButton android:layout_height="wrap_content" 
            android:src="@drawable/help" 
            android:id="@+id/ibHelpV" 
            android:layout_width="wrap_content"
            android:layout_marginLeft="65dp"
            android:background="@null" 
            android:layout_marginTop="10dp">
        </ImageButton>
     </LinearLayout>
     <LinearLayout android:id="@+id/llCanvasV" 
        android:layout_height="wrap_content" 
        android:layout_width="match_parent">
     </LinearLayout>
</LinearLayout>

onMeasure method missing in VectorsView class. and also you can add VectorsView from Layout by keeping the complete package <package.VectorsView android:id= android:width= android:height= ></package.VectorsView>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM