Ask the next, I think beginning of the text in edittext in drawing an icon, try to rewrite onDraw, draw, dispatchDraw methods have no effect on the eclipse xml Design view can be displayed, but to the real machine still does not show up , it is really strange. The following is my code
public class SearchEdit extends EditText {
/ ** Icon on the left * /
private Drawable mDrawableLeft;
public SearchEdit (Context context) {
super (context);
}
public SearchEdit (Context context, AttributeSet attrs) {
super (context, attrs);
}
public SearchEdit (Context context, AttributeSet attrs, int defStyle) {
super (context, attrs, defStyle);
}
public void setCompoundDrawables (Drawable left, Drawable top,
Drawable right, Drawable bottom) {
mDrawableLeft = left;
/ / TODO Auto-generated method stub
super.setCompoundDrawables (null, top, right, bottom);
}
private int [] coordinates = new int [2];
private Matrix identityMatrix = new Matrix ();
@ Override
public void onDraw (Canvas canvas) {
super.onDraw (canvas);
if (mDrawableLeft == null)
return;
canvas.save ();
int comPad = getCompoundDrawablePadding ();
int w = getWidth ();
int h = getHeight ();
int pl = getPaddingLeft ();
int pt = getPaddingTop ();
int pr = getPaddingRight ();
int pb = getPaddingBottom ();
Layout layout = getLayout ();
/ / Compute left drawable size to fit this view
int imgW = mDrawableLeft.getIntrinsicWidth ();
int imgH = mDrawableLeft.getIntrinsicHeight ();
int validH = h - pt - pb;
FontMetrics fm = getPaint () getFontMetrics ();.
validH = (int) ((fm.descent - fm.ascent) * 0.85);
if (imgH> validH)
{
float scale = validH * 1.0f / imgH;
imgH = validH;
imgW = (int) (imgW * scale);
}
/ / Draw left icon
String text = getText () toString ();.
int left = 0;
int top = ((h - imgH) >> 1);
if (text! = null && text.length ()! = 0)
{
/ / When this edit has text in it
left = (int) layout.getLineLeft (0) - imgW;
}
else
{
/ ***
* Because there's a bug in EditText when attribute singleLine = true,
* So must do this to move the canvas's positon back, otherwise you
* Will draw imagine to the incorrect coordnation!
** /
getLocationInWindow (coordinates);
canvas.setMatrix (identityMatrix);
canvas.translate (coordinates [0], coordinates [1]);
/ / Compute icon left of hit text
CharSequence chs = getHint ();
if (chs! = null)
text = chs.toString ();
int textWidth = 0;
if (text! = null && text.length ()! = 0)
{
TextPaint paint = getPaint ();
textWidth = (int) layout.getDesiredWidth (text, paint);
}
int gravity = getGravity () & Gravity.HORIZONTAL_GRAVITY_MASK;
if (gravity == Gravity.LEFT)
{
left = pl + textWidth + comPad;
}
else if (gravity == Gravity.CENTER_HORIZONTAL)
{
left = ((w - textWidth) >> 1) + pl - imgW - comPad;
}
else
left = w - textWidth - pr - imgW - comPad;
}
if (left> = pl && (left + imgW
mDrawableLeft.setBounds (left, top, left + imgW, top + imgH);
mDrawableLeft.draw (canvas);
}
canvas.restore ();
}
}
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->Reply:
Get the original TextView in onPaint already coordinates the canvas were offset in accordance with scrollX original coordinates must add getScrollX 0:00 to play, correct code:
Reply:
super.onDraw (canvas);
if (mDrawableLeft == null)
return;
canvas.save ();
int comPad = getCompoundDrawablePadding ();
int w = getWidth ();
int h = getHeight ();
int pl = getPaddingLeft ();
int pt = getPaddingTop ();
int pr = getPaddingRight ();
int pb = getPaddingBottom ();
Layout layout = getLayout ();
/ / Compute left drawable size to fit this view
int imgW = mDrawableLeft.getIntrinsicWidth ();
int imgH = mDrawableLeft.getIntrinsicHeight ();
int validH = h - pt - pb;
FontMetrics fm = getPaint () getFontMetrics ();.
validH = (int) ((fm.descent - fm.ascent) * 0.85);
if (imgH> validH)
{
float scale = validH * 1.0f / imgH;
imgH = validH;
imgW = (int) (imgW * scale);
}
/ / Draw left icon
String text = getText () toString ();.
int left = 0;
int top = ((h - imgH) >> 1);
int scrollX = getScrollX ();
if (text! = null && text.length ()! = 0)
{
/ / When this edit has text in it
left = (int) (w - layout.getLineWidth (0)) / 2 - imgW;
left - = comPad;
}
else
{
/ / Compute icon left of hit text
CharSequence chs = getHint ();
if (chs! = null)
text = chs.toString ();
int textWidth = 0;
if (text! = null && text.length ()! = 0)
{
TextPaint paint = getPaint ();
textWidth = (int) layout.getDesiredWidth (text, paint);
}
int gravity = getGravity () & Gravity.HORIZONTAL_GRAVITY_MASK;
if (gravity == Gravity.LEFT)
{
left = pl + textWidth + comPad;
}
else if (gravity == Gravity.CENTER_HORIZONTAL)
{
left = ((w - textWidth) >> 1) + pl - imgW - comPad;
}
else
left = w - textWidth - pr - imgW - comPad;
}
if (left> = pl && ((left + imgW) <(w - pr)))
{
mDrawableLeft.setBounds (left + scrollX, top, left + scrollX + imgW, top + imgH);
mDrawableLeft.draw (canvas);
}
canvas.restore ();
}
Reply:

Reply:
Congratulations and scattered it.

Reply:
Congratulations! Find scattered points!
No comments:
Post a Comment