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;


    }
4

2 に答える 2

0

さて、私はちょうど解決策を見つけました。ソースはこちら:コードリンク

主なアイデアは、SnowFallView が View を拡張し、onDraw(Canvas canvas) イベントをオーバーライドして、雪片のドローアブルを描画することです。

コードはテスト済みで、問題なく動作します。

于 2015-06-07T16:34:00.477 に答える