我有这个条件,一切正常,但我认为这是一个混乱的代码。你能帮我找一个更好的代码,我想更新背景的位图我只更改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;
答案 0 :(得分:1)
以下是一些提示
String.equals()
中的if (sharedpreferences.getString("getFrame", "getFrame").equals("Frame1")
方法,否则您需要比较指针而不是实际内容。 根据共享偏好的价值而变化的唯一内容" 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);
如果通过创建将帧名称映射到已经解码的资源的映射多次调用此方法,则可以更有效地选择mBg
变量。为此,您最好在活动的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"));
答案 1 :(得分:0)
试试这个
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);