Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/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 自定义视图到Toast-不绘制_Android_Bitmap_Android Canvas_Toast_Custom View - Fatal编程技术网

Android 自定义视图到Toast-不绘制

Android 自定义视图到Toast-不绘制,android,bitmap,android-canvas,toast,custom-view,Android,Bitmap,Android Canvas,Toast,Custom View,我正在尝试使用我创建的自定义视图显示Toast消息。 视图的背景上有一个位图,我想在上面写一些文字 如果我将位图分配给主代码上的ImageView,我会设法使它显示为Toast t。。。t、 展示; 但是当我的类的onDraw方法分配位图时,什么也不显示。 我选中了,我的视图的大小为0,当我按照我在下转录的方式创建时为0 请帮忙 Main.java LimitView.java 请尝试以下代码: Main.java 上下文=这个 LimitView.java 这里有几个错误 BitmapFact

我正在尝试使用我创建的自定义视图显示Toast消息。 视图的背景上有一个位图,我想在上面写一些文字

如果我将位图分配给主代码上的ImageView,我会设法使它显示为Toast t。。。t、 展示;
但是当我的类的onDraw方法分配位图时,什么也不显示。 我选中了,我的视图的大小为0,当我按照我在下转录的方式创建时为0

请帮忙

Main.java

LimitView.java

请尝试以下代码:

Main.java

上下文=这个

LimitView.java


这里有几个错误

BitmapFactory.decodeResource(getResources(), R.drawable.limit)
这是一项繁重的操作,不应处于视图绘制阶段

canvas.setBitmap();
此方法不在画布上绘制位图,而是将画布缓冲区设置为使用此位图

试试这样的东西:

public class MyView extends View{

    private Bitmap bitmap;

    public MyView(Context context) {
        super(context);
        bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.limit);
    }

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

}
为此:

canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.button_pressed),left,top, new Paint());

这里左边和右边是屏幕的位置。

但是当我的类的onDraw方法分配位图时,什么也不显示。这一部分我不清楚。@Raghunandan:意思是它不能正常工作。你看不到我已经管理的@Raghunandan图像。支票ṩ 解决方案有什么区别?代码postedsetBitmap用于在画布中绘制可变位图有什么问题。@Ȃŵåiṩ我在问为什么它不起作用。Babibu post清除了我的疑问ok:顺便说一句,如果你应该选择任何一个答案作为正确答案。ok。。。工作因为我使用的是API7,所以getMatrix不可用,使用的是drawBitmapbmp,0.0f,0.0f,new Paint
BitmapFactory.decodeResource(getResources(), R.drawable.limit)
canvas.setBitmap();
public class MyView extends View{

    private Bitmap bitmap;

    public MyView(Context context) {
        super(context);
        bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.limit);
    }

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

}
replace this 

canvas.setBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.limit));
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.button_pressed),left,top, new Paint());