Android:自定义视图太慢?

发布于 2024-11-29 12:01:25 字数 1823 浏览 0 评论 0原文

我有一个自定义视图,上面有很多 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;
    }

}

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

提前致谢!

I have a custom view with a lot of png images on it(For every three characters an image). but it is too slow on drawing and scrolling.

It is my code for custom view:

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

}

How can I speed up my custom view? I think I must to create bitmap of all images once. but how to?

thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧城烟雨 2024-12-06 12:01:25

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

You are loading and decoding several bitmaps on every draw. You need to load the bitmaps ahead of time, once and then draw them.

玻璃人 2024-12-06 12:01:25
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(){}
 })
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(){}
 })
毁梦 2024-12-06 12:01:25

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

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.
}

The following should help. just outline of what can be done.

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.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文