Android:加速动态UI生成

Android:加速动态UI生成,android,Android,我有一个生成日历视图的类。我不太喜欢内置的,我正在尝试对它的外观进行更多的控制。但是,绘制新UI(例如,在刷卡时)需要1-2秒。有什么地方可以让我加快速度吗?在HTC One S 2012车型上进行Am测试 应该相对简单地遵循: import android.graphics.Color; import android.graphics.Paint; import android.graphics.Typeface; import android.graphics.drawable.ShapeD

我有一个生成日历视图的类。我不太喜欢内置的,我正在尝试对它的外观进行更多的控制。但是,绘制新UI(例如,在刷卡时)需要1-2秒。有什么地方可以让我加快速度吗?在HTC One S 2012车型上进行Am测试

应该相对简单地遵循:

import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import org.joda.time.LocalDate;

import java.util.ArrayList;

public class Calendar extends AppCompatActivity {

    private LocalDate _currentSelectedDate = new LocalDate();;
    private LocalDate _today = new LocalDate();;

    private float x1 = 0;
    private float x2 = 0;
    private float y1 = 0;
    private float y2 = 0;

    private TableLayout _tableLayout;
    private RelativeLayout _relativeLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calendar);
        _relativeLayout = (RelativeLayout) findViewById(R.id.calendarLayout);


        recreateUI(_currentSelectedDate.getYear(), _currentSelectedDate.getMonthOfYear());
    }

    @Override
    public boolean onTouchEvent(MotionEvent touchevent)
    {
        switch (touchevent.getAction())
        {
            case MotionEvent.ACTION_DOWN:
            {
                x1 = touchevent.getX();
                y1 = touchevent.getY();
                break;
            }
            case MotionEvent.ACTION_UP:
            {
                x2 = touchevent.getX();
                y2 = touchevent.getY();

                // up
                if (y1 > y2)
                {
                    _currentSelectedDate = _currentSelectedDate.plusMonths(1);
                    recreateUI(_currentSelectedDate.getYear(), _currentSelectedDate.getMonthOfYear());
                }
                // down
                if (y1 < y2)
                {
                    _currentSelectedDate = _currentSelectedDate.minusMonths(1);
                    recreateUI(_currentSelectedDate.getYear(), _currentSelectedDate.getMonthOfYear());
                }

                break;
            }
        }
        return false;
    }


    private void recreateUI(int year, int month)
    {
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);

        _tableLayout = new TableLayout(this);
        _tableLayout.setLayoutParams(lp);
        _tableLayout.setStretchAllColumns(true);
        _relativeLayout.removeAllViews();
        _relativeLayout.addView(_tableLayout);

        LocalDate date = new LocalDate().withYear(year).withMonthOfYear(month).dayOfMonth().withMinimumValue();
        LocalDate last = date.dayOfMonth().withMaximumValue();

        addMonthNameToUi(date);
        addDaysNamesToUi();
        addDayNumberssToUi(date, last);
    }


    private void addMonthNameToUi(LocalDate date) {
        TableRow row = new TableRow(this);

        TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.FILL_PARENT);
        params.span = 7;

        TextView t = new TextView(this);

        t.setLayoutParams(params);
        t.setGravity(Gravity.CENTER);
        t.setTypeface(null, Typeface.BOLD);
        t.setText(date.toString("MMM yyyy"));
        row.addView(t);

        float d = getResources().getDisplayMetrics().density;
        int margin = (int)(20 * d);
        ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) t.getLayoutParams();

        mlp.setMargins(mlp.leftMargin, mlp.topMargin, mlp.rightMargin, margin);

        _tableLayout.addView(row);
    }

    private void addDaysNamesToUi() {
        TableRow dayNameRow = new TableRow(this);

        addMonth(dayNameRow, "Mon");
        addMonth(dayNameRow, "Tue");
        addMonth(dayNameRow, "Wed");
        addMonth(dayNameRow, "Thu");
        addMonth(dayNameRow, "Fri");
        addMonth(dayNameRow, "Sat");
        addMonth(dayNameRow, "Sun");

        _tableLayout.addView(dayNameRow);
    }

    private void addDayNumberssToUi(LocalDate date, LocalDate last) {
        TableRow row = null;
        int columnsCount = 0;
        boolean firstRow = true;

        while (date.isBefore(last) || date.isEqual(last)) {
            if (columnsCount == 0) {
                row = new TableRow(this);

                _tableLayout.addView(row, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.FILL_PARENT, 1.0f));

                // blank columns for days not at the start of month
                if (firstRow) {
                    firstRow = false;
                    int i = 1;
                    for (; i < date.getDayOfWeek(); i++) {
                        addDayNumberToRow(row, date, "");
                    }

                    columnsCount += i - 1;
                    date.plusDays(i - 1);
                }
            }

            addDayNumberToRow(row, date, String.valueOf(date.getDayOfMonth()));

            date = date.plusDays(1);
            columnsCount++;

            if (columnsCount == 7)
                columnsCount = 0;
        }

        while (row.getChildCount() < 7)
            addDayNumberToRow(row, date, "");
    }


    private void addMonth(TableRow row, String month)
    {
        TextView t = new TextView(this);

        t.setText(month);
        t.setGravity(Gravity.CENTER);
        t.setTypeface(null, Typeface.BOLD);
        row.addView(t);
    }

    private void addDayNumberToRow(TableRow row, final LocalDate date, String text)
    {
        TextView v = new TextView(this);
        v.setText(text);
        v.setGravity(Gravity.CENTER);
        v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.FILL_PARENT, 1.0f));

        if (date.getDayOfMonth() == _today.getDayOfMonth() && date.getMonthOfYear() == _today.getMonthOfYear() && date.getYear() == _today.getYear()) {
            v.setTypeface(v.getTypeface(), Typeface.BOLD);
            v.setTextSize(v.getTextSize() + 1);
        }

        ShapeDrawable border = new ShapeDrawable(new RectShape());
        border.getPaint().setStyle(Paint.Style.STROKE);
        border.getPaint().setColor(Color.BLACK);

        v.setBackground(border);
        row.addView(v);
    }
}

你的重新创建UI方法做了很多事情,比如创建新视图。避免通过重用已经创建的对象来创建新对象


通常日历视图非常复杂。以这种方式使用多个视图很可能无法实现平滑帧率。您必须编写自定义视图,它将自动绘制。

事实证明,很多减速是由于在调试模式下运行应用程序造成的-运行应用程序通常会产生可接受的性能。

使用方法跟踪来确定您的时间花在哪里。嗯,今天的速度似乎不错。也许当时我的设备有点落后……奇怪!