簡體   English   中英

如何以編程方式更改位圖?

[英]How to change programmatically the bitmap?

我有這種情況,並且一切正常,但是我認為這是一個混亂的代碼。 您能幫我獲得更好的代碼嗎,我想更新背景位圖,我只更改了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中,永遠不要使用“ ==”運算符來比較字符串。 像在if (sharedpreferences.getString("getFrame", "getFrame").equals("Frame1")那樣使用String.equals()方法if (sharedpreferences.getString("getFrame", "getFrame").equals("Frame1")否則您將比較它們的指針,而不是它們的實際內容。
  2. 根據共享首選項“ getFrame”的值更改的唯一更改是變量mbg,因此實際上它是您唯一需要放入這些if塊中的更改。 您的整個if塊將簡化為:

     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<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阻止了,則只需替換該笨拙的代碼:

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

嘗試這個

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);

如何以編程方式更改 android 的值: tint 屬性<bitmap xml< div><div id="text_translate"><p> 我是新來的,我需要你的幫助。 請告訴我如何從程序級別訪問層列表 xml 結構並從程序級別動態更改 bitmap 的“色調”顏色。</p><pre> &lt;layer-list xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:id="@+id/bg_peper" android:right="65dp"&gt; &lt;bitmap android:gravity="top|left" android:tint="@color/red" android:src="@drawable/ic_favorite" /&gt; &lt;/item&gt; &lt;item.... &lt;/layer-list&gt;</pre></div></bitmap>

[英]How to programmatically change the value of the android: tint property in <bitmap xml

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM