gpt4 book ai didi

java - 如何以编程方式更改位图?

转载 作者:行者123 更新时间:2023-12-01 12:06:40 27 4
gpt4 key购买 nike

我有这种情况,一切正常,但我认为这是一个困惑的代码。您能帮助我获得更好的代码吗?我想更新背景位图,我只更改 mBg。

我有这种情况,一切正常,但我认为这是一个困惑的代码。您能帮助我获得更好的代码吗?我想更新背景位图,我只更改 mBg。

Bitmap mBg;

mBackground = Bitmap.createBitmap(1800, 1200, Bitmap.Config.RGB_565);
// Put back and top images in your res folder
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;

File f = new File(AppConstant.filepathone);
Bitmap mImageone = decodeFile(f);
File g = new File(AppConstant.filepathtwo);
Bitmap mImagetwo = decodeFile(g);
File h = new File(AppConstant.filepaththree);
Bitmap mImagethree = decodeFile(h);
File i = new File(AppConstant.filepathfour);
Bitmap mImagefour = decodeFile(i);

if (sharedpreferences.getString("getFrame", "getFrame") == "Frame1") {
mBg = BitmapFactory.decodeResource(getResources(),
R.drawable.frameone);
Bitmap mBack = Bitmap.createScaledBitmap(mBg, 1800, 1200, true);
Bitmap mImaget = Bitmap.createScaledBitmap(mImagetwo, 515, 360,
true);
Bitmap mImageth = Bitmap.createScaledBitmap(mImagethree, 515, 360,
true);
Bitmap mImagef = Bitmap.createScaledBitmap(mImagefour, 515, 360,
true);
Bitmap mImageo = Bitmap.createScaledBitmap(mImageone, 1080, 635,
true);

mCanvas = new Canvas(mBackground);
mCanvas.drawARGB(255, 150, 150, 10);
mCanvas.drawBitmap(mBack, 0, 0, null);
mCanvas.drawBitmap(mImaget, 75, 75, null);
mCanvas.drawBitmap(mImageo, 75, 490, null);
mCanvas.drawBitmap(mImageth, 645, 75, null);
mCanvas.drawBitmap(mImagef, 1215, 75, null);

} else if (sharedpreferences.getString("getFrame", "getFrame") == "Frame2") {
mBg = BitmapFactory.decodeResource(getResources(),
R.drawable.frametwo);
Bitmap mBack = Bitmap.createScaledBitmap(mBg, 1800, 1200, true);
Bitmap mImaget = Bitmap.createScaledBitmap(mImagetwo, 515, 360,
true);
Bitmap mImageth = Bitmap.createScaledBitmap(mImagethree, 515, 360,
true);
Bitmap mImagef = Bitmap.createScaledBitmap(mImagefour, 515, 360,
true);
Bitmap mImageo = Bitmap.createScaledBitmap(mImageone, 1080, 635,
true);

mCanvas = new Canvas(mBackground);
mCanvas.drawARGB(255, 150, 150, 10);
mCanvas.drawBitmap(mBack, 0, 0, null);
mCanvas.drawBitmap(mImaget, 75, 75, null);
mCanvas.drawBitmap(mImageo, 75, 490, null);
mCanvas.drawBitmap(mImageth, 645, 75, null);
mCanvas.drawBitmap(mImagef, 1215, 75, null);

} else if (sharedpreferences.getString("getFrame", "getFrame") == "Frame3") {
mBg = BitmapFactory.decodeResource(getResources(),
R.drawable.framethree);
Bitmap mBack = Bitmap.createScaledBitmap(mBg, 1800, 1200, true);
Bitmap mImaget = Bitmap.createScaledBitmap(mImagetwo, 515, 360,
true);
Bitmap mImageth = Bitmap.createScaledBitmap(mImagethree, 515, 360,
true);
Bitmap mImagef = Bitmap.createScaledBitmap(mImagefour, 515, 360,
true);
Bitmap mImageo = Bitmap.createScaledBitmap(mImageone, 1080, 635,
true);

mCanvas = new Canvas(mBackground);
mCanvas.drawARGB(255, 150, 150, 10);
mCanvas.drawBitmap(mBack, 0, 0, null);
mCanvas.drawBitmap(mImaget, 75, 75, null);
mCanvas.drawBitmap(mImageo, 75, 490, null);
mCanvas.drawBitmap(mImageth, 645, 75, null);
mCanvas.drawBitmap(mImagef, 1215, 75, null);
}

try {
String friendlydate = DateFormat.getTimeInstance(DateFormat.MEDIUM)
.format(new Date());
friendlydate = friendlydate.replace(':', '_');
String filename = friendlydate + ".jpg";
mBitmapDrawable = new BitmapDrawable(mBackground);
Bitmap mNewSaving = mBitmapDrawable.getBitmap();
String FtoSave = mTempDir + filename;
File mFile = new File(FtoSave);
mFileOutputStream = new FileOutputStream(mFile);

// mNewSaving.recycle();
mNewSaving.compress(Bitmap.CompressFormat.PNG, 100,
mFileOutputStream);
mFileOutputStream.flush();
mFileOutputStream.close();
mFileOutputStream = null;

最佳答案

这里有一些提示

  1. 在 java 中,永远不要使用“==”运算符来比较字符串。使用 String.equals() 方法,如 if (sharedpreferences.getString("getFrame", "getFrame").equals("Frame1") 否则你会进行比较它们的指针而不是它们的实际内容。
  2. 唯一根据共享首选项“getFrame”的值而变化的是变量 mbg,因此它实际上是您需要放入这些 if block 中的唯一内容。您的整个 if block 将简化为:

    if (sharedpreferences.getString("getFrame", "getFrame").equals("Frame1")) {
    mBg = BitmapFactory.decodeResource(getResources(),
    R.drawable.frameone);
    } else if (sharedpreferences.getString("getFrame", "getFrame").equals("Frame2")) {
    mBg = BitmapFactory.decodeResource(getResources(),
    R.drawable.frametwo);
    } else if (sharedpreferences.getString("getFrame", "getFrame").equals("Frame3")) {
    mBg = BitmapFactory.decodeResource(getResources(),
    R.drawable.framethree);
    }

    Bitmap mBack = Bitmap.createScaledBitmap(mBg, 1800, 1200, true);
    Bitmap mImaget = Bitmap.createScaledBitmap(mImagetwo, 515, 360,
    true);
    Bitmap mImageth = Bitmap.createScaledBitmap(mImagethree, 515, 360,
    true);
    Bitmap mImagef = Bitmap.createScaledBitmap(mImagefour, 515, 360,
    true);
    Bitmap mImageo = Bitmap.createScaledBitmap(mImageone, 1080, 635,
    true);

    mCanvas = new Canvas(mBackground);
    mCanvas.drawARGB(255, 150, 150, 10);
    mCanvas.drawBitmap(mBack, 0, 0, null);
    mCanvas.drawBitmap(mImaget, 75, 75, null);
    mCanvas.drawBitmap(mImageo, 75, 490, null);
    mCanvas.drawBitmap(mImageth, 645, 75, null);
    mCanvas.drawBitmap(mImagef, 1215, 75, null);
  3. 如果您通过创建一个将帧名称映射到已解码资源的映射来多次调用此方法,则可以使 mBg 变量的选择更加高效。为此,您最好在 Activity 的 onCreate() 方法中声明并填充如下所示的 map :

    Map<String, Bitmap> mBgMap = new HashMap<>();
    mBgMap.put("Frame1", BitmapFactory.decodeResource(getResources(),
    R.drawable.frameone));
    mBgMap.put("Frame2", BitmapFactory.decodeResource(getResources(),
    R.drawable.frametwo));
    mBgMap.put("Frame3", BitmapFactory.decodeResource(getResources(),
    R.drawable.framethree));

然后将那个笨拙的 if block 替换为:

    mBg = mBgMap.get(sharedpreferences.getString("getFrame", "getFrame"));

关于java - 如何以编程方式更改位图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27540230/

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