[英]Cannot resolve method 'getdrawable()
我是编程的新手。 我想构建一个可以保存或下载二维码的二维码生成器。
这是我的生成器代码:
public class GeneratorActivity extends AppCompatActivity {
EditText text;
Button gen_btn;
ImageView image;
String text2Qr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_generator);
text = findViewById(R.id.text);
gen_btn = findViewById(R.id.gen_btn);
image = findViewById(R.id.image);
gen_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
text2Qr = text.getText().toString().trim();
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
try{
BitMatrix bitMatrix = multiFormatWriter.encode(text2Qr, BarcodeFormat.QR_CODE,200,200);
BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix);
image.setImageBitmap(bitmap);
bitmap = ((BitmapDrawable) ImageView.getdrawable()).getBitmap();
}
catch (WriterException e){
e.printStackTrace();
}
}
});
}
}
我收到一个错误,我无法解析方法“getdrawable()”
有人知道如何解决这个问题吗?
这是错误的截图:截图
返回与特定资源 ID 关联并为指定主题设置样式的可绘制对象。
你应该通过OBJECT
bitmap = ((BitmapDrawable) image.getDrawable()).getBitmap();
供参考
Drawable drawable = image.getDrawable();
您已经有一个位图,并且已将其设置为 imageview。 为什么需要这条线
位图 = ((BitmapDrawable) ImageView.getdrawable()).getBitmap();
错误是说 ImageView 类中没有名为 getDrawable() 的方法。
从imageview中获取drawable like
Drawable myDrawable = imageView.getDrawable();
您可以将其与可绘制资源进行比较,例如
if(iv.getDrawable()==getResources().getDrawable(R.drawable.image1)){
//do work here
}
我想你可以用它Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix);
直接地。 可能不需要bitmap = ((BitmapDrawable) image.getdrawable()).getBitmap();
您可以从将返回 null 的空imageview
中drawable
。 要从drawble
获取位图,请尝试此方法。
public static Bitmap drawableToBitmap (Drawable drawable) {
Bitmap bitmap = null;
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
if(bitmapDrawable.getBitmap() != null) {
return bitmapDrawable.getBitmap();
}
}
希望能帮到你!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.