安卓:刷新位图新数据 [英] Android: Refresh bitmap with new data

查看:184
本文介绍了安卓:刷新位图新数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建的随机像素颜色15fps的位图视频。下面code正确地显示一个位图,但不更新/一个新的随机的刷新图像。任何人都可以请帮我找出为什么?

I am trying to create a 15fps "bitmap video" of random pixel colors. The following code displays one bitmap correctly, however does not update/refresh the image with a new random one. Can anyone please help me figure out why?

public class ViewThread extends Thread {
    private SurfaceHolder mHolder;
    private Panel mPanel;
    private boolean mRun = false;

    public ViewThread( Panel panel) {
        mPanel = panel;
        mHolder = mPanel.getHolder();
    }

    public void setRunning(boolean run) {
        mRun = run;
    }

    @Override
    public void run() {
        Canvas canvas = null;
        while (mRun) {
            canvas = mHolder.lockCanvas();
            if (canvas != null) {
                mPanel.doDraw(canvas);
                mHolder.unlockCanvasAndPost(canvas);
            }
        }
    }
}

public class Panel extends SurfaceView implements SurfaceHolder.Callback {
    private ViewThread mThread;
    private Bitmap mBitmap;
    private int[]   mcolors;

    public Panel(Context context) {
        super(context);
        init();
    } 

    private void init() {
        getHolder().addCallback(this);
        mThread = new ViewThread(this);

        // create a graphic
        mcolors = colorMap(); 
        int[] colors = mcolors; 
        mBitmap = Bitmap.createBitmap(colors, 64, 64, Bitmap.Config.ARGB_8888);  
        mBitmap = Bitmap.createScaledBitmap(mBitmap, 256, 256, false);
    }

    protected void doDraw(Canvas canvas) {
        canvas.drawColor(Color.BLACK);
        canvas.drawBitmap(mBitmap, 8, 8, null);
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    }

    //@Override
    public void surfaceCreated(SurfaceHolder holder) {
        if (!mThread.isAlive()) {
            mThread = new ViewThread(this);
            mThread.setRunning(true);
            mThread.start();
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
         if (mThread.isAlive()) {
             mThread.setRunning(false);
         }
    }
}

private static int[] colorMap() {

     int[] ltable = { 0xff000000, 0xff00000f, 0xff00001e, 0xff00002d, 0xff00003c, 0xff00004b, 
                    0xff00005a, 0xff000069, 0xff000078, 0xff000087, 0xff000096, 0xff0000a5, 
                    0xff0000b4, 0xff0000c3, 0xff0000d2, 0xff0000e1, 0xff0000f0, 0xff0000ff,
                    0xff0f00ff, 0xff1e00ff, 0xff2d00ff, 0xff3c00ff, 0xff4b00ff, 0xff5a00ff,
                    0xff6900ff, 0xff7800ff, 0xff8700ff, 0xff9600ff, 0xffa500ff, 0xffb400ff, 
                    0xffc300ff, 0xffd200ff, 0xffe100ff, 0xfff000ff, 0xffff00ff, 0xffff00f0,
                    0xffff00e1, 0xffff00d2, 0xffff00c3, 0xffff00b4, 0xffff00a5, 0xffff0096,
                    0xffff0087, 0xffff0078, 0xffff0069, 0xffff005a, 0xffff004b, 0xffff003c,
                    0xffff002d, 0xffff001e, 0xffff000f, 0xffff0000 };

     int[] mcolors = new int[4096];
     Random rand = new Random();

     for(int i=0; i < 4096; i++)
     {
         int num = rand.nextInt(52); 
         mcolors[i] = ltable[num];
     }

     return mcolors;
}

}

推荐答案

在我看来,你在呼唤你的线程只有一次,当被称为surfaceCreated()。在这之后,你的线程永远不会再次调用。我相信你需要像计时器,重新开始线程或致电线程的run()方法来显示另一个图像

It appears to me that you are calling your thread only once when surfaceCreated() is called. After that, your thread is never called again. I believe you need something like timer and reinitiate thread or call thread run() method to display another image.

一些其他的东西。

1)重新在每一个while循环画布持有人。

1) Recreate your canvas holder in every while loop.

while (run) {
         c = null;
         try {
            c =  panel.getHolder().lockCanvas();

2)此外,Android 1.5的后surfaceview不能每秒刷新更多的时间40-45所以尽量减少FPS为30,看它是否有什么差别。

2) Also, after android 1.5 a surfaceview can't be refresh more 40-45 time per second so try reducing fps to 30 and see if it makes any difference.

例如:<一href=\"http://www.anddev.org/android-2d-3d-graphics-opengl-problems-f55/surfaceview-portage-1-5-other-version-of-android-t15675.html\" rel=\"nofollow\">http://www.anddev.org/android-2d-3d-graphics-opengl-problems-f55/surfaceview-portage-1-5-other-version-of-android-t15675.html

这篇关于安卓:刷新位图新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆