本文實例講述了Android編程實現扭曲圖像的繪制功能。分享給大家供大家參考,具體如下:
為了實現動畫效果,使用drawBitmapMess方法對圖像進行扭曲,使用定時器以100毫秒的頻率按圓形軌跡扭曲圖像。
扭曲的關鍵是生成verts數組。本例一開始會先生成verts數組的初始值:有一定水平和垂直間距的網點坐標。然后通過warp方法按一定的數學方法變化verts數組中的坐標。關鍵部分的代碼如下:
定義基本變量:MyView是用于顯示扭曲的圖像的自定義view,angle是圓形軌跡的當前角度:
private static Bitmap bitmap;private MyView myView;private int angle = 0; // 圓形軌跡當前的角度private Handler handler = new Handler(){ public void handleMessage(Message msg) { switch (msg.what) { case 1: Random random = new Random(); // 計算圖形中心點坐標 int centerX = bitmap.getWidth() / 2; int centerY = bitmap.getHeight() / 2; double radian = Math.toRadians((double) angle); // 通過圓心坐標、半徑和當前角度計算當前圓周的某點橫坐標 int currentX = (int) (centerX + 100 * Math.cos(radian)); // 通過圓心坐標、半徑和當前角度計算當前圓周的某點縱坐標 int currentY = (int) (centerY + 100 * Math.sin(radian)); // 重繪View,并在圓周的某一點扭曲圖像 myView.mess(currentX, currentY); angle += 2; if (angle > 360) angle = 0; break; } super.handleMessage(msg); }};private TimerTask timerTask = new TimerTask(){ public void run() { Message message = new Message(); message.what = 1; handler.sendMessage(message); }以下是自定義view,MyView的具體內容:
private static class MyView extends View{ private static final int WIDTH = 20; private static final int HEIGHT = 20; private static final int COUNT = (WIDTH + 1) * (HEIGHT + 1); private final float[] verts = new float[COUNT * 2]; private final float[] orig = new float[COUNT * 2]; private final Matrix matrix = new Matrix(); private final Matrix m = new Matrix(); // 設置verts數組的值 private static void setXY(float[] array, int index, float x, float y) { array[index * 2 + 0] = x; array[index * 2 + 1] = y; } public MyView(Context context) { super(context); setFocusable(true); bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image); float w = bitmap.getWidth(); float h = bitmap.getHeight(); int index = 0; // 生成verts和orig數組的初始值,這兩個數組的值是一樣的,只是在扭曲的過程中需要修改verts // 的值,而修改verts的值要將原始的值保留在orig數組中 for (int y = 0; y <= HEIGHT; y++) { float fy = h * y / HEIGHT; for (int x = 0; x <= WIDTH; x++) { float fx = w * x / WIDTH; setXY(verts, index, fx, fy); setXY(orig, index, fx, fy); index += 1; } } matrix.setTranslate(10, 10); setBackgroundColor(Color.WHITE); } @Override protected void onDraw(Canvas canvas) { canvas.concat(matrix); canvas.drawBitmapMesh(bitmap, WIDTH, HEIGHT, verts, 0, null, 0,null); } // 用于扭曲圖像的方法,在該方法中根據當前扭曲的點(扭曲區域的中心點),也就是cx和cy參數, // 來不斷變化verts數組中的坐標值 private void warp(float cx, float cy) { final float K = 100000; // 該值越大,扭曲得越嚴重(扭曲的范圍越大) float[] src = orig; float[] dst = verts; // 按一定的數學規則生成verts數組中的元素值 for (int i = 0; i < COUNT * 2; i += 2) { float x = src[i + 0]; float y = src[i + 1]; float dx = cx - x; float dy = cy - y; float dd = dx * dx + dy * dy; float d = FloatMath.sqrt(dd); float pull = K / ((float) (dd *d)); if (pull >= 1) { dst[i + 0] = cx; dst[i + 1] = cy; } else { dst[i + 0] = x + dx * pull; dst[i + 1] = y + dy * pull; } } } // 用于MyView外部控制圖像扭曲的方法。該方法在handleMessage方法中被調用 public void mess(int x, int y) { float[] pt ={ x, y }; m.mapPoints(pt); // 重新生成verts數組的值 warp(pt[0], pt[1]); invalidate(); } }}以下是Activity的onCreate方法:
protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); myView = new MyView(this); setContentView(myView); Timer timer = new Timer(); // 開始定時器 timer.schedule(timerTask, 0, 100);}下面來看看扭曲后的效果,不同時刻,圖片呈現出不同的扭曲效果:

希望本文所述對大家Android程序設計有所幫助。
新聞熱點
疑難解答