如何使用SharedPreference在Canvas上绘制字符串?

时间:2016-09-27 07:29:36

标签: java android canvas

我有字符串的ArrayList,我想在Sharedprefrence中保存该列表我能够做到这一点,我也在检索那些字符串,但我想知道如何在canvas中使用该字符串onDraw方法和在画布上绘图

这里我在屏幕上绘制文本并将其保存在ArrayLIst

if (text_c == 0 && c.getImage() == 4) {

        textsize = c.getTextsize();
        canvas.drawText(c.getEdittext(), startX, startY, mPaint);
        mPaint.setStyle(Style.FILL);
        c.setImage(0);


    }


for (EdtText l : editTexts) {


        mPaint.setTextSize(l.textsize);
        canvas.drawText(l.EdtText1, l.startX, l.stopY, mPaint);
        c.setstringDraw(editTexts.toString());
    }

保存字符串的方法

public void saveString() {


    String sp=c.getStringDraw();



    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getContext());
    SharedPreferences.Editor editor = sharedPrefs.edit();
    editor.putString("kEy", sp);
    editor.commit();

}

检索该字符串的方法

public void openString(){


    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getContext());
    Gson gson = new Gson();
    String json = sharedPrefs.getString("kEy", null);


}

现在,我想检索保存在sharedpreference中的字符串并想要在onDraw方法中绘制

1 个答案:

答案 0 :(得分:0)

这里我将这些行添加到openString方法

public void openString(){


SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getContext());
Gson gson = new Gson();
String json = sharedPrefs.getString("kEy", null);

   Type type = new TypeToken<ArrayList<EdtText>>() {
    }.getType();
    ArrayList<EdtText> arrayList = gson.fromJson(json, type);


    if (arrayList != null) {
        editTexts = arrayList;
    }

并且在onDraw方法中调用openString方法

 if (text_save == 0) {

        openString();
        text_save++;
    }

那就是......