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

艾伊兹·卡布哈特(Aiiz Cabuhat)Ü

我有这种情况,一切都在工作,但我认为这是一个混乱的代码。您能帮我获得更好的代码吗,我想更新背景位图,我只更改了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;
埃兰戈

试试这个

String uri = "@drawable/myresource.png";
int imageResource = getResources().getIdentifier(uri, null, getPackageName());
imageview= (ImageView)findViewById(R.id.imageView);
Drawable res = getResources().getDrawable(imageResource);
imageView.setImageDrawable(res);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章