【问题标题】:SurfaceView + Canvas, fade from one color to anotherSurfaceView + Canvas,从一种颜色淡入另一种颜色
【发布时间】:2012-08-31 22:17:35
【问题描述】:

我有

ArrayList<ColorDrawable> colors = new ArrayList<ColorDrawable>();
for(int i = 0; i = intList.size(); i++){ //some list of ints
    colors.add(new ColorDrawable(intList.get(i)));

我想使用 SurfaceView + Canvas 方法从列表中的一种颜色淡化为另一种颜色。这是我的尝试:

 public void run() {
    int maxIndex = colors.size() - 1;
        while (isItOK) {
            for (int i = 0; i <= maxIndex; i++) {
                int color = colors.get(i).getColor();
                int nextColor = (i == maxIndex) ? colors.get(0).getColor() : colors.get(i + 1).getColor();

                if(color < nextColor) {
                    for(; color <= nextColor; color++) {
                        Canvas c = holder.lockCanvas();
                        c.drawColor(color);
                        holder.unlockCanvasAndPost(c);
                    }
                }


                if(color > nextColor) {
                    for(; color >= nextColor; color--) {
                        Canvas c = holder.lockCanvas();
                        c.drawColor(color);
                        holder.unlockCanvasAndPost(c);
                    }
                }
            }

        }
    }

我觉得这应该按原样工作,并从第一种颜色渐变到第二种颜色,依此类推......最终循环,但相反,它从第一种颜色开始,并渐变为一些不相关的颜色, 一遍又一遍。 (我也测试了不同的数据)。这是我第一次使用 SurfaceView,所以我不确定我的画布方法是否正确。 使用 Log.d,我看到一旦它进入内部 for 循环之一(前面带有“if”语句的循环),它就不会离开那个 for 循环....对我来说没有意义,但我认为这与画布和支架有关。帮忙?

【问题讨论】:

    标签: android surfaceview surfaceholder


    【解决方案1】:

    我不确定,如果我理解正确,如果没有,请告诉我。

    据我了解,您希望:遍历颜色并将每次背景色设置为颜色列表的第 i 个元素

    更新:注意,我还没有测试过它!

    int currentIndex = 0;
    int nextIndex = 0;
    
    while (isItOK) 
    {
        nextIndex = (currentIndex + 1) % colors.size();
    
        int currentColor = colors.get(currentIndex).getColor();
        int nextColor = colors.get(nextIndex).getColor();
        while(currentColor != nextColor)
        {
            //extract red, green, blue, alpha from the current color
    
            int r = Color.red(currentColor); //android.graphics.Color
            int g = Color.green(currentColor);
            int b = Color.blue(currentColor);
            int a = Color.alpha(currentColor);
    
            //extract the same from nextColor
            int nr = Color.red(nextColor);
            int ng = Color.green(nextColor);
            int nb = Color.blue(nextColor);
            int na = Color.alpha(nextColor);
    
            //get currentColors values closer to nextColor 
            r = (r<nr) ? r+1 : ((r>nr) ? r-1 : r);
            g = (g<ng) ? g+1 : ((g>ng) ? g-1 : g);
            b = (b<nb) ? b+1 : ((b>nb) ? b-1 : b);
            a = (a<na) ? a+1 : ((a>ar) ? a-1 : a);
    
            // generate currentColor back to a single int
            currentColor = Color.argb(a,r,g,b);
    
            // paint it 
            Canvas canvas = holder.lockCanvas();
            canvas.drawColor(currentColor );
            holder.unlockCanvasAndPost(canvas);
        }
        currentIndex = (currentIndex + 1) % colors.size();
    }
    

    【讨论】:

    • 比较是因为我想从一种颜色渐变到另一种颜色。我可以用你提供的代码让它快速闪烁。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-18
    • 1970-01-01
    • 1970-01-01
    • 2013-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多