标签:android sharedpreferences surfaceview
我正在这个应用程序中实现一个游戏应用程序我在首选项活动中使用共享首选项
spinnerTheme.setOnItemSelectedListener(new OnItemSelectedListener(){
SharedPreferences prefs1 = getSharedPreferences("TipCalcPreferenceDatabase", 0);
Editor e = prefs1.edit();
public void onItemSelected(AdapterView parent,View v,int position,long id)
{
prefs = getSharedPreferences("TipCalcPreferenceDatabase", 0);
Editor e = prefs.edit();
String str;
if(position==0)
{
str= String.valueOf((R.drawable.image1));
e.putString("TipCalcPropertyName", str);
}
if(position==1)
{
str= String.valueOf((R.drawable.image2));
e.putString("TipCalcPropertyName", str);
}
else if(position==2)
{
str= String.valueOf((R.drawable.ballon_background));
e.putString("TipCalcPropertyName", str);
}
e.commit();
/* Preferences current Theme update START*/
String mySetting = prefs.getString("TipCalcPropertyName", "");
LinearLayout ln=(LinearLayout) findViewById(R.id.LinearLayout);
ln.setBackgroundResource((int) Double.parseDouble(mySetting) );
}
public void onNothingSelected(AdapterView<?> arg0)
{
// TODO Auto-generated method stub
}
});
以上共享优势如何在表面视图类中实现
这是surface类的initialize()抽象方法扩展到布局类
public void initialize()
{
int n;
Bitmap background;
// Screen size
Log.v("height",scwidth+"is width and height is "+scheight);
background=getImage(R.drawable.image1);
background = background.createScaledBitmap(background,scwidth,scheight, true);
}
下面的xml文件是表面视图
<com.softwares.bird.BirdGame xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_absolute"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF000000" android:orientation="horizontal">
</com.softwares.bird.BirdGame>
这个共享的prefrences值如何传递表面视图类
提出有价值的建议我在更多的日子里对这个问题感到震惊
提前致谢
解决方法:
我实际上有这个问题,因为我的应用程序想要存储一个更复杂的对象我做了这个我把对象序列化然后将它存储到一个文件.然后,我可以从任何活动检查此文件并获取完整对象.无需使用prefs和Bundles的字符串键垃圾.
/**
* @param act - current activity calling the function
* @return false if global Object not set and the dat file is not there or empty
*/
public static void setInFile(Activity act, Object_VO obj) {
((Object) act.getApplication()).setObjectState(obj);
String ser = SerializeObject.objectToString(obj);
if (ser!= null && !ser.equalsIgnoreCase("")) {
SerializeObject.WriteSettings(act, ser, "android.dat");
} else {
SerializeObject.WriteSettings(act, "", "android.dat");
}
}
您将需要它来序列化和反序列化:
/**
* Take an object and serialize and then save it to preferences
*
*/
public class SerializeObject {
/**
* Create a String from the Object using Base64 encoding
* @param object - any Object that is Serializable
* @return - Base64 encoded string.
*/
public static String objectToString(Serializable object) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
new ObjectOutputStream(out).writeObject(object);
byte[] data = out.toByteArray();
out.close();
out = new ByteArrayOutputStream();
Base64OutputStream b64 = new Base64OutputStream(out,0);
b64.write(data);
b64.close();
out.close();
return new String(out.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* Creates a generic object that needs to be cast to its proper object
* from a Base64 ecoded string.
*
* @param encodedObject
* @return
*/
public static Object stringToObject(String encodedObject) {
try {
return new ObjectInputStream(new Base64InputStream(
new ByteArrayInputStream(encodedObject.getBytes()), 0)).readObject();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* Save serialized settings to a file
* @param context
* @param data
*/
public static void WriteSettings(Context context, String data, String filename){
FileOutputStream fOut = null;
OutputStreamWriter osw = null;
try{
fOut = context.openFileOutput(filename, Context.MODE_PRIVATE);
osw = new OutputStreamWriter(fOut);
osw.write(data);
osw.flush();
//Toast.makeText(context, "Settings saved",Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
// Toast.makeText(context, "Settings not saved",Toast.LENGTH_SHORT).show();
}
finally {
try {
if(osw!=null)
osw.close();
if (fOut != null)
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* Read data from file and put it into a string
* @param context
* @param filename - fully qualified string name
* @return
*/
public static String ReadSettings(Context context, String filename){
StringBuffer dataBuffer = new StringBuffer();
try{
// open the file for reading
InputStream instream = context.openFileInput(filename);
// if file the available for reading
if (instream != null) {
// prepare the file for reading
InputStreamReader inputreader = new InputStreamReader(instream);
BufferedReader buffreader = new BufferedReader(inputreader);
String newLine;
// read every line of the file into the line-variable, on line at the time
while (( newLine = buffreader.readLine()) != null) {
// do something with the settings from the file
dataBuffer.append(newLine);
}
// close the file again
instream.close();
}
} catch (java.io.FileNotFoundException f) {
// do something if the myfilename.txt does not exits
Log.e("FileNot Found in ReadSettings", "filename = " + filename);
} catch (IOException e) {
Log.e("IO Error in ReadSettings", "filename = " + filename);
}
return dataBuffer.toString();
}
}
最后,您需要创建可以引用的全局对象状态.
package com.utils;
/**
* Global class to hold the application state of the object
*
*/
public class ObjectState extends Application {
private Object_VO obj;
public Object_VO getObjectState() {
return this.obj;
}
public void setObjectState(Object_VO o) {
this.obj = o;
}
}
哦是的,您需要更新AndroidManifest.xml文件以包含对ObjectState的引用,因为它引用了这样的应用程序…
<application android:name="com.utils.ObjectState" android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
标签:android,sharedpreferences,surfaceview 来源: https://codeday.me/bug/20190710/1419723.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。