gpt4 book ai didi

java - 如何将 Drawable 传递给类?

转载 作者:行者123 更新时间:2023-11-30 10:59:04 26 4
gpt4 key购买 nike

是否有像这样通过类传递 Drawable 的方法:

public loadImage(Context context, int drawable, int x, int y, int width, int height) {
this.drawable = context.getResources().getDrawable(drawable);
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}

尝试将 Drawable 传递到类中:

candyImg = new loadImage(getContext(), getResources().getDrawable(R.drawable.candy), 0, 0, 50, 50);

它说 getResources().getDrawable(drawable); 已弃用。那么我如何通过这样的类传递 Drawable

最佳答案

首先,将int drawable参数改为Drawable drawable

getResources().getDrawable(drawable); 开始,您需要将其替换为 ContextCompat.getDrawable(context, R.drawable.my_drawable)

Context 上下文 参数是多余的,你可以删除它:

public loadImage(Drawable drawable, int x, int y, int width, int height) {
this.drawable = drawable;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}

然后,尝试将其传递给类:

candyImg = new loadImage(ContextCompat.getDrawable(this, R.drawable.my_drawable), 0, 0, 50, 50);

关于java - 如何将 Drawable 传递给类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32031923/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com