如何用画布填充所有屏幕

时间:2014-12-02 15:26:32

标签: android colors background android-canvas

我有画布,但由于某种原因,这不会填满所有屏幕。屏幕上出现白色边框。我需要在整个屏幕上填充黑色画布背景。

enter image description here

我该如何解决这个问题?

这是我的实际代码:

package com.simmaro.pruebas_canvas_2;

import android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Duda resuelta en http://stackoverflow.com/questions/27250143/how-to-insert-an-image-with-canvas/27251423#27251423     
        RelativeLayout layout1 = (RelativeLayout) findViewById(R.id.layout1);
        Lienzo image = new Lienzo(this);
        //Finalmente se añade el Lienzo al layout
        layout1.addView(image);     
    }

}


//Modo 2
class Lienzo extends View {
    private Drawable theimage;

    public Lienzo(Context context) {
        super(context);
    }

    protected void onDraw(Canvas canvas) {
        //Width
        int ancho=canvas.getWidth();
        //Height
        int alto=canvas.getHeight();
        //Canvas background color
        canvas.drawRGB(0, 0, 0);

        Bitmap bmp=BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
        canvas.drawBitmap(bmp,(ancho-48)/2,(alto-48)/2,null);   
    }
}

编辑:这是布局。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.simmaro.pruebas_canvas_2.MainActivity" >

</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

修改布局如下: 问题是由于填充物没有完全填充

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.simmaro.pruebas_canvas_2.MainActivity" >

</RelativeLayout>

答案 1 :(得分:0)

您的问题来自您的基础RealativaLayout,您设置了填充,因此添加的子视图将尊重它们。删除它们以获得全屏画布:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</RelativeLayout>