如何在安卓中添加雪花效果?

3

我尝试添加飘雪效果,但没有成功。 我试图做出不断飘动雪花的效果。 这是可能的吗?如果可以,请给出建议。

以下是我的代码:

public class MainActivity extends Activity {

    private int COLOR_MAX = 0xff;

    ImageView image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        image = (ImageView)findViewById(R.id.imageView1);
        Bitmap imagebitmap = BitmapFactory.decodeResource(getResources(),R.drawable.hydrangeas);    
        applySnowEffect(imagebitmap);

    }
    Bitmap applySnowEffect(Bitmap source) 
    {

        // get image size
        int width = source.getWidth();
        int height = source.getHeight();
        int[] pixels = new int[width * height];

        // get pixel array from source
        source.getPixels(pixels, 0, width, 0, 0, width, height);

        // random object
        Random random = new Random();

        int R, G, B, index = 0, thresHold = 50;
        // iteration through pixels


        for(int y = 0; y < height; ++y) {
            for(int x = 0; x < width; ++x) {

                // get current index in 2D-matrix

                index = y * width + x;              
                // get color
                R = Color.red(pixels[index]);
                G = Color.green(pixels[index]);
                B = Color.blue(pixels[index]);
                // generate threshold
                thresHold = random.nextInt(COLOR_MAX );
                if(R > thresHold && G > thresHold && B > thresHold) {
                    pixels[index] = Color.rgb(COLOR_MAX, COLOR_MAX, COLOR_MAX);
                }                           
            }
        }
        // output bitmap                
        Bitmap bmOut = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
        bmOut.setPixels(pixels, 0, width, 0, 0, width, height);

        Toast.makeText(getApplicationContext(),"processed",10).show();
        return bmOut;


    }

你找到解决方案了吗? - powder366
2个回答

4

0

好的,我刚刚找到了解决方案。 源代码在这里:代码链接

主要思路是SnowFallView扩展了View,并覆盖了onDraw(Canvas canvas)事件,在这里我们绘制了雪花可绘制对象。

代码已经测试过并且运行良好。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接