2015-04-25 7 views
5

Per migliorare il rendering TextView in Instagram, gli ingegneri di Instagram forniscono un trucco in here, usano una vista personalizzata (TextLayoutView) per memorizzare il testo. in questo post, non ci danno una demo o ci dicono come usarlo, quindi se voglio usare questo hack, come potrei fare?Come utilizzare "TextLayoutView" di Instagram per migliorare il rendering TextView su Android

+0

ho creato GitHub Repo per esso. https://github.com/Kishanjvaghela/TextLabel –

risposta

2

Questa è la mia semplice implementazione:

TextLayoutView:

public class TextLayoutView extends View { 

private Layout mLayout; 

public TextLayoutView(Context context) { 
    this(context, null); 
} 

public TextLayoutView(Context context, AttributeSet attrs) { 
    this(context, attrs, 0); 
} 

public TextLayoutView(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
    setFocusable(true); 
} 

@Override 
protected void onDraw(Canvas canvas) { 
    long t1=System.currentTimeMillis(); 
    super.onDraw(canvas); 
    canvas.save(); 
    if (mLayout != null) { 
     canvas.translate(getPaddingLeft(), getPaddingTop()); 
     mLayout.draw(canvas); 
    } 

    canvas.restore(); 
    long t2=System.currentTimeMillis(); 
    Log.i("TEST", "onDraw::"+(t2-t1)); 
} 

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    long t1=System.currentTimeMillis(); 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
    if (mLayout != null) { 
     setMeasuredDimension(
       getPaddingLeft() + getPaddingRight() + mLayout.getWidth(), 
       getPaddingTop() + getPaddingBottom() + mLayout.getHeight()); 
    } 
    long t2=System.currentTimeMillis(); 
    Log.i("TEST", "onMeasure::"+(t2-t1)); 
} 


public void setTextLayout(Layout layout) { 
    mLayout = layout; 
    requestLayout(); 
}} 

si può usare solo questo:

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    ...... 
    viewHolder.textView.setTextLayout(getLayout(mList.get(position))); 
    return convertView; 
} 
    private final Map<String, Layout> mLayoutMap = new ConcurrentHashMap<String, Layout>(); 
private Layout getLayout(String str) { 
    Layout layout = mLayoutMap.get(str); 
    if (layout == null) { 
     TextPaint textPaint = new TextPaint(); 
     textPaint.setTextSize(20); 
     layout = new StaticLayout(str, textPaint, width, Alignment.ALIGN_CENTER, 
       1.0f, 0.0f, true); 
     mLayoutMap.put(str, layout); 
    } 
    return layout; 
} 
+0

Ho creato GitHub Repo per questo. https://github.com/Kishanjvaghela/TextLabel –

Problemi correlati