如何在Java中将图像存储在变量中?(安卓开发)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19285556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 15:42:33  来源:igfitidea点击:

How to store an image in a variable in Java? (Android Development)

javaandroid

提问by Nicolas Mustaro

I've searched around Google and Bing for a while, but so far have only found legit answers pertaining to JavaScript and similar web scripting languages. Hoping one of you fine folks could provide me some insight on the best way to go about this.

我已经在 Google 和 Bing 上搜索了一段时间,但到目前为止只找到了与 JavaScript 和类似 Web 脚本语言有关的合法答案。希望你们中的一个好人可以为我提供一些有关解决此问题的最佳方法的见解。

Basically I'm developing a card-game for Android, and I'm storing my individual cards in an ArrayList of the class Card. Card, so far, has three priv variables: Rank, Suit and Image. Rank and suit are obvious, but I'm stumped as for a way to store the image for the card in the class. Should I store a string that is the directory for the image in res/mdpi, or should I actually store it as a bitmap (if that's even possible...).

基本上,我正在为 Android 开发纸牌游戏,并将我的个人纸牌存储在 Card 类的 ArrayList 中。到目前为止,卡片有三个 priv 变量:Rank、Suit 和 Image。等级和花色是显而易见的,但我对在班级中存储卡片图像的方法感到困惑。我应该在 res/mdpi 中存储一个作为图像目录的字符串,还是应该将它实际存储为位图(如果可能的话......)。

Basically I want to be able to, when dealing the cards in later-created classes, to use a method that will randomly select a card, make sure it hasn't been dealt yet, then deal the card. I'm guessing I'll draw it to the screen by calling something like "Card.drawCard" where "drawCard" is a method of "Card" I will create to paint the card... Probably end up doing it a completely different way, but I'll learn-as-I-go.

基本上,我希望能够在以后创建的类中发牌时,使用一种随机选择一张牌的方法,确保它尚未发牌,然后发牌。我猜我会通过调用“Card.drawCard”之类的东西将它绘制到屏幕上,其中“drawCard”是我将创建的一种“卡片”方法来绘制卡片......最终可能会完全不同方式,但我会边走边学。

So... How do I store the image in a variable?

那么...如何将图像存储在变量中?

回答by qwertyuiop5040

You are looking for the Bitmap class.

您正在寻找 Bitmap 类。

    Bitmap b=BitmapFactory.decodeResource(getResources(),R.drawable.bitmap);//R.drawable.bitmap is the bitmap in your drawable folder.
    ImageView iv=(ImageView)findViewById(R.id.image);

and use

并使用

    iv.setImageResource(R.drawable.bitmap);

or

或者

    iv.setImageBitmap(bmp);

The above method is a very simple way of displaying graphics. For more advanced graphics, extend some View class and make your custom graphics.

上述方法是一种非常简单的图形显示方式。对于更高级的图形,请扩展一些 View 类并制作自定义图形。

回答by yushulx

Use BitmapFactoryto decode an image to bitmap and store it in memory. like:

使用BitmapFactory将图像解码为位图并将其存储在内存中。喜欢:

Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/image.jpg");

then you can use Canvasto draw bitmap.

那么你就可以使用Canvas来绘制位图了。

回答by zahid ali

use the Bitmap to store image in bitmap . i.e when you store name , then u use string. if you wanna to store image then use bitmap.

使用 Bitmap 将图像存储在 bitmap 中。即当您存储 name 时,您将使用字符串。如果要存储图像,请使用位图。

public BitMap image;