[英]Android Studio - how do I call a method which references a drawable?
This is very much a noob question.这是一个非常菜鸟的问题。
With the benefit of this answer from a different question, I have created a class which theoretically takes a BMP image and resizes it to the size of the image, rather than the (blank) background.借助来自不同问题的这个答案,我创建了一个类,该类理论上采用 BMP 图像并将其调整为图像的大小,而不是(空白)背景。
However, I can't work out how to run the class and reference an image which is saved as a drawable.但是,我无法弄清楚如何运行该类并引用保存为可绘制对象的图像。
I've tried我试过了
package com.example.trimmer_test;
import android.media.Image;
public class Activity_main {
"@drawable/imagename"
}
After some searching, I thought that I had to use the getResource method to import the image, so I tried经过一番搜索,我认为我必须使用getResource方法来导入图像,所以我尝试了
package com.example.trimmer_test;
import android.media.Image;
public class Activity_main {
Activity_main.getResource("@drawable/imagename")
}
But this brings up a "cannot resolve symbol 'getResource'" error.但这会带来“无法解析符号'getResource'”错误。
I've tried what feels like a million variants of this and can't figure it out.我已经尝试过这种感觉像一百万种变体,但无法弄清楚。
Apologies for what I imagine is a basic question, but I have searched thoroughly and can't find the answer anywhere.对我想象的基本问题表示歉意,但我已经彻底搜索过,但在任何地方都找不到答案。
You can try this out.你可以试试这个。
ContextCompat.getDrawable(this, R.drawable.your_image_name) ContextCompat.getDrawable(this, R.drawable.your_image_name)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.