Android:自定义视图太慢了?

时间:2011-08-16 05:23:42

标签: android custom-view

我有一个自定义视图,上面有很多png图像(每三个字符一个图像)。但它在绘制和滚动时太慢了。

这是我自定义视图的代码:

public class Textview extends View
{       
    private String m_szText;
    Context ctx;
    Paint mTextPaint;
    private Canvas canva;
    Bitmap b ;

    public Textview(Context context)
    {
        super(context);
        ctx = context;          
        mTextPaint= new Paint();            
        mTextPaint.setTypeface(m_tTypeface);
        mTextPaint.setStyle(Paint.Style.FILL);
    }           

    public void SetText(String newtext) {
        m_szText = newtext;
        text(newtext);
        this.invalidate();
    }
    @Override 
    protected void onDraw(Canvas canvas)
    {               
        super.onDraw(text(canvas,m_szText));
    }

    Canvas text(Canvas canvas,String txt)
    {        
        int left = 400;         
        int top = 0;        
        try {
            for(int i=0;i<txt.length();i=i+3)
            {
                String adres = "glyph/" + txt.substring(i, i+3) + ".png";               
                Bitmap btm = getBitmapFromAsset(adres);
                if(left <= 5) 
                    {left = 400;top += btm.getHeight();}
                else
                    left = left - btm.getWidth();
                canvas.drawBitmap(btm, left ,top,mTextPaint);
            }
        } catch (IOException e) {
            canvas.drawText(e.toString(), 50, 50, mTextPaint);
        }
        return canvas;
    }

    private Bitmap getBitmapFromAsset(String strName) throws IOException
    {
        AssetManager assetManager =  ctx.getAssets();
        InputStream istr = assetManager.open(strName);        
        Bitmap bitmap = BitmapFactory.decodeStream(istr);
        return bitmap;
    }

}

如何加快自定义视图的速度?我想我必须创建一次所有图像的位图。但是如何?

提前感谢!

3 个答案:

答案 0 :(得分:3)

您正在每次绘制中加载和解码几个位图。您需要提前加载位图,一次,然后绘制它们。

答案 1 :(得分:1)

You can use Thread to speed up process, and there are two way to use thread
 1)Implementing Runnable that override void run(){}
 2)or use Thread th=new Thread(new Runnable(){void run(){}
 })

答案 2 :(得分:0)

以下内容应该有所帮助。只是概述了可以做些什么。

static HashMap<String, Bitmap> mBitmaps = new HashMap<String, Bitmap>();
public void SetText(String newtext) {
    m_szText = newtext;
    makeBitmap();
    this.invalidate();
}

void makeBitmap()
{        
    for(int i=0; i<m_szText.length(); i=i+3)
        {
            String adres = "glyph/" + m_szText.substring(i, i+3) + ".png";
            Bitmap btm = null; 
            if (!mBitmaps.containsKey(adres)) {
                 btm = getBitmapFromAsset(adres);
                 mBitmaps.add(adres , btm);
            } else {
                 btm = (Bitmap)mBitmaps.get(adres); 
            }                
            length += btm.getWidth(); // considering only single  line.
        }
    // create a new blank Bitmap of height and 'length' and assign to member.
    mTextBitmap = Bitmap.createBitmap(length, height, Bitmap.Config.ARGB_8888);

    // in for loop draw all the bitmaps on mTextBitmap just like you did on canvas in ur code.
}