简体   繁体   English

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

[英]How to change programmatically the bitmap?

I have this condition and all is working but i think it's a mess code. 我有这种情况,并且一切正常,但是我认为这是一个混乱的代码。 Can you please help me for a better codes which i want to update of the bitmap of background i only change the mBg. 您能帮我获得更好的代码吗,我想更新背景位图,我只更改了mBg。

I have this condition and all is working but i think it's a mess code. 我有这种情况,并且一切正常,但是我认为这是一个混乱的代码。 Can you please help me for a better codes which i want to update of the bitmap of background i only change the 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;

Here are some tips for you 这是给你的一些提示

  1. In java you should never use the "==" operator to compare strings. 在Java中,永远不要使用“ ==”运算符来比较字符串。 Use String.equals() method like in if (sharedpreferences.getString("getFrame", "getFrame").equals("Frame1") otherwise you'd be comparing their pointers and not their actual contents. 像在if (sharedpreferences.getString("getFrame", "getFrame").equals("Frame1")那样使用String.equals()方法if (sharedpreferences.getString("getFrame", "getFrame").equals("Frame1")否则您将比较它们的指针,而不是它们的实际内容。
  2. The only thing that changes depending on the value of the shared preference "getFrame" is the variable mbg so it's actually the only thing you need to put inside those if blocks. 根据共享首选项“ getFrame”的值更改的唯一更改是变量mbg,因此实际上它是您唯一需要放入这些if块中的更改。 Your entire if block would be reduced into this: 您的整个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. You can make the selection of mBg variable even more efficient if you call this method many times by creating a map that maps the frame name to a resource that has already been decoded. 如果您多次调用此方法,则可以通过创建将帧名映射到已解码的资源的映射来使mBg变量的选择更加有效。 To do so you would declare and populate a map like the following one preferably inside the onCreate() method of your Activity: 为此,您最好在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)); 

And then just replace that clumsy if block by: 然后, if阻止了,则只需替换该笨拙的代码:

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

try this 尝试这个

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.

相关问题 如何以编程方式更改 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 Android,以编程方式更改位图纵横比 - Android, change Bitmap aspect ratio programmatically 如何更改位图的大小? - how to change the size of Bitmap? 如何在TouchEvent上更改位图资源 - How To Change The Bitmap Resource On TouchEvent 如何更改位图的不透明度? - How to change a bitmap's opacity? 如何以编程方式更改密码? - How to change password programmatically? 如何以编程方式更改代理? - How to change proxy programmatically? 如何从Android中的位图以编程方式创建SVG文件? - How to create SVG file from Bitmap in Android Programmatically? Android:如何以编程方式仅舍入位图的顶角? - Android: how to programmatically round only top corners of a bitmap? 如何在libgdx中更改位图字体的alpha - How to change the alpha of a bitmap font in libgdx
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM