您好,登錄后才能下訂單哦!
怎么在Android中實現(xiàn)繪制扭曲圖像?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
定義基本變量: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的具體內(nèi)容:
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(); // 設(shè)置verts數(shù)組的值 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數(shù)組的初始值,這兩個數(shù)組的值是一樣的,只是在扭曲的過程中需要修改verts // 的值,而修改verts的值要將原始的值保留在orig數(shù)組中 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); } // 用于扭曲圖像的方法,在該方法中根據(jù)當前扭曲的點(扭曲區(qū)域的中心點),也就是cx和cy參數(shù), // 來不斷變化verts數(shù)組中的坐標值 private void warp(float cx, float cy) { final float K = 100000; // 該值越大,扭曲得越嚴重(扭曲的范圍越大) float[] src = orig; float[] dst = verts; // 按一定的數(shù)學規(guī)則生成verts數(shù)組中的元素值 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方法中被調(diào)用 public void mess(int x, int y) { float[] pt ={ x, y }; m.mapPoints(pt); // 重新生成verts數(shù)組的值 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); }
下面來看看扭曲后的效果,不同時刻,圖片呈現(xiàn)出不同的扭曲效果:
看完上述內(nèi)容,你們掌握怎么在Android中實現(xiàn)繪制扭曲圖像的方法了嗎?如果還想學到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。