android開發 自定義圖文混排控件


功能:圖文混排,可自動縮放字體,如圖:

 

單點觸控使用的代碼來自:http://blog.csdn.net/xiaanming/article/details/42833893  謝謝博主!

在該demo中只是將bitmap改為了顯示圖文混排的效果,不足之處,請大家指正,共同進步!

貼上2個重要的方法:

private void MeasureBitmapTextSize(HSpannableString htr)  //計算圖文內容需要的bitmap高寬以及對應的字體大小
    {
        //根據字體大小重新計算字體大小
        int columnSpace = 5; //列間距
        int rowSpace = 5;  //行間距
        float charWidth = 0;  //每個字符占的寬度,以一個英文字符為單位
        float charHeight = 0;  //每個字符的高度
        
        Paint tryPaint = new Paint();
        tryPaint.setTextAlign(Paint.Align.CENTER);
        tryPaint.setAntiAlias(true);   //設置畫筆為無鋸齒  
//        tryPaint.setTextSize(mTextSize);
        tryPaint.setColor(mTextColor);
        
        float curRowLen = 0;
        mBmpWidth = 0;
        mBmpHeight = 0;
        int size = htr.stringList.size();
        
        int curTextSize = (int) mTextSize;
        if(mTextSize==min_textsize)
        {
            curTextSize = max_textsize;
        }
        for(;curTextSize > min_textsize;curTextSize--)  //從當前字體開始循環縮小
        {
            mBmpHeight = 0;
            tryPaint.setTextSize(curTextSize);
            FontMetrics fontMetrics = tryPaint.getFontMetrics();
            charHeight = fontMetrics.bottom - fontMetrics.top;
            curRowLen = columnSpace;
            for(int i=0;i<size;i++)
            {
                Hstring hs = htr.stringList.get(i);
                if(hs.type==Hstring.TYPE_TEXT)
                {
                    charWidth = tryPaint.measureText(hs.ch+"",0,1);
                }
                else  //圖片元素,,一個圖片占一個中文方塊字的高寬
                {
                    charWidth = tryPaint.measureText("哈"+"",0,1);
                }
                curRowLen += (columnSpace + charWidth);
                Log.e("resetBitmap", "curRowLen="+curRowLen+";charWidth="+charWidth+";mTextSize="+mTextSize+";mBmpHeight="+mBmpHeight);
                if(curRowLen > (mParentWidth-TEXT_PADDING*2))  //換行
                {
                    Log.e("resetBitmap", "TAG1:curRowLen="+curRowLen+";mParentWidth="+mParentWidth+";TEXT_PADDING="+TEXT_PADDING);
                    //需要換行
                    curRowLen = charWidth+ columnSpace;
                    mBmpWidth = (mParentWidth - TEXT_PADDING*2);
                    mBmpHeight += (charHeight+rowSpace);
                    Log.e("resetBitmap", "TAG1:mBmpWidth="+mBmpWidth+";i="+i+";size="+size);
                    if(i>=(size-1))  //說明是最后一行
                    {
                        mBmpHeight += (charHeight+rowSpace);
                    }
                }
                else if(i>=(size-1) && curRowLen > minBmpWidth && mBmpWidth!=(mParentWidth- TEXT_PADDING*2))//第一行時
                {
                    mBmpWidth = (int) curRowLen;
                    mBmpHeight = (int)(charHeight+rowSpace);
                    Log.e("resetBitmap", "TAG2:mBmpWidth="+mBmpWidth);
                    break;
                }
                else if(i>=(size-1) && mBmpWidth!=(mParentWidth- TEXT_PADDING*2))//第一行時
                {
                    mBmpWidth = minBmpWidth;
                    mBmpHeight = (int) (charHeight+rowSpace);
                    Log.e("resetBitmap", "TAG3:mBmpWidth="+mBmpWidth);
                    break;
                }
                else if(i>=(size-1) && mBmpWidth == (mParentWidth- TEXT_PADDING*2))  //當前有換行
                {
                    mBmpWidth = (mParentWidth- TEXT_PADDING*2);
                    mBmpHeight += (charHeight+rowSpace);
                    Log.e("resetBitmap", "TAG4:mBmpWidth="+mBmpWidth);
                    break;
                }
            } 
            if(mBmpHeight<=(mParentHeight - TEXT_PADDING*2) || curTextSize <=min_textsize)
            {
                mTextSize = curTextSize;
                break;
            }
        }
//        Log.v("resetBitmap", "mBmpWidth="+mBmpWidth+";mBmpHeight="+mBmpHeight);
    }

 

再次,將內容畫到bitmap上去(mBmpWidth和 mBmpHeight)

/**
     * 將圖文內容生成圖片
     * @param htr
     * @param textSize
     * @param textColor
     * @return
     */
    public Bitmap createTextBitmap(HSpannableString htr,float textSize,int textColor)
    {
        //設置初始屬性
        int columnSpace = 5; //列間距
        int rowSpace = 5;  //行間距
        float curX =0,curY=0;
//        float curRowWidth = 0; //當前所在行的實時寬度
        MeasureBitmapTextSize(htr);  //重新設置改bitmap的高寬
        if(mBmpWidth <=0 || mBmpHeight <=0)
        {
            mBmpWidth = minBmpWidth;
            mBmpHeight = minBmpHeight;
        }
        Bitmap bitmap = Bitmap.createBitmap(mBmpWidth, mBmpHeight, Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        //畫背景
        Paint bgpaint = new Paint();
        bgpaint.setColor(Color.parseColor("#CEFFCE"));
        Rect bgrect = new Rect(0, 0, mBmpWidth, mBmpHeight);
        canvas.drawRect(bgrect, bgpaint);
        //畫內容
        Paint fontPaint = new Paint();
        fontPaint.setTextAlign(Paint.Align.CENTER);
        fontPaint.setAntiAlias(true);   //設置畫筆為無鋸齒  
        fontPaint.setTextSize(textSize);
        fontPaint.setColor(textColor);
        
        // 計算每一個坐標  
//        float baseX = 0;  
//        float baseY = rowSpace;
        FontMetrics fontMetrics = fontPaint.getFontMetrics();
        float fontTotalHeight = fontMetrics.bottom - fontMetrics.top;  
//        float offY = fontTotalHeight / 2 - fontMetrics.bottom;  
//        float newY = baseY + offY;
        curX = columnSpace;
        curY = fontTotalHeight - fontMetrics.descent;
        float charWidth= 0;
        float tryCurWidth=0; //用於判斷換行的
        for(int i=0;i<htr.stringList.size();i++)
        {
            Hstring hs = htr.stringList.get(i);
            if(hs.type==Hstring.TYPE_TEXT)  //文本
            {
                charWidth = fontPaint.measureText(hs.ch+"",0,1);
                //計算會不會超出邊界
                tryCurWidth = curX + (columnSpace + charWidth);
                
                if(tryCurWidth > (mParentWidth-TEXT_PADDING*2))
                {
                    curY += rowSpace+fontTotalHeight;
                    curX = columnSpace;
                }
                else
                {
                    curX = tryCurWidth-(columnSpace + charWidth);
                }
                Log.e("createTextBitmap3", "curX="+curX+";hs.ch="+hs.ch+";charWidth="+charWidth);
                canvas.drawText(hs.ch+"", curX+charWidth/2, curY, fontPaint);
                curX += (columnSpace + charWidth);
                tryCurWidth = 0;
            }
            else  //圖片
            {
                charWidth = fontPaint.measureText("哈"+"",0,1);
                //預算會不會超出邊界
                tryCurWidth = curX + (columnSpace + charWidth);
                if(tryCurWidth > (mParentWidth-TEXT_PADDING*2))
                {
                    curY += rowSpace+fontTotalHeight;
                    curX = columnSpace;
                }
                else
                {
                    curX = tryCurWidth-(columnSpace + charWidth);
                }
                Rect srcRect = new Rect();
                srcRect.left = 0;
                srcRect.top = 0;
                srcRect.right = htr.stringList.get(i).bmp.getWidth();
                srcRect.bottom = htr.stringList.get(i).bmp.getWidth() + htr.stringList.get(i).bmp.getHeight();
                Rect bmpRect = new Rect((int)curX,(int)(curY + fontMetrics.ascent+fontMetrics.leading),(int)(curX+columnSpace + charWidth),(int)(curY -fontMetrics.ascent));
                Log.v("圖片", "curY="+curY+";ascent="+fontMetrics.ascent+";leading="+fontMetrics.leading);
                Paint bmpPaint = new Paint();
                Log.e("createTextBitmap3", "curX="+curX+";hs.ch="+hs.ch+";charWidth="+charWidth);
                canvas.drawBitmap(htr.stringList.get(i).bmp, srcRect,bmpRect, bmpPaint);
                curX += (columnSpace + charWidth);
                tryCurWidth = 0;
            }
        }
        return bitmap;
    }

 

demo下載地址:http://download.csdn.net/detail/feijian_/9014939

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM