改进代码格式(性能)

时间:2013-05-16 21:01:53

标签: java android textview

我尝试格式化我的代码以获得最佳性能并避免内存泄漏和我的应用程序的操作延迟,其中一个活动处理以编程方式创建的多个textview(40 textview段以分隔符分隔)。

作为我对Android开发的小知识我到达下面的代码,所有textview都有相同的文字颜色,相同的文字大小和相同的自定义字体和重力但它们不同的字符串由html定制字符串xml中的标签。

如下代码:

是否有更好的构造代码格式化以达到相同的目的

public class Text extends Activity {
 @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
 Boolean customTitleSupported = requestWindowFeature (Window.FEATURE_CUSTOM_TITLE);      setContentView(R.layout.text);  
     if (customTitleSupported) { getWindow().setFeatureInt
           (Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title); }

    TextView tv = (TextView) findViewById(R.id.title_tv1); 
    Typeface face=Typeface.createFromAsset(getAssets(),"BFantezy.ttf");     
    tv.setTypeface(face);
    tv.setText(" My Text ");  


    LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout);

        TextView tv1 = new TextView(this);
        tv1.setGravity(Gravity.CENTER);
        tv1.setTextSize(30);    
        tv1.setTypeface(FontFactory.getBFantezy(getBaseContext()));
        ll.addView(tv1);
        tv1.setText(Html.fromHtml(getString(R.string.text1)));  

        ImageView divider1 = new ImageView(this);
        LinearLayout.LayoutParams lp1 = 
        new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5);
        lp1.setMargins(10, 10, 10, 10);
        divider1.setLayoutParams(lp1);
        divider1.setBackgroundColor(Color.BLUE);
        ll.addView(divider1);

        TextView tv2 = new TextView(this);      
        tv2.setGravity(Gravity.RIGHT);
        tv2.setTextSize(30);
        tv2.setTypeface(FontFactory.getBFantezy(getBaseContext()));
        ll.addView(tv2);
        tv2.setText(Html.fromHtml(getString(R.string.TEXT2)));

        ImageView divider2 = new ImageView(this);
        LinearLayout.LayoutParams lp2 = 
        new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5);
        lp2.setMargins(10, 10, 10, 10);
        divider2.setLayoutParams(lp2);
        divider2.setBackgroundColor(Color.BLUE);
        ll.addView(divider2);

        TextView tv3 = new TextView(this);
        tv3.setGravity(Gravity.CENTER);
        tv3.setTextSize(30);
        tv3.setTypeface(FontFactory.getBFantezy(getBaseContext()));
        ll.addView(tv3);
        tv3.setText(Html.fromHtml(getString(R.string.TEXT3)));

        ImageView divider3 = new ImageView(this);
        LinearLayout.LayoutParams lp3 = 
        new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5);
        lp3.setMargins(10, 10, 10, 10);
        divider3.setLayoutParams(lp3);
        divider3.setBackgroundColor(Color.BLUE);
        ll.addView(divider3);

        TextView tv4 = new TextView(this);
        tv4.setGravity(Gravity.CENTER);
        tv4.setTextSize(30);    
        tv4.setTypeface(FontFactory.getBFantezy(getBaseContext()));
        ll.addView(tv4);
        tv4.setText(Html.fromHtml(getString(R.string.TEXT4)));  

        ImageView divider4 = new ImageView(this);
        LinearLayout.LayoutParams lp4 = 
        new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5);
        lp4.setMargins(10, 10, 10, 10);
        divider4.setLayoutParams(lp4);
        divider4.setBackgroundColor(Color.BLUE);
        ll.addView(divider4);

,这将以同样的方式继续,直到textview tv40。

添加自定义字体如下:

    public static class FontFactory {

            private static Typeface t1;

            public static Typeface getBFantezy(Context c) {
                if (t1 == null) {
                    t1 = Typeface.createFromAsset(c.getAssets(), "BFantezy.ttf");
                }
                return t1;
            }}

            }   

更新 我尝试这样做,但我有力量关闭:

MainActivity:

  public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String[] paragraphs = getResources().getStringArray(R.array.paragraphs);
    LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
    addParagraphs(layout, paragraphs);

    setContentView(R.layout.main);}

private void addParagraphs(LinearLayout layout, String[] paragraphs) {
    for (String paragraph : paragraphs) {
        TextView tv = new TextView(this);
        tv.setGravity(Gravity.CENTER);
        tv.setTextSize(30);   
        tv.setTextColor(Color.GREEN);
        tv.setTypeface(FontFactory.getBFantezy(getBaseContext()));
        layout.addView(tv);
        tv.setText(paragraph);  

        ImageView divider = new ImageView(this);
        LinearLayout.LayoutParams params = new  
            LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5);
        params.setMargins(10, 10, 10, 10);
        divider.setLayoutParams(params);
        divider.setBackgroundColor(Color.BLUE);
        layout.addView(divider); } 
                                }
                            }

FontFactory:

public class FontFactory {

private static Typeface t1;

public static Typeface getBFantezy(Context c) {
    if (t1 == null) {
        t1 = Typeface.createFromAsset(c.getAssets(), "BFantezy.ttf");
    }
    return t1;
}}

LOGCAT:

 FATAL EXCEPTION: main
   java.lang.RuntimeException: Unable to start activity  
ComponentInfo{com.test.demo/com.test.demo.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
      Caused by: java.lang.NullPointerException
at com.test.demo.MainActivity.addParagraphs(MainActivity.java:29)
at com.test.demo.MainActivity.onCreate(MainActivity.java:18)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
... 11 more

2 个答案:

答案 0 :(得分:1)

我强烈建议您考虑使用ListView,这样您就不必单独创建这些项目。另外,我不知道您对FontFactory的实现是什么,但如果可能的话,只创建一次字体。

答案 1 :(得分:0)

如果您确实希望使用TextView并使用ImageView作为分隔符来执行此操作,则可以先创建段落的字符串数组。

将其放入/res/values/arrays.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="paragraphs">
        <item>Paragraph 1 content here</item>
        <item>Paragraph 2 content here</item>
        <item>Paragraph 3 content here</item>
<!-- And so on until how many paragraphs you like -->
    </string-array>
</resources>

然后你可以在代码中得到这些段落:

String[] paragraphs = getResources().getStringArray(R.array.paragraphs);

然后你可以使用这样的方法:

private void addParagraphs(LinearLayout layout, String[] paragraphs) {
    for (String paragraph : paragraphs) {
        TextView tv = new TextView(this);
        tv.setGravity(Gravity.CENTER);
        tv.setTextSize(30);    
        tv.setTypeface(FontFactory.getBFantezy(getBaseContext()));
        layout.addView(tv);
        tv.setText(paragraph);  

        ImageView divider = new ImageView(this);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 5);
        params.setMargins(10, 10, 10, 10);
        divider.setLayoutParams(params);
        divider.setBackgroundColor(Color.BLUE);

        layout.addView(divider);
    }
}

要完成此示例,您可以使用以下段落填充布局:

String[] paragraphs = getResources().getStringArray(R.array.paragraphs);
LinearLayout layout = (LinearLayout) findViewById(R.id.yourLayout);
addParagraphs(layout, paragraphs);

编辑:如果要设置其他段落参数(如对齐),可以在res/values/arrays.xml上创建另一个字符串数组,其项目数与段落数组相同,然后在方法上传递它们以设置对齐每个段落编号各自。有更多方法可以做到这一点,这只是一个例子。发挥创意,享受。 :)