如何插入用帆布的形象? [英] How to insert an image with canvas?

查看:165
本文介绍了如何插入用帆布的形象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试在Android的帆布插入一个简单的图像与下一个code,但是当我尝试在智能手机来执行,只出现一个白色的屏幕没有图像。

I´m trying to insert a simple image with canvas in Android with the next code, but when i try to execute in the smartphone, only appears a white screen without image.

import android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
    }

}

class Lienzo extends View {
    private Drawable theimage;

    public Lienzo(Context context) {
        super(context);
        Resources res = context.getResources();
        theimage = res.getDrawable(R.drawable.ic_launcher);
        theimage.setBounds(30, 30, 200, 200);
    }

    protected void onDraw(Canvas canvas) {
        theimage.draw(canvas);
    }
}

我敢肯定这是一个愚蠢,但是,what's错在code?
道理很简单,在画布中插入图像。

I´m sure this is a foolishness but, what´s wrong in the code? The idea is simple, insert an image in the canvas.

推荐答案

我找到了答案:
我添加了一个视图(Lienzo)的布局。
回答这个西班牙网站找到。

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    RelativeLayout layout1 = (RelativeLayout) findViewById(R.id.layout1);
    Lienzo image = new Lienzo(this);
    layout1.addView(image);     
}

感谢泽维尔Falempin,您启发了我。

Thanks Xavier Falempin, you inspired me.

这篇关于如何插入用帆布的形象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆