gpt4 book ai didi

java - 改进代码格式(性能)

转载 作者:行者123 更新时间:2023-12-01 14:34:33 26 4
gpt4 key购买 nike

我尝试格式化我的代码以获得最佳性能并避免内存泄漏和应用程序操作延迟,这是处理以编程方式创建的多个 TextView 的 Activity 之一(由分隔符分隔的 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;
}}

}

更新:我尝试这样做,但我强行关闭:

主要 Activity :

  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); }
}
}

字体工厂:

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

最佳答案

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

关于java - 改进代码格式(性能),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16597422/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com