天天看點

Android 水波紋效果實作

     這個小DEMO是朋友發給小馬的,忘了是誰,在這感謝下他, 沒有這個DEMO我真不知道安卓水波紋是怎麼實作的,這個不多講了,模拟器中有點卡的樣子,大家在真機上試是正常的小馬直接上代碼,上效果,有需要的朋友下載下傳下附件DEMO就可以了, 裡面有些是調用NATIVE庫的,希望CPP高手C高手多留言指點指點,放在這,供大家下載下傳學習交流,吼吼,好東西,必須分享,這個DEMO,小馬不多講了,直接寫了,原因看注釋裡面就行的了 效果如下:

Android 水波紋效果實作

  1. package com.example.plasma;  
  2. import android.app.Activity;  
  3. import android.content.Context;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.util.AttributeSet;  
  7. import android.view.MotionEvent;  
  8. import android.view.View;  
  9. import android.view.Window;  
  10. import android.graphics.Bitmap;  
  11. import android.graphics.BitmapFactory;  
  12. import android.graphics.Canvas;  
  13. import android.graphics.Color;  
  14. /**    
  15. * @Title: AnimActivity.java  
  16. * @Package com.example.plasma  
  17. * @Description: 安卓水波紋效果制作學習  
  18. * @author XiaoMa  
  19. *   
  20. * 這個小DEMO是朋友發給小馬的,忘了是誰,在這感謝下他,  
  21. * 沒有這個DEMO我真不知道安卓水波紋是怎麼實作的,這個不多講了,  
  22. * 模拟器中有點卡的樣子,大家在真機上試是正常的  
  23. * 小馬直接上代碼,上效果,有需要的朋友下載下傳下附件DEMO就可以了  
  24. * 裡面有些是調用NATIVE庫的,希望CPP高手C高手多留言指點指點,  
  25. * 放在這,供大家下載下傳學習交流,吼吼,好東西,必須分享  
  26. *   
  27. */ 
  28. public class AnimActivity extends Activity {  
  29.     @Override 
  30.     public void onCreate(Bundle savedInstanceState){  
  31.         super.onCreate(savedInstanceState);  
  32.         setContentView(new PlasmaView(this));  
  33. //      this.requestWindowFeature(Window.FEATURE_NO_TITLE);  
  34.     }  
  35. }  
  36. class PlasmaView extends View implements View.OnTouchListener{    
  37.     private Bitmap mBitmap;  
  38.     long time;  
  39.     long fps;  
  40.     public PlasmaView(Context context) {  
  41.         super(context);          
  42.         Bitmap bmp = BitmapFactory.decodeResource(this.getResources(),R.drawable.lvzi);  
  43.         mBitmap = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), Bitmap.Config.RGB_565);  
  44.         AnimRender.setBitmap(bmp);  
  45.         this.setOnTouchListener(this);  
  46.     }  
  47.     @Override   
  48.     protected void onDraw(Canvas canvas) {  
  49.         long ct = System.currentTimeMillis();  
  50.         if(ct - time > 1000){  
  51.             time = ct;  
  52.             fps = 0;  
  53.         }  
  54.         fps++;        
  55.         AnimRender.render(mBitmap);       
  56.         canvas.drawBitmap(mBitmap, 0, 0, null);  
  57.         postInvalidate();  
  58.     }  
  59.     @Override 
  60.     public boolean onTouch(View v, MotionEvent event) {  
  61.         AnimRender.drop((int)event.getX(), (int)event.getY(), 500);  
  62.         return false;  
  63.     }  
  64. }  
  65. class AnimRender{  
  66.     public static native void setBitmap(Bitmap src);  
  67.     public static native void render(Bitmap dst);  
  68.     public static native void drop(int x, int y, int height);  
  69.     static {  
  70.         System.loadLibrary("plasma");  
  71.     }  
  72. }