Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 我的活动没有显示线条_Android_Android Activity_Line_Draw - Fatal编程技术网

Android 我的活动没有显示线条

Android 我的活动没有显示线条,android,android-activity,line,draw,Android,Android Activity,Line,Draw,我编写了以下代码,但输出没有显示任何内容。没有任何异常抛出,也没有使用此代码绘制线条。请问我哪里错了 主类 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); View custom_view = (View) findViewById(R.id.

我编写了以下代码,但输出没有显示任何内容。没有任何异常抛出,也没有使用此代码绘制线条。请问我哪里错了

主类

    @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);

}
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);


}
活动\u 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>

我认为问题在于您需要为您的
视图设置一个特定的高度

尝试以下方法:

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>


希望对您有所帮助。

布局

<?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>

我认为您最好只创建一个扩展视图的类,然后重写onDraw方法并将其添加到布局中。我认为您必须在onDraw重写方法中绘制一些内容。在使用Paint.Style.STROKE时尝试设置setTextSize。谢谢Ogravity,这个想法对我帮助很大,但我通过创建自定义视图来实现这一点。下面是解决方案基本上你是说你放置自定义视图的相对位置不应该是根布局,而是另一个布局的内部,填充父布局,从那里你可以得到视图的宽度和高度?