程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> java完成簡略的搜刮引擎

java完成簡略的搜刮引擎

編輯:關於JAVA

java完成簡略的搜刮引擎。本站提示廣大學習愛好者:(java完成簡略的搜刮引擎)文章只能為提供參考,不一定能成為您想要的結果。以下是java完成簡略的搜刮引擎正文


  在項目中有一個小功效須要完成,就是對多行文本停止排版結構,每行的內容又分為兩部門,右邊為題目,左邊為描寫,右邊內容長度不肯定,左邊的內容須要對齊,若有換行也須要對齊左邊的文本。

1、後果圖

       

可以看到內容分紅了兩部門,右邊的色彩與左邊紛歧致,左邊的描寫案牍同一對齊。

2、完成計劃

       以上功效,因為輸出內容輸出行數不肯定,而且右邊的案牍長度也不肯定,是以不克不及直接在結構中完成,基於此這裡重要完成了以下6種方法

計劃1

       采取自界說控件的方法,繼續TextView,從新onDraw函數,完成以下:

/**
 * 盤算出右邊最長的顯示字符串maxLeftWidth,以後draw每行字符,左邊的描寫從maxLeftWidth開端draw
 * 當一行顯示不完整時,折行而且空出maxLeftWidth的空格長度
 */
public class TypographyView1 extends TextView {

  private Paint leftPaint = new Paint();
  private Paint rightPaint = new Paint();
  private int fullWidth;
  private float textSize;
  private JSONArray array;
  private int middlePadding = 0;
  float maxLeftWidth = 0;
  int itemSize = 0;

  public TypographyView1(Context context) {
    super(context);
    init();
  }

  public TypographyView1(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
  }

  public TypographyView1(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
  }

  private void init() {
    textSize = getResources().getDimensionPixelSize(R.dimen.text_size_13);
    leftPaint.setAntiAlias(true);
    leftPaint.setTextSize(textSize);
    leftPaint.setColor(getResources().getColor(R.color.color_black_999999));
    rightPaint.setAntiAlias(true);
    rightPaint.setTextSize(textSize);
    rightPaint.setColor(getResources().getColor(R.color.color_black));
    middlePadding = getResources().getDimensionPixelSize(R.dimen.padding_value);
  }

  @Override
  protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    fullWidth = getWidth();// 全部textView的寬度
  }

  public void setText(JSONArray array) {
    this.array = array;
    if (array != null) {
      try {
        int size = itemSize = array.length();
        for (int i = 0; i < size; ++i) {
          JSONArray o = (JSONArray) array.get(i);
          String key = o.getString(0);
          String value = o.getString(1);
          if (TextUtils.isEmpty(key) || TextUtils.isEmpty(value)) {
            itemSize--;
            continue;
          }
          float curWidth = leftPaint.measureText(key);
          if (curWidth > maxLeftWidth) {
            maxLeftWidth = curWidth;
          }
        }
        maxLeftWidth = maxLeftWidth + middlePadding;
        invalidate();
      } catch (Exception e) {

      }
    }
  }

  boolean setHeight = false;

  @Override
  protected void onDraw(Canvas canvas) {
    if (array == null) {
      return;
    }
    int lineCount = 0;
    try {
      JSONArray item;
      float offsetY;
      for (int i = 0; i < itemSize; ++i) {
        item = (JSONArray) array.get(i);
        offsetY = (lineCount + 1) * textSize;
        canvas.drawText(item.getString(0), 0, offsetY, leftPaint);

        String value = item.getString(1);
        float valueWidth = rightPaint.measureText(value);
        if (valueWidth > fullWidth - maxLeftWidth) {// 一行顯示不完
          char[] textCharArray = value.toCharArray();
          float charWidth;
          float drawWidth = maxLeftWidth;
          for (int j = 0; j < textCharArray.length; j++) {
            charWidth = rightPaint.measureText(textCharArray, j, 1);
            if (fullWidth - drawWidth < charWidth) {
              lineCount++;
              drawWidth = maxLeftWidth;
              offsetY += textSize;
            }
            canvas.drawText(textCharArray, j, 1, drawWidth, offsetY, rightPaint);
            drawWidth += charWidth;
          }
        } else {
          canvas.drawText(value, maxLeftWidth, offsetY, rightPaint);
        }
        lineCount += 2;
      }
      if (!setHeight) {
        setHeight((lineCount + 1) * (int) textSize);
        setHeight = true;
      }
    } catch (JSONException e) {
      e.printStackTrace();
    }
  }
}

       添加了setText(JSONArray array)作為數據輸出,而且在這外面丈量了右邊title的最年夜寬度,以後挪用invalidate觸發重繪,在onSizeChanged獲得全部控件的寬度,重繪會挪用onDraw函數,這裡不須要挪用super函數,TextView的onDraw函數做了異常多的操作,解析傳入的數據,分離一行一行挪用canvas來停止drawText操作,當繪制描寫時,先盤算寬度,假如跨越殘剩控件解釋須要換行,最初挪用setHeight設置高度,這個加一個斷定前提,由於會觸發requestLayout()停止從新結構和invalidate()停止重繪,假如不加斷定會一向重繪。

計劃2

       方法2與方法1差不多,分歧為一切盤算都在onDraw函數中:

/**
 * 該方法與方法1很相似,只是一切的盤算都放在了onDraw辦法中。
 */
public class TypographyView2 extends TextView {

  private Paint paint1 = new Paint();
  private Paint paint2 = new Paint();
  private int middlePadding = 0;
  int width;
  private float textSize;
  private JSONArray array;

  public TypographyView2(Context context) {
    super(context);
    init();
  }

  public TypographyView2(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
  }

  public TypographyView2(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
  }

  private void init() {
    textSize = getResources().getDimensionPixelSize(R.dimen.text_size_13);
    paint1.setAntiAlias(true);
    paint1.setTextSize(textSize);
    paint1.setColor(getResources().getColor(R.color.color_black_999999));
    paint2.setAntiAlias(true);
    paint2.setTextSize(textSize);
    paint2.setColor(getResources().getColor(R.color.color_black));
    middlePadding = getResources().getDimensionPixelSize(R.dimen.padding_value);
  }

  @Override
  protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    width = getWidth();// 全部textView的寬度
  }

  public void setText(JSONArray array) {
    this.array = array;
    if (array != null) {
      invalidate();
    }
  }

  boolean setHeight = false;

  @Override
  protected void onDraw(Canvas canvas) {
    // super.onDraw(canvas);
    int lineCount = 0;
    int size = array.length();
    float maxLeftWidth = 0;
    float drawWidth = 0;
    try {
      for (int i = 0; i < size; ++i) {
        JSONArray o = (JSONArray) array.get(i);
        String key = o.getString(0);
        float v = paint1.measureText(key);
        if (v > maxLeftWidth) {
          maxLeftWidth = v;
        }
      }
      maxLeftWidth = maxLeftWidth + middlePadding;
      for (int i = 0; i < size; ++i) {
        JSONArray o = (JSONArray) array.get(i);
        String key = o.getString(0);
        canvas.drawText(key, 0, (lineCount + 1) * textSize, paint1);
        String value = o.getString(1);
        char[] textCharArray = value.toCharArray();
        float charWidth;
        drawWidth = maxLeftWidth;
        for (int j = 0; j < textCharArray.length; j++) {
          charWidth = paint1.measureText(textCharArray, j, 1);
          if (width - drawWidth < charWidth) {
            lineCount++;
            drawWidth = maxLeftWidth;
          }
          canvas.drawText(textCharArray, j, 1, drawWidth, (lineCount + 1) * textSize, paint2);
          drawWidth += charWidth;
        }
        lineCount += 2;
      }
      if (!setHeight) {
        setHeight((lineCount + 1) * (int) textSize + 5);
        setHeight = true;
      }
    } catch (JSONException e) {
      e.printStackTrace();
    }
  }
}

       該計劃的完成是不太好的,計劃1也是在此基本長進行調劑的,在這裡放出來只是為了解釋,一切的盤算不要全體放在onDraw外面,由於該辦法能夠會重復挪用屢次,如許就下降了機能。

計劃3

       將數據源拼接成SpannableString,重寫onDraw函數,依據內容draw每個字符:

/**
 * 該辦法,是須要顯示的內容先拼接成SpannableString,在onDraw辦法中獲得一切的char字符,一個一個比擬
 * 當為分號是,表現為key與value的分隔符。
 */
public class TypographyView3 extends TextView {

  private Paint leftPaint = new Paint();
  private Paint rightPaint = new Paint();
  int width;
  private String text;
  private float textSize;
  float maxLeftWidth = 0;
  private int middlePadding = 0;

  public TypographyView3(Context context) {
    super(context);
    init();
  }

  public TypographyView3(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
  }

  public TypographyView3(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
  }

  private void init() {
    textSize = getResources().getDimensionPixelSize(R.dimen.text_size_13);
    textSize = getResources().getDimensionPixelSize(R.dimen.text_size_13);
    leftPaint.setAntiAlias(true);
    leftPaint.setTextSize(textSize);
    leftPaint.setColor(getResources().getColor(R.color.color_black_999999));
    rightPaint.setAntiAlias(true);
    rightPaint.setTextSize(textSize);
    rightPaint.setColor(getResources().getColor(R.color.color_black));
    middlePadding = getResources().getDimensionPixelSize(R.dimen.padding_value);
  }

  @Override
  protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    width = getWidth();// 全部textView的寬度
  }

  public void setText(JSONArray data) {
    if (data == null) {
      return;
    }
    try {
      int size = data.length();
      for (int i = 0; i < size; ++i) {
        JSONArray o = (JSONArray) data.get(i);
        String key = o.getString(0);
        float v = leftPaint.measureText(key);
        if (v > maxLeftWidth) {
          maxLeftWidth = v;
        }
      }
      maxLeftWidth += middlePadding;
      SpannableStringBuilder ssb = new SpannableStringBuilder();
      for (int i = 0; i < size; ++i) {
        addItem((JSONArray) data.get(i), ssb, i != 0);
      }
      setText(ssb, BufferType.SPANNABLE);
    } catch (Exception e) {

    }
  }

  private void addItem(JSONArray item, SpannableStringBuilder ssb, boolean breakLine) {
    try {
      if (item == null || item.length() == 0) {
        return;
      }
      String key = item.getString(0);
      String value = (item.length() >= 2) ? item.getString(1) : "";
      if (TextUtils.isEmpty(key) && TextUtils.isEmpty(value)) {
        return;
      }
      if (breakLine) {// 換行
        ssb.append("\r\n");
        ssb.append("\r\n");
      }
      SpannableString span = new SpannableString(key);
      //      span.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.colorAccent)), 0, key
      // .length(),
      //          Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
      ssb.append(span);
      ssb.append(value);

    } catch (JSONException e) {
      e.printStackTrace();
    }
  }

  @Override
  protected void onDraw(Canvas canvas) {
    // super.onDraw(canvas);
    int lineCount = 0;
    text = this.getText().toString();
    if (text == null)
      return;
    char[] textCharArray = text.toCharArray();
    // 已繪的寬度
    float drawWidth = 0;
    float charWidth;
    Paint paint = leftPaint;
    for (int i = 0; i < textCharArray.length; i++) {
      charWidth = leftPaint.measureText(textCharArray, i, 1);

      if (textCharArray[i] == '\n') {
        lineCount++;
        drawWidth = 0;
        paint = leftPaint;
        continue;
      }
      if (width - drawWidth < charWidth) {
        lineCount++;
        drawWidth = maxLeftWidth;
      }
      if (i > 1 && textCharArray[i - 1] == ':') {
        drawWidth = maxLeftWidth;
        paint = rightPaint;
      }
      canvas.drawText(textCharArray, i, 1, drawWidth, (lineCount + 1) * textSize, paint);
      drawWidth += charWidth;
    }
    //may be need set height
    //setHeight((lineCount + 1) * (int) textSize + 5);
  }
}

       這裡先盤算右邊title的最年夜寬度,同時將一切的數據拼接成一個SpannableStringBuilder,挪用setText函數會觸發重繪,在onDraw函數中停止處置,因為未從新super函數,是以SpannableString的setSpan函數掉效,該計劃重要依據分隔符來停止朋分,是以分隔符須要獨一。

計劃4

       采取GridLayout方法完成,然則原始控件有展現成績,是以對此停止了修正:

public class Typography4Activity extends BaseActivity {

  public static void start(Context context) {
    Intent intent = new Intent();
    intent.setClass(context, Typography4Activity.class);
    context.startActivity(intent);
  }

  private LinearLayout root;
  private Paint leftPaint = new Paint();
  private float textSize;
  private float maxLeftWidth;
  private int middlePadding = 0;
  private float maxRightWidth;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    root = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.activity_typography4, null);
    setContentView(root);
    initPaint();
    findViews();
    loadData();
  }

  private void initPaint() {
    textSize = getResources().getDimensionPixelSize(R.dimen.text_size_13);
    leftPaint.setAntiAlias(true);
    leftPaint.setTextSize(textSize);
    leftPaint.setColor(getResources().getColor(R.color.color_black_999999));
    middlePadding = getResources().getDimensionPixelSize(R.dimen.padding_value);
  }

  private void findViews() {

  }

  private void loadData() {
    addGridLayout(DataSource.getArray());
    TextView view = new TextView(this);
    view.setText("修正後的完成");
    view.setGravity(Gravity.CENTER);
    view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 160));
    root.addView(view);
    addModifyGridLayout(DataSource.getArray());
  }

  private void addGridLayout(JSONArray data) {
    try {
      GridLayout layout = createGridLayout();
      int size = data.length();
      for (int i = 0; i < size; ++i) {
        JSONArray item = (JSONArray) data.get(i);
        String key = item.getString(0);
        String value = (item.length() >= 2) ? item.getString(1) : "";
        GridLayout.Spec row = GridLayout.spec(i);
        GridLayout.Spec col1 = GridLayout.spec(0);
        GridLayout.Spec col2 = GridLayout.spec(1);
        GridLayout.LayoutParams params = new GridLayout.LayoutParams(row, col1);

        TextView title = getLeftTextView(key);
        layout.addView(title, params);

        params = new GridLayout.LayoutParams(row, col2);
        TextView desc = getRightTextView(value);
        layout.addView(desc, params);
      }
      root.addView(layout);
    } catch (Exception e) {

    }
  }

  @NonNull
  private TextView getRightTextView(String value) {
    TextView desc = new TextView(this);
    desc.setTextSize(13);
    desc.setTextColor(getResources().getColor(R.color.black));
    desc.setText(value);
    return desc;
  }

  @NonNull
  private TextView getLeftTextView(String key) {
    TextView title = new TextView(this);
    title.setText(key);
    title.setPadding(0, middlePadding, middlePadding, 0);
    title.setTextColor(getResources().getColor(R.color.color_black_999999));
    title.setTextSize(13);
    return title;
  }

  private void addModifyGridLayout(JSONArray data) {
    try {
      calculateLeftMaxWidth(data);
      GridLayout layout = createGridLayout();
      int size = data.length();
      for (int i = 0; i < size; ++i) {
        JSONArray item = (JSONArray) data.get(i);
        GridLayout.Spec row = GridLayout.spec(i);

        String key = item.getString(0);
        GridLayout.Spec col1 = GridLayout.spec(0);
        GridLayout.LayoutParams params = new GridLayout.LayoutParams(row, col1);

        TextView title = getLeftTextView(key);
        layout.addView(title, params);

        String value = (item.length() >= 2) ? item.getString(1) : "";
        GridLayout.Spec col2 = GridLayout.spec(1);
        params = new GridLayout.LayoutParams(row, col2);

        TextView desc = getRightTextView(value);
        params.width = (int) maxRightWidth;
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        layout.addView(desc, params);
      }
      root.addView(layout);
    } catch (Exception e) {

    }
  }

  private void calculateLeftMaxWidth(JSONArray data) {
    try {
      DisplayUtil.init(this);// 這個可以在運用法式起來的時刻init
      int size = data.length();
      for (int i = 0; i < size; ++i) {
        JSONArray o = (JSONArray) data.get(i);
        String key = o.getString(0);
        String value = o.getString(1);
        if (TextUtils.isEmpty(key) || TextUtils.isEmpty(value)) {
          continue;
        }
        float curWidth = leftPaint.measureText(key);
        if (curWidth > maxLeftWidth) {
          maxLeftWidth = curWidth;
        }
      }
      maxLeftWidth = maxLeftWidth + middlePadding;
      maxRightWidth = DisplayUtil.screenWidth - DisplayUtil.dp2px(this, 32 + 10) - maxLeftWidth;
    } catch (Exception e) {

    }
  }

  private GridLayout createGridLayout() {
    GridLayout layout = new GridLayout(this);
    layout.setColumnCount(2);
    //layout.setRowCount(5);
    layout.setOrientation(GridLayout.HORIZONTAL);
    return layout;
  }
}

       假如直接創立一個GridLayout,外面添加每項,假如描寫太長都招致顯示不全,這個是體系的一個bug,盤算的寬度有成績,是以須要對此計劃停止更改。
       更改方法為先盤算右邊占用的最年夜寬度,在添加左邊的項時,設置結構參數掌握最年夜的長度。

計劃5

       采取每行一個結構,手動一行一行停止添加:

public class Typography5Activity extends BaseActivity {

  public static void start(Context context) {
    Intent intent = new Intent();
    intent.setClass(context, Typography5Activity.class);
    context.startActivity(intent);
  }


  private LinearLayout root;
  private Paint leftPaint = new Paint();
  private float textSize;
  private float maxLeftWidth;
  private int middlePadding = 0;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    root = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.activity_typography5, null);
    setContentView(root);
    initPaint();
    loadData();
  }

  private void initPaint() {
    textSize = getResources().getDimensionPixelSize(R.dimen.text_size_13);
    leftPaint.setAntiAlias(true);
    leftPaint.setTextSize(textSize);
    leftPaint.setColor(getResources().getColor(R.color.color_black_999999));
    middlePadding = getResources().getDimensionPixelSize(R.dimen.padding_value);
  }

  private void loadData() {
    JSONArray array = DataSource.getArray();
    calculateLeftMaxWidth(array);
    if (array != null) {
      try {
        int size = array.length();
        for (int i = 0; i < size; ++i) {
          JSONArray o = (JSONArray) array.get(i);
          String key = o.getString(0);
          String value = o.getString(1);
          addItem(key, value);
        }
      } catch (Exception e) {

      }
    }
  }

  private void calculateLeftMaxWidth(JSONArray data) {
    try {
      int size = data.length();
      for (int i = 0; i < size; ++i) {
        JSONArray o = (JSONArray) data.get(i);
        String key = o.getString(0);
        String value = o.getString(1);
        if (TextUtils.isEmpty(key) || TextUtils.isEmpty(value)) {
          continue;
        }
        float curWidth = leftPaint.measureText(key);
        if (curWidth > maxLeftWidth) {
          maxLeftWidth = curWidth;
        }
      }
      maxLeftWidth = maxLeftWidth + middlePadding;
    } catch (Exception e) {

    }
  }

  private void addItem(String key, String value) {
    LinearLayout layout = getItemLayout();
    TextView left = (TextView) layout.findViewById(R.id.left);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
        ViewGroup.LayoutParams.WRAP_CONTENT);

    params.width = (int) maxLeftWidth;
    left.setLayoutParams(params);
    left.setText(key);

    TextView right = (TextView) layout.findViewById(R.id.right);
    right.setText(value);

    root.addView(layout);
  }

  private LinearLayout getItemLayout() {
    LinearLayout layout = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.compose_item_layout, null);
    return layout;
  }
}

       改計劃也須要先盤算右邊的最年夜占用寬度,來設置左邊占用的年夜小,每項的結構以下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="horizontal"
  android:paddingTop="@dimen/text_padding_10"
  tools:context=".activity.Typography1Activity">

  <TextView
    android:id="@+id/left"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="@dimen/text_padding_10"
    android:textColor="@color/color_black_999999"
    android:textSize="@dimen/text_size_13"/>

  <TextView
    android:id="@+id/right"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textColor="@color/black"
    android:textSize="@dimen/text_size_13"/>


</LinearLayout>

       每行有兩個TextView,右邊寬度為自順應,左邊占領剩下閣下的地位,在盤算出右邊最年夜寬度後,從新設置右邊每個TextView占用的寬度。

計劃6

       方法與1差不多,然則不在繼續TextView,而是直接繼續View:

public class TypographyView4 extends View {

  private Paint leftPaint = new Paint();
  private Paint rightPaint = new Paint();
  private int fullWidth;
  private float textSize;
  private JSONArray array;
  private int middlePadding = 0;
  float maxLeftWidth = 0;
  int itemSize = 0;

  public TypographyView4(Context context) {
    super(context);
    init();
  }

  public TypographyView4(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
  }

  public TypographyView4(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
  }

  private void init() {
    textSize = getResources().getDimensionPixelSize(R.dimen.text_size_13);
    leftPaint.setAntiAlias(true);
    leftPaint.setTextSize(textSize);
    leftPaint.setColor(getResources().getColor(R.color.color_black_999999));
    rightPaint.setAntiAlias(true);
    rightPaint.setTextSize(textSize);
    rightPaint.setColor(getResources().getColor(R.color.color_black));
    middlePadding = getResources().getDimensionPixelSize(R.dimen.padding_value);
  }

  @Override
  protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    fullWidth = getWidth();// 全部textView的寬度
  }

  public void setText(JSONArray array) {
    this.array = array;
    if (array != null) {
      try {
        int size = itemSize = array.length();
        for (int i = 0; i < size; ++i) {
          JSONArray o = (JSONArray) array.get(i);
          String key = o.getString(0);
          String value = o.getString(1);
          if (TextUtils.isEmpty(key) || TextUtils.isEmpty(value)) {
            itemSize--;
            continue;
          }
          float curWidth = leftPaint.measureText(key);
          if (curWidth > maxLeftWidth) {
            maxLeftWidth = curWidth;
          }
        }
        maxLeftWidth = maxLeftWidth + middlePadding;
        invalidate();
      } catch (Exception e) {

      }
    }
  }

  @Override
  protected void onDraw(Canvas canvas) {
    if (array == null) {
      return;
    }
    int lineCount = 0;
    try {
      JSONArray item;
      float offsetY;
      for (int i = 0; i < itemSize; ++i) {
        item = (JSONArray) array.get(i);
        offsetY = (lineCount + 1) * textSize;
        canvas.drawText(item.getString(0), 0, offsetY, leftPaint);

        String value = item.getString(1);
        float valueWidth = rightPaint.measureText(value);
        if (valueWidth > fullWidth - maxLeftWidth) {// 一行顯示不完
          char[] textCharArray = value.toCharArray();
          float charWidth;
          float drawWidth = maxLeftWidth;
          for (int j = 0; j < textCharArray.length; j++) {
            charWidth = rightPaint.measureText(textCharArray, j, 1);
            if (fullWidth - drawWidth < charWidth) {
              lineCount++;
              drawWidth = maxLeftWidth;
              offsetY += textSize;
            }
            canvas.drawText(textCharArray, j, 1, drawWidth, offsetY, rightPaint);
            drawWidth += charWidth;
          }
        } else {
          canvas.drawText(value, maxLeftWidth, offsetY, rightPaint);
        }
        lineCount += 2;
      }
    } catch (JSONException e) {
      e.printStackTrace();
    }
  }
}

       該計劃重要繼續自View,不再繼續TextView,因為在在上述計劃中不在挪用super,是以TextView曾經退步為一個View,是以直接繼續View。

總結

       由於右邊的寬度不肯定,是以一切的計劃都停止了異樣的一個操作,就是丈量了右邊顯示的最年夜寬度,後續的操作再依據該寬度停止調劑。上述的計劃中1,2,3,6都只需用一個View來停止顯示,4,5都須要多個View停止顯示。

 完全的代碼可以在檢查鏈接長進行檢查。

以上就是本文的全體內容,願望對年夜家的進修有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved