Android的画布的drawLine不是MainActivity画 [英] Android Canvas drawLine not drawing on MainActivity

查看:399
本文介绍了Android的画布的drawLine不是MainActivity画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提请使用画布的主要活动线路。的问题是,它没有画什么。我有以下的code:

 位图位= Bitmap.createBitmap(1920,1080,Bitmap.Config.ARGB_8888);

帆布油画=新的Canvas(位);
涂料粉刷=新的油漆();
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
paint.setStrokeWidth(10);

左浮动= 20;
浮顶= 20;
浮动权= 50;
浮底= 100;

canvas.drawLine(左,上,右,下,涂料);
 

解决方案

您可以显示这样的位图:

  canvas.drawBitmap(BMP,位X,positionY,油漆);
 

在你的情况,你可以试试财产以后像这样的:

  canvas.drawBitmap(位图,0,0,NULL);
 

但你需要使用不同势帆布它。画布至极,让你画的东西在你的屏幕将被传递给你的OnDraw()方法,在你的视图。所以,你需要做一个视图类第一,并在您MainActivity添加它。

您可以做到这一点是这样的: 首先创建一个类MyView的,并添加这个code到它:

 公共类MyView的扩展视图{

    点阵位图;

    公共MyView的(上下文的背景下){
        位图= Bitmap.createBitmap(1920,1080,Bitmap.Config.ARGB_8888);

        帆布油画=新的Canvas(位);
        涂料粉刷=新的油漆();
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        paint.setStrokeWidth(10);

        左浮动= 20;
        浮顶= 20;
        浮动权= 50;
        浮底= 100;

        canvas.drawLine(左,上,右,下,涂料);
    }

    @覆盖
    保护无效的OnDraw(帆布油画){
        canvas.drawBitmap(位图,0,0,NULL);
        super.onDraw(画布);
    }
}
 

然后你在MainActivity改变code在你的onCreate()方法,以这样的:

  @覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);

    MyView的MyView的=新的MyView的(这一点);
    的setContentView(MyView的);
}
 

I want to draw a line on the Main Activity using Canvas. The problem is, it is not drawing anything. I have the following code:

 Bitmap bitmap = Bitmap.createBitmap(1920, 1080, Bitmap.Config.ARGB_8888);

Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
paint.setStrokeWidth(10);

float left = 20;
float top = 20;
float right = 50;
float bottom = 100;

canvas.drawLine(left, top, right, bottom, paint);

解决方案

you can display the bitmap like that:

canvas.drawBitmap(bmp, positionX, positionY, paint);

in your case you can try somthing like this:

canvas.drawBitmap(bitmap, 0, 0, null);

but you need to use a diffrent canvas for it. The canvas wich let you draw stuff on your screen will be passed to your onDraw() method in your View. So you need to make a View class first and add it in your MainActivity.

You can do that like this: First you create a class called MyView and add this code to it:

public class MyView extends View {

    Bitmap bitmap;

    public MyView(Context context) {
        bitmap = Bitmap.createBitmap(1920, 1080, Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint();
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        paint.setStrokeWidth(10);

        float left = 20;
        float top = 20;
        float right = 50;
        float bottom = 100;

        canvas.drawLine(left, top, right, bottom, paint);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawBitmap(bitmap, 0, 0, null);
        super.onDraw(canvas);
    }
}

then you change the code in your onCreate() method in your MainActivity to this:

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

    MyView myView = new MyView(this);
    setContentView(myView);
}

这篇关于Android的画布的drawLine不是MainActivity画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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