Disposition mixte graphique et texte (centre de l'image + changement de couleur du texte)

paquet com . hoola . erciyuan . ui . widget ;
importer android.content.Context ;
importer android.graphics.Canvas ;
importer android.graphics.Paint ;
importer android.graphics.Rect ;
importer android.graphics.drawable.Drawable ;
importer android.text.style.ImageSpan ;
importer java.lang.ref.WeakReference ;
/**
 * Nom : CenterImageSpan.java
 * Auteur : lilei
 * Date : 2018/5/6
 * Commentaire : //



TODO afficher les images au centre des graphiques mixtes
 */
 public class CenterImageSpan étend ImageSpan {
 private WeakReference < Drawable > mDrawableRef ;
     public CenterImageSpan ( Context context , 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 = dessinable . getBounds () ;
        if ( fontMetricsInt != null ) {
 Paint.FontMetricsInt fmPaint = paint . getFontMetricsInt () ;
            int fontHeight = fmPaint . descente                                                   -fmPaint . _ ascension ;
            int drHauteur = rect . bas - rect . haut ;
            int centerY = fmPaint . ascend + fontHeight / 2 ;
fontMetricsInt . ascension = centreY - drHauteur / 2 ;
fontMetricsInt . haut = fontMetricsInt . ascension ;
fontMetricsInt .
                                    bas = centreY + drHauteur / 2 ;
fontMetricsInt . descente = fontMetricsInt . bas ;
}
 retour rect . droite ;
}
 @Override
 public void draw ( Canvas canvas , CharSequence text , int start , int end , float x , int top , int y ,
                      int bottom , Paint                                
        paint ) {
 Drawable drawable = getCachedDrawable () ;
toile . enregistrer () ;
Paint.FontMetricsInt fmPaint = peinture . getFontMetricsInt () ;
        int fontHeight = fmPaint . descente - fmPaint . ascension ;
        int centerY = y + fmPaint . descente - fontHeight / 2 ;
        entier                        transY = centerY - ( drawable . getBounds () . bottom - drawable . getBounds () . top ) / 2 ;
toile . translate ( x , transY ) ;
dessinable . dessiner ( toile ) ;
toile . restaurer () ;
}
 dessinable privé getCachedDrawable () {
 WeakReference <                            
            Dessinable > wr = mDrawableRef ;
Dessinable d = null ;
        si ( wr != null ) {
= wr . obtenir () ;
}
 si (== null ) {
= getDrawable () ;
mDrawableRef = new WeakReference <> ( d ) ;
}
 retour d ;
}
 }                            
                                        
            

Classe ImageSpan intégrée

Utilisation spécifique

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);


Je suppose que tu aimes

Origine blog.csdn.net/qq_25409587/article/details/80215613
conseillé
Classement