2012-08-03 48 views
2

我寫下面的代碼,但輸出什麼也沒有顯示。沒有任何異常拋出,並且沒有線正在使用此代碼進行繪製。請幫我,我錯了嗎?我的活動沒有顯示行

主類

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    View custom_view = (View) findViewById(R.id.custom_view); 

    Paint p = new Paint(); 
    p.setColor(Color.BLACK); 
    p.setStyle(Paint.Style.STROKE); 
    p.setStrokeWidth((float)5); 
    Canvas canvas = new Canvas(); 
    canvas.drawColor(Color.BLUE); 

    canvas.drawLine((float)0, (float)0, (float)100, (float)100, p); 
    custom_view.draw(canvas); 

} 

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#00000010" > 

<View 
    android:id="@+id/custom_view" 
    android:background="#cccccc" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" /></RelativeLayout> 

回答

0

佈局

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal" > 

    <RelativeLayout 
     android:id="@+id/camera_preview" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

     <com.example.drawingapp.CameraView 
      android:id="@+id/cameraView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentTop="true" /> 

    </RelativeLayout> 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginLeft="20dp" 
     android:layout_marginTop="152dp" 
     android:contentDescription="@string/app_name" 
     android:src="@drawable/ic_launcher" /> 

    <Button 
     android:id="@+id/button_capture" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:text="@string/capture" /> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginTop="152dp" 
     android:text="@string/app_name" /> 

</RelativeLayout> 

定製視圖類

public class CameraView extends View { 

    private Paint p; 
    int width = 0; 
    int height = 0; 
    int pass = 0; 
    int xpos = 0; 
    int ypos = 0; 

    public CameraView(Context context) { 
     super(context); 
     // TODO Auto-generated constructor stub 
     p = new Paint(); 
     p.setColor(Color.WHITE); 
     p.setStyle(Paint.Style.STROKE); 
     p.setStrokeWidth(1); 
     p.setAntiAlias(true); 
    } 

    public CameraView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     p = new Paint(); 
     p.setColor(Color.WHITE); 
     p.setStyle(Paint.Style.STROKE); 
     p.setStrokeWidth(1); 
     p.setAntiAlias(true); 
    } 

    public CameraView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     p = new Paint(); 
     p.setColor(Color.WHITE); 
     p.setStyle(Paint.Style.STROKE); 
     p.setStrokeWidth(1); 
     p.setAntiAlias(true); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     // TODO Auto-generated method stub 
     super.onDraw(canvas); 
     width = getWidth(); 
     height = getHeight(); 
     xpos = width/5; 
     ypos = height/5; 
     for (int i = 0; i < 5; i++) { 

      p.setColor(Color.argb(100, 255, 255, 255)); 
      canvas.drawLine(xpos + (xpos * i), 0, xpos + (xpos * i), 
        height, p); 
      // canvas.drawLine(startX, startY, stopX, stopY, paint) 

     } 
     p.setStyle(Style.STROKE); 
     for (int i = 0; i < 5; i++) { 
      p.setColor(Color.argb(100, 255, 255, 255)); 
      canvas.drawLine(0, (ypos * pass) + 5, width, (ypos * pass) + 5, 
        p); 
      pass++; 

     } 
    } 
    public void hide(){ 
     Visibility visibility = new Visibility(); 
     visibility. 

    } 

} 

活動類

public class MainActivity extends Activity { 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_main); 


} 
+0

所以基本上你是說你放置自定義視圖的RelativeLayout不應該是ROOT佈局,而是在填充父項的另一個佈局的內部,並且從那裏獲得視圖的寬度和高度? – marienke 2013-06-21 09:09:14

1

我覺得現在的問題是,你需要設置一個特定的與和高度的View

試試類似於:

public class CustomView extends View{ 

private Paint p; 

public CustomView(Context context, AttributeSet attrs) { 
    super(context, attrs); 

    init();   
} 

public CustomView(Context context){ 
    super(context);  

    init(); 
} 

private void init() {  

    p = new Paint(); 
    p.setColor(Color.WHITE); 
    p.setStyle(Paint.Style.STROKE); 
    p.setStrokeWidth(5); 
    p.setAntiAlias(true); 
} 

@Override 
protected void onDraw(Canvas canvas) { 

    super.onDraw(canvas); 

    canvas.drawLine(0, 0, 100, 100, p); 
} 
} 

您的活動類:

RelativeLayout mLayout; 
CustomView mView; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main);  

    mLayout = (RelativeLayout)findViewById(R.id.rootLayout); 
    mView = new CustomView(this); 
    mLayout.addView(mView); 
} 

並把一個ID的rootLayout相對佈局。

<?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/rootLayout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<View 
    android:id="@+id/custom_view" 
    android:background="#000000" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"  
/> 
</RelativeLayout> 

希望有所幫助。定義特定寬度後

+0

仍無變化和高度... – 2012-08-03 01:17:43

+0

我想你會更好的創建一個擴展視圖的類,然後重寫onDraw方法並將其添加到您的佈局。 – 0gravity 2012-08-03 01:28:49

+0

看看我的編輯,可能有幫助。 – 0gravity 2012-08-03 01:44:49