2014-12-02 10 views
0

У меня есть холст, но по какой-то причине это не заполняет весь экран. На экране появляется белая рамка. Мне нужно, чтобы черный фон холста заполнил весь экран.Как я могу заполнить весь экран холстом

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

Edit: Это расположение.

<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> 
+0

показать файл макета (activity_main) ... –

+0

Ваш вопрос о сокрытии «Pruebas_canvas_2» название? Это о панели действий Android, и вы можете скрыть это, используя следующий код в вашем ** «методе OnCreate» ** 'getWindow(). RequestFeature (Window.FEATURE_ACTION_BAR); getActionBar(). Hide(); ' – TeachMeJava

ответ

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> 
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> 
Смежные вопросы