Diseño mixto de gráfico y texto (centro de la imagen + cambio de color del texto)

paquete com . hoolai _ erciyuán . ui _ artilugio ;
importar android.content.Context ;
importar android.graphics.Canvas ;
importar android.graphics.Paint ;
importar android.graphics.Rect ;
importar android.graphics.drawable.Drawable ;
importar android.text.style.ImageSpan ;
importar java.lang.ref.WeakReference ;
/**
 * Nombre: CenterImageSpan.java
 * Autor: lilei
 * Fecha: 2018/5/6
 * Comentario: //



TODO mostrar imágenes en el centro de gráficos mixtos
 */
 clase pública CenterImageSpan extiende ImageSpan {
 private WeakReference < Drawable > mDrawableRef ;
     public CenterImageSpan ( contexto contexto , int resourceId , int verticalAlignment ) {
 super ( context , resourceId , verticalAlignment ) ;
 }
 @Override
 public int getSize ( Paint paint , 
    
            
        CharSequence text , int start , int end ,
 Paint.FontMetricsInt fontMetricsInt ) {
 Drawable drawable = getDrawable () ;
Rect rect = dibujable . obtenerLímites () ;
        if ( fontMetricsInt != null ) {
 Paint.FontMetricsInt fmPaint = pintura . getFontMetricsInt () ;
            int fontHeight = fmPaint . descendencia                                                   - pintura fm . ascenso ;
            int drAltura = rect . inferior - rect . arriba ;
            int centroY = fmPaint . ascenso + fontHeight / 2 ;
fontMetricsInt . ascenso = centerY - drHeight / 2 ;
fontMetricsInt . arriba = fontMetricsInt . ascenso ;
fontMetricsInt .
                                    inferior = centerY + drHeight / 2 ;
fontMetricsInt . descenso = fontMetricsInt . fondo ;
}
 devolver rect . derecho ;
}
 @Override
 public void draw ( Canvas canvas , CharSequence text , int start , int end , float x , int top , int y ,
                      int bottom , Paint                                
        pintar ) {
 dibujable dibujable = getCachedDrawable () ;
lona _ guardar () ;
Paint.FontMetricsInt fmPaint = pintura . getFontMetricsInt () ;
        int fontHeight = fmPaint . descenso - fmPaint . ascenso ;
        int centerY = y + fmPaint . descenso - fontHeight / 2 ;
        En t                        transY = centerY - ( dibujable . getBounds () . bottom - dibujable . getBounds () . top ) / 2 ;
lona _ traducir ( x , transY ) ;
dibujable _ dibujar ( lienzo ) ;
lona _ restaurar () ;
}
 privado Drawable getCachedDrawable () {
 WeakReference <                            
            Dibujable > wr = mDrawableRef ;
Dibujable d = nulo;
        si ( wr != nulo ) {
 re = wr . obtener () ;
}
 if ( d == null ) {
 d = getDrawable ( ) ;
mDrawableRef = nueva referencia débil <> ( d ) ;
}
 devuelve d ;
}
 }                            
                                        
            

Clase ImageSpan integrada

Uso específico

TextView txt_explain = view.findViewById(R.id.txt_personal_dialog_explain);
String explain = txt_explain.getText().toString();
int start = explain.length();
String explain_pink = "变色字体";
int end = explain.length() + explain_pink.length();
SpannableString spannableString = new SpannableString(explain + explain_pink);
//设置颜色
spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#ff427b")), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

//设置问号
ImageSpan image = new CenterImageSpan(mContext, R.mipmap.ic_question, DynamicDrawableSpan.ALIGN_BASELINE);

spannableString.setSpan(image, end - 1, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
txt_explain.setText(spannableString);


Supongo que te gusta

Origin blog.csdn.net/qq_25409587/article/details/80215613
Recomendado
Clasificación