[英]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 这是给你的一些提示
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")
否则您将比较它们的指针,而不是它们的实际内容。 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);
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.